From d26dbab8f789d787d5c39f8700b4ac19701f286f Mon Sep 17 00:00:00 2001 From: Julien Sere Date: Sat, 22 Jan 2011 17:22:13 +0000 Subject: initial check out java git-svn-id: file:///var/svn/2011-usi/trunk@13 db941bf7-0cb3-4dda-9634-87faf64f93a7 --- app/v2_java/src/org/usi2011/Database.java | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 app/v2_java/src/org/usi2011/Database.java (limited to 'app/v2_java/src/org/usi2011/Database.java') diff --git a/app/v2_java/src/org/usi2011/Database.java b/app/v2_java/src/org/usi2011/Database.java new file mode 100644 index 0000000..6ebb63a --- /dev/null +++ b/app/v2_java/src/org/usi2011/Database.java @@ -0,0 +1,73 @@ +package org.usi2011; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * @author XKHM6852 + * + */ +public final class Database { + + private static Connection connection; + + /** */ + public static Connection getConnection() { + if (connection == null) { + createDatabase(); + } + return connection; + } + + private static void createDatabase() { + try { + connection = DriverManager.getConnection("jdbc:hsqldb:mem:aname", + "sa", ""); + String query = "DROP TABLE game; CREATE TABLE game ( " + + " id INT NOT NULL PRIMARY KEY, " + + " longpollingduration INT NOT NULL, " + + " nbusersthreshold INT NOT NULL," + + " questiontimeframe INT NOT NULL," + + " nbquestions INT NOT NULL" + ");"; + + Statement statement = connection.createStatement(); + ResultSet resultset = statement.executeQuery(query); + connection.commit(); + createUser(); + + } catch (SQLException e) { + + System.out.println("SQL Exception " + e.getMessage()); + } + } + + private static void createUser() { + /* + `id` int(11) NOT NULL AUTO_INCREMENT, + `firstname` varchar(255) COLLATE utf8_bin NOT NULL, + `lastname` varchar(255) COLLATE utf8_bin NOT NULL, + `mail` varchar(255) COLLATE utf8_bin NOT NULL, + `password` varchar(255) COLLATE utf8_bin NOT NULL, + */ + try { + String query = "DROP TABLE user; CREATE TABLE user ( " + + " id INT GENERATED BY DEFAULT AS IDENTITY, " + + " firstname VARCHAR NOT NULL, " + + " lastname VARCHAR NOT NULL," + + " mail VARCHAR NOT NULL," + + " password VARCHAR NOT NULL" + ");"; + + Statement statement = connection.createStatement(); + ResultSet resultset = statement.executeQuery(query); + connection.commit(); + + } catch (SQLException e) { + + System.out.println("SQL Exception " + e.getMessage()); + } + } + +} -- cgit v1.2.3