Commit 2de5496f authored by Rakhmatullah Yoga Sutrisna's avatar Rakhmatullah Yoga Sutrisna
Browse files

create database handler

parent c290cb32
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=GrandQuestServer
application.vendor=Rakhmatullah Yoga S
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
......@@ -26,12 +27,15 @@ dist.archive.excludes=
dist.dir=dist
dist.jar=${dist.dir}/GrandQuestServer.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.json-simple-1.1.1.jar=lib\\json-simple-1.1.1.jar
file.reference.mysql-connector-java-5.1.34-bin.jar=lib/mysql-connector-java-5.1.34-bin.jar
includes=**
jar.compress=false
javac.classpath=\
${file.reference.json-simple-1.1.1.jar}
${file.reference.json-simple-1.1.1.jar}:\
${file.reference.mysql-connector-java-5.1.34-bin.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package grandquestserver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Rakhmatullah Yoga S
*/
public class DBHandler {
private final Connection koneksi;
private final String databaseURL;
private final String username;
private final String password;
private final Statement statement;
public DBHandler() throws SQLException {
databaseURL = "jdbc:mysql://localhost:3306/philosopher_stone";
username = "root";
password = "";
koneksi = DriverManager.getConnection(databaseURL, username, password);
statement = koneksi.createStatement();
}
public void addUser(String username, String password) {
try {
statement.executeUpdate("insert into user (username, password)"
+ "values ('" + username + "', '" + password + "')");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public boolean authenticate(String username, String password) throws SQLException {
boolean authenticated = false;
ResultSet result = statement.executeQuery("select * from user where username like '" + username + "' and password like '" + password + "'");
while(result.next()) {
String user = result.getString("username");
String pass = result.getString("password");
authenticated = (username.equals(user) && password.equals(pass));
}
return authenticated;
}
public void addInventory(String username, int item) {
try {
statement.executeUpdate("insert into inventory (username, item)"
+ "values ('" + username + "', " + item + ")");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
......@@ -7,6 +7,7 @@
package grandquestserver;
import java.io.IOException;
import java.sql.SQLException;
/**
*
......@@ -17,11 +18,17 @@ public class GrandQuestServer {
/**
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.sql.SQLException
* @throws java.lang.ClassNotFoundException
* @throws java.lang.InstantiationException
* @throws java.lang.IllegalAccessException
*/
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
int port = 8000;
Thread t = new Server(port);
t.start();
//Thread t = new Server(port);
//t.start();
DBHandler database = new DBHandler();
database.addInventory("ardi", 2);
}
}
......@@ -27,7 +27,7 @@ public class Server extends Thread {
private ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
//serverSocket.setSoTimeout(10000);
}
public String Login(String input) throws ParseException {
......@@ -71,6 +71,7 @@ public class Server extends Thread {
out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!");
}
server.close();
} catch (ParseException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment