From 2d2cc3c924c5cd900880edbe6a5967b979e3579f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 18 Jul 2016 14:36:33 +0200 Subject: [PATCH] Using JDA --- pom.xml | 6 ++++++ .../norbipeti/chat/server/IndexPage.java | 2 +- src/io/github/norbipeti/chat/server/Main.java | 14 +++++--------- .../norbipeti/chat/server/db/DataProvider.java | 18 ++++++++++++++++++ src/main/resources/META-INF/persistence.xml | 11 +++++++++++ 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 src/io/github/norbipeti/chat/server/db/DataProvider.java create mode 100644 src/main/resources/META-INF/persistence.xml diff --git a/pom.xml b/pom.xml index 21f4084..08db502 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,12 @@ derbyclient 10.12.1.1 + + + org.hibernate + hibernate-core + 5.2.1.Final + 1.8 diff --git a/src/io/github/norbipeti/chat/server/IndexPage.java b/src/io/github/norbipeti/chat/server/IndexPage.java index 19994f0..015bbbf 100644 --- a/src/io/github/norbipeti/chat/server/IndexPage.java +++ b/src/io/github/norbipeti/chat/server/IndexPage.java @@ -8,7 +8,7 @@ public class IndexPage extends Page { @Override public void handle(HttpExchange exchange) throws IOException { - + IOHelper.SendResponse(200, "

Index

", exchange); } } diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 3b851c5..364d8f3 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -11,6 +11,8 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; +import io.github.norbipeti.chat.server.db.DataProvider; + public class Main { // public static final HashMap Pages = new HashMap(); @@ -20,15 +22,9 @@ public class Main { // https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/ // https://docs.oracle.com/javase/8/docs/api/ System.out.println("Loading database..."); - Connection conn = DriverManager.getConnection("jdbc:derby:memory:chatserver;create=true"); - Statement statement = conn.createStatement(); - if (statement.execute("CREATE TABLE users ( username varchar(255), password varchar(255), id int )")) - System.out.println("Created users table"); - else - System.out.println("Failed to create users table!"); - ResultSet results = statement.executeQuery("SELECT * FROM users"); - while (results.next()) - results.getString(0); + try (DataProvider provider = new DataProvider()) { + + } System.out.println("Starting webserver..."); HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); server.createContext("/").setHandler(new IndexPage()); diff --git a/src/io/github/norbipeti/chat/server/db/DataProvider.java b/src/io/github/norbipeti/chat/server/db/DataProvider.java new file mode 100644 index 0000000..761c89f --- /dev/null +++ b/src/io/github/norbipeti/chat/server/db/DataProvider.java @@ -0,0 +1,18 @@ +package io.github.norbipeti.chat.server.db; + +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +public class DataProvider implements AutoCloseable { + private EntityManagerFactory emf; + + public DataProvider() { + Persistence.createEntityManagerFactory("ChatServerPU"); + } + + @Override + public void close() { + if (emf != null) + emf.close(); + } +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..a7a89c1 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file