summaryrefslogtreecommitdiff
path: root/app/v2_java/src/org/usi2011/Database.java
blob: 6ebb63afb108d8c2b85a2cc6d540e5cd31fd2488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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());
		}
	}

}