Database Update
This commit is contained in:
+14
-20
@@ -14,7 +14,7 @@ public class ReadDAO extends AbstractDAO {
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
ResultSet resultSet = resultSetStatement("select name from gameInfo");
|
||||
ResultSet resultSet = resultSetStatement("select name from GAME");
|
||||
|
||||
while (true){
|
||||
try {
|
||||
@@ -38,32 +38,26 @@ public class ReadDAO extends AbstractDAO {
|
||||
|
||||
connect();
|
||||
|
||||
ResultSet resultInfo = resultSetStatement("select * from gameInfo where name = '"+gridName+"'");
|
||||
ResultSet res = resultSetStatement("select * from GAME where name = '"+gridName+"'");
|
||||
|
||||
try {
|
||||
resultInfo.next();
|
||||
int id = resultInfo.getInt("id");
|
||||
int width = resultInfo.getInt("width");
|
||||
int height = resultInfo.getInt("height");
|
||||
res.next();
|
||||
int width = res.getInt("width");
|
||||
int height = res.getInt("height");
|
||||
String data = res.getString("data");
|
||||
res.close();
|
||||
|
||||
resultInfo.close();
|
||||
|
||||
boolean[][] cells = new boolean[width][height];
|
||||
|
||||
ResultSet resultData = resultSetStatement("select * from gameData where id ="+id);
|
||||
|
||||
while (resultData.next()){
|
||||
cells[resultData.getInt("x")][resultData.getInt("y")] = true;
|
||||
}
|
||||
ArrayParser arrayParser = new ArrayParser();
|
||||
|
||||
boolean[][] grid = arrayParser.deserialize(width, height, data);
|
||||
disconnect();
|
||||
|
||||
return cells;
|
||||
return grid;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
disconnect();
|
||||
}
|
||||
|
||||
disconnect();
|
||||
|
||||
return null;
|
||||
@@ -73,7 +67,7 @@ public class ReadDAO extends AbstractDAO {
|
||||
|
||||
connect();
|
||||
|
||||
ResultSet resultSet = resultSetStatement("select generation from gameInfo where name = '"+gridName+"'");
|
||||
ResultSet resultSet = resultSetStatement("select generation from GAME where name = '"+gridName+"'");
|
||||
try {
|
||||
|
||||
resultSet.next();
|
||||
@@ -94,7 +88,7 @@ public class ReadDAO extends AbstractDAO {
|
||||
|
||||
connect();
|
||||
|
||||
ResultSet resultSet = resultSetStatement("select CELLCOUNT from gameInfo where name = '"+gridName+"'");
|
||||
ResultSet resultSet = resultSetStatement("select CELLCOUNT from GAME where name = '"+gridName+"'");
|
||||
try {
|
||||
resultSet.next();
|
||||
int cellCount = resultSet.getInt("CELLCOUNT");
|
||||
|
||||
@@ -4,13 +4,13 @@ import org.h2.tools.DeleteDbFiles;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class WriteDAO extends AbstractDAO{
|
||||
|
||||
public void createTables(){
|
||||
public void createTable(){
|
||||
connect();
|
||||
simpleStatement("create table IF NOT EXISTS gameInfo(id int not null auto_increment, name varchar, width int, height int, generation int, cellcount int)");
|
||||
simpleStatement("create table IF NOT EXISTS gameData(id int, x int, y int)");
|
||||
simpleStatement("create table if not exists GAME(id int auto_increment,name varchar not null,width int not null,height int not null,generation int not null,max_cellcount int not null,data clob);create unique index GAME_ID_UINDEX on GAME (id);create unique index GAME_NAME_UINDEX on GAME (name);");
|
||||
disconnect();
|
||||
}
|
||||
|
||||
@@ -24,30 +24,13 @@ public class WriteDAO extends AbstractDAO{
|
||||
DeleteDbFiles.execute(dbDir, dbName, quiet);
|
||||
}
|
||||
|
||||
public void saveGrid(boolean[][] grid, String gridName, int generation, int cellCount){
|
||||
public void saveGrid(boolean[][] grid, String gridName, int generation, int maxCellCount){
|
||||
|
||||
ArrayParser arrayParser = new ArrayParser();
|
||||
String data = arrayParser.serialize(grid);
|
||||
|
||||
connect();
|
||||
|
||||
simpleStatement("insert into GAMEINFO(NAME, WIDTH, HEIGHT, GENERATION, CELLCOUNT) values ('"+gridName+"', "+grid.length+", "+grid[0].length+", "+generation+","+cellCount+")");
|
||||
ResultSet resultSet = resultSetStatement("select * from gameInfo order by id desc limit 1");
|
||||
|
||||
|
||||
try {
|
||||
resultSet.next();
|
||||
int id = resultSet.getInt("id");
|
||||
|
||||
for (int x = 0; x < grid.length; x++) {
|
||||
for (int y = 0; y < grid[0].length; y++) {
|
||||
if (grid[x][y]){
|
||||
simpleStatement("insert into gameData (id, x, y) values ("+id+", "+x+", "+y+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
simpleStatement("insert into GAME (NAME, WIDTH, HEIGHT, GENERATION, MAX_CELLCOUNT, DATA) VALUES ( '"+gridName+"', "+grid[0].length+", "+grid.length+", "+generation+", "+maxCellCount+", '"+data+"' )");
|
||||
disconnect();
|
||||
|
||||
}
|
||||
|
||||
@@ -61,13 +61,13 @@ public class DrawGrid extends JPanel {
|
||||
cellSizeY *= ratio;
|
||||
}
|
||||
|
||||
for (int x = 0; x < xSize; x++)
|
||||
for (int y = 0; y < xSize; y++)
|
||||
{
|
||||
for (int y = 0; y < ySize; y++)
|
||||
for (int x = 0; x < ySize; x++)
|
||||
{
|
||||
if (currentGrid[x][y])
|
||||
if (currentGrid[y][x])
|
||||
{
|
||||
g2d.fillRect((int) ((x * cellSizeX) + offset), (int) ((y * cellSizeY) + offset), (int) ((cellSizeX + 1)), (int) (cellSizeY + 1));
|
||||
g2d.fillRect((int) ((y * cellSizeY) + offset), (int) ((x * cellSizeX) + offset), (int) ((cellSizeY + 1)), (int) (cellSizeX + 1));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -1,8 +1,13 @@
|
||||
package main;
|
||||
|
||||
|
||||
import database.ArrayParser;
|
||||
import database.WriteDAO;
|
||||
import gamecontrol.Control;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
|
||||
//TODO: Database Seve Button.
|
||||
@@ -28,9 +33,6 @@ public class Main {
|
||||
|
||||
Control control = new Control();
|
||||
control.start();
|
||||
// ReadDAO dataBase = new ReadDAO();
|
||||
// dataBase.deleteTable("gameData");
|
||||
// dataBase.createTables();
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user