diff options
author | Julien Sere <jsere> | 2011-01-22 17:22:13 +0000 |
---|---|---|
committer | Julien Sere <jsere> | 2011-01-22 17:22:13 +0000 |
commit | d26dbab8f789d787d5c39f8700b4ac19701f286f (patch) | |
tree | 4525a459c2c6582e6015fd82714f7c09b791efb9 /app/v2_java/src | |
parent | a1051d374d9d7e0892dda3bf19bd4224ff0fa06b (diff) | |
download | 2011-usi-d26dbab8f789d787d5c39f8700b4ac19701f286f.tar.gz 2011-usi-d26dbab8f789d787d5c39f8700b4ac19701f286f.tar.bz2 2011-usi-d26dbab8f789d787d5c39f8700b4ac19701f286f.zip |
initial check out java
git-svn-id: file:///var/svn/2011-usi/trunk@13 db941bf7-0cb3-4dda-9634-87faf64f93a7
Diffstat (limited to 'app/v2_java/src')
-rw-r--r-- | app/v2_java/src/org/usi2011/Database.java | 73 | ||||
-rw-r--r-- | app/v2_java/src/org/usi2011/Question.java | 67 | ||||
-rw-r--r-- | app/v2_java/src/org/usi2011/User.java | 56 |
3 files changed, 196 insertions, 0 deletions
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());
+ }
+ }
+
+}
diff --git a/app/v2_java/src/org/usi2011/Question.java b/app/v2_java/src/org/usi2011/Question.java new file mode 100644 index 0000000..0c391d3 --- /dev/null +++ b/app/v2_java/src/org/usi2011/Question.java @@ -0,0 +1,67 @@ +package org.usi2011;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Servlet implementation class question
+ */
+public class Question extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+ private Connection connection;
+ /**
+ * Default constructor.
+ */
+ public Question() {
+ super();
+ connection = Database.getConnection();
+ }
+
+ /**
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
+ * response)
+ */
+ protected void doGet(HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
+ ServletOutputStream out = response.getOutputStream();
+
+ try {
+ String query = "INSERT INTO game VALUES (1, 2, 3, 4, 5)";
+ Statement statement = connection.createStatement();
+ ResultSet resultset = statement.executeQuery(query);
+ query = "SELECT * FROM game";
+ statement = connection.createStatement();
+ resultset = statement.executeQuery(query);
+ while (resultset.next()) {
+ out.println(resultset.getString(1));
+ }
+ } catch (SQLException e) {
+ response.sendError(400);
+ }
+
+
+ out.println("hello world");
+ out.println(request.getRequestURI());
+
+ }
+
+ /**
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
+ * response)
+ */
+ protected void doPost(HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
+ // TODO Auto-generated method stub
+ }
+
+}
diff --git a/app/v2_java/src/org/usi2011/User.java b/app/v2_java/src/org/usi2011/User.java new file mode 100644 index 0000000..091b4eb --- /dev/null +++ b/app/v2_java/src/org/usi2011/User.java @@ -0,0 +1,56 @@ +package org.usi2011;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Servlet implementation class User
+ */
+public class User extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+ private Connection connection;
+ /**
+ * @see HttpServlet#HttpServlet()
+ */
+ public User() {
+ super();
+ connection = Database.getConnection();
+ }
+
+ /**
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+ */
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ try {
+ String firstname = request.getParameter("firstname");
+ String lastname = request.getParameter("lastname");
+ String mail = request.getParameter("mail");
+ String password = request.getParameter("password");
+
+ System.out.println(firstname);
+ System.out.println(lastname);
+ System.out.println(mail);
+ System.out.println(password);
+
+ String query = "INSERT INTO game(firstname, lastname, mail) VALUES ('"+firstname+"', '"+lastname+"', '"+mail+"', '"+password+"')";
+ Statement statement = connection.createStatement();
+ ResultSet resultset = statement.executeQuery(query);
+
+ response.setStatus(response.SC_CREATED);
+
+ } catch (SQLException e) {
+ System.out.println(e.getMessage());
+ response.sendError(400);
+ }
+ }
+
+}
|