Database Implemented
This commit is contained in:
@@ -7,5 +7,6 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="h2-1.4.200" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -0,0 +1,124 @@
|
||||
package data.database;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DataBase {
|
||||
|
||||
private Connection connection;
|
||||
private Statement statement;
|
||||
|
||||
public void connect(){
|
||||
try {
|
||||
connection = DriverManager.getConnection("jdbc:h2:./lib/Conway-Game-Of-Live", "sa", "");
|
||||
statement = connection.createStatement();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect(){
|
||||
|
||||
try {
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void simpleSQL(String sqlStatement){
|
||||
try {
|
||||
statement.executeUpdate(sqlStatement);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet resultSetSQL(String sqlStatement){
|
||||
try {
|
||||
return statement.executeQuery(sqlStatement);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public void createTables(){
|
||||
connect();
|
||||
|
||||
simpleSQL("create table gameInfo(id int not null auto_increment, name varchar, width int, height int, generation int)");
|
||||
simpleSQL("create table gameData(id int, x int, y int)");
|
||||
|
||||
disconnect();
|
||||
}
|
||||
|
||||
public void deleteTables(){
|
||||
connect();
|
||||
|
||||
simpleSQL("drop table gameInfo");
|
||||
simpleSQL("drop table gameData");
|
||||
|
||||
disconnect();
|
||||
}
|
||||
|
||||
public List<String> getAllGrids(){
|
||||
|
||||
List<String> allGrids = new ArrayList<>();
|
||||
|
||||
connect();
|
||||
|
||||
ResultSet resultSet = resultSetSQL("select name from gameInfo");
|
||||
|
||||
while (true){
|
||||
try {
|
||||
if (!resultSet.next()) break;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
allGrids.add(resultSet.getString(1));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
disconnect();
|
||||
|
||||
return allGrids;
|
||||
|
||||
}
|
||||
|
||||
public void saveGrid(boolean[][] grid, String gridName, int generation){
|
||||
|
||||
connect();
|
||||
|
||||
simpleSQL("insert into GAMEINFO(name, width, height, GENERATION) values ('"+gridName+"', "+grid.length+", "+grid[0].length+", "+generation+")");
|
||||
ResultSet resultSet = resultSetSQL("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]){
|
||||
simpleSQL("insert into gameData (id, x, y) values ("+id+", "+x+", "+y+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
disconnect();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
package game;
|
||||
|
||||
import data.database.DataBase;
|
||||
import gui.GUI;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static java.lang.Math.round;
|
||||
@@ -10,6 +13,7 @@ public class Control {
|
||||
|
||||
private GUI gui;
|
||||
private Clock clock;
|
||||
private DataBase dataBase;
|
||||
|
||||
private int xSize, ySize, cellCount, gen;
|
||||
|
||||
@@ -28,6 +32,7 @@ public class Control {
|
||||
|
||||
clock = new Clock(this, gui);
|
||||
gui = new GUI(this);
|
||||
dataBase = new DataBase();
|
||||
gui.createMenuWindow();
|
||||
|
||||
}
|
||||
@@ -422,8 +427,12 @@ public class Control {
|
||||
resetCells();
|
||||
syncCells();
|
||||
|
||||
int counter = ((gen - wantedtGen) / wantedtGen ) + 1;
|
||||
|
||||
for (int i = 0; i < wantedtGen; i++) {
|
||||
nextGenLight();
|
||||
gen = gen - counter;
|
||||
gui.updateGameWindowTitle(gen);
|
||||
|
||||
}
|
||||
|
||||
@@ -434,6 +443,10 @@ public class Control {
|
||||
multipleGenSkip(wantedtGen - gen);
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getAllGrids(){
|
||||
return dataBase.getAllGrids();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.List;
|
||||
|
||||
public class GUI {
|
||||
|
||||
@@ -176,5 +178,9 @@ public class GUI {
|
||||
return controlWindow;
|
||||
}
|
||||
|
||||
public List<String> getAllGrids(){
|
||||
return control.getAllGrids();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -306,6 +306,10 @@ public class MenuWindow {
|
||||
addComboBoxEntry(modeBox, "Blinker");
|
||||
addComboBoxEntry(modeBox, "Toad");
|
||||
|
||||
for (int i = 0; i < gui.getAllGrids().size(); i++){
|
||||
addComboBoxEntry(modeBox, gui.getAllGrids().get(i));
|
||||
}
|
||||
|
||||
modeName = new JLabel("Select Figure: ");
|
||||
modeName.setBounds(50, 250, 200, 30);
|
||||
modeName.setFont(text);
|
||||
|
||||
+24
-2
@@ -1,6 +1,7 @@
|
||||
package main;
|
||||
|
||||
|
||||
import data.database.DataBase;
|
||||
import game.Control;
|
||||
|
||||
public class Main {
|
||||
@@ -12,8 +13,29 @@ public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Control control = new Control();
|
||||
control.start();
|
||||
// Control control = new Control();
|
||||
// control.start();
|
||||
DataBase dataBase = new DataBase();
|
||||
dataBase.deleteTables();
|
||||
dataBase.createTables();
|
||||
|
||||
|
||||
// boolean[][] cells = new boolean[44][66];
|
||||
//
|
||||
// for (int x = 0; x < 6; x++) {
|
||||
// for (int y = 0; y < 6; y++) {
|
||||
// cells[x][y] = false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// cells[3][3] = true;
|
||||
//
|
||||
// dataBase.saveGrid(cells, "whoa", 88);
|
||||
//
|
||||
// System.out.println(dataBase.getAllGrids());
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user