changed folder structure
This commit is contained in:
87
src/com/wasteinformationserver/db/Database.java
Executable file
87
src/com/wasteinformationserver/db/Database.java
Executable file
@ -0,0 +1,87 @@
|
||||
package com.wasteinformationserver.db;
|
||||
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Vector;
|
||||
|
||||
public abstract class Database {
|
||||
|
||||
protected String user;
|
||||
protected String password;
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
|
||||
protected String dbName;
|
||||
|
||||
public Database(String user, String password, String host, int port, String dbName) {
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.dbName = dbName;
|
||||
}
|
||||
|
||||
public abstract Connection getConnection() throws SQLException;
|
||||
|
||||
public static void logToConsole(ResultSet res) {
|
||||
try {
|
||||
|
||||
ResultSetMetaData rsmd = res.getMetaData();
|
||||
|
||||
while (res.next()) {
|
||||
String row = "";
|
||||
|
||||
for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
|
||||
if (row.length() > 0) {
|
||||
row += ", ";
|
||||
}
|
||||
|
||||
if (res.getObject(i) != null) {
|
||||
row +=
|
||||
rsmd.getColumnName(i) +
|
||||
": " +
|
||||
res.getObject(i).toString();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(row);
|
||||
}
|
||||
|
||||
} catch(SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public static DefaultTableModel logToTable(ResultSet res) {
|
||||
try {
|
||||
ResultSetMetaData rsmd = res.getMetaData();
|
||||
Vector<String> columnNames = new Vector<>();
|
||||
int columnCount = rsmd.getColumnCount();
|
||||
|
||||
for (int i = 1; i <= columnCount; ++i) {
|
||||
columnNames.add(rsmd.getColumnName(i));
|
||||
}
|
||||
|
||||
Vector<Vector<Object>> data = new Vector<>();
|
||||
|
||||
while(res.next()) {
|
||||
Vector<Object> row = new Vector();
|
||||
|
||||
for (int i = 1; i <= columnCount; ++i) {
|
||||
row.add(res.getObject(i));
|
||||
}
|
||||
|
||||
data.add(row);
|
||||
}
|
||||
|
||||
return new DefaultTableModel(data, columnNames);
|
||||
} catch(SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
28
src/com/wasteinformationserver/db/MySQLConnector.java
Executable file
28
src/com/wasteinformationserver/db/MySQLConnector.java
Executable file
@ -0,0 +1,28 @@
|
||||
package com.wasteinformationserver.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class MySQLConnector extends Database {
|
||||
|
||||
static {
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public MySQLConnector(String user, String password, String host, int port, String dbName) {
|
||||
super(user, password, host, port, dbName);
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(
|
||||
"jdbc:mysql://" + host + ":" + port + "/" + dbName + "?useSSL=false",
|
||||
user,
|
||||
password);
|
||||
}
|
||||
|
||||
}
|
48
src/com/wasteinformationserver/db/PostRequest.java
Normal file
48
src/com/wasteinformationserver/db/PostRequest.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.wasteinformationserver.db;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class PostRequest implements HttpHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange httpExchange) throws IOException {
|
||||
if (httpExchange.getRequestMethod().equals("POST")) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
InputStream ios = httpExchange.getRequestBody();
|
||||
int i;
|
||||
while ((i = ios.read()) != -1) {
|
||||
sb.append((char) i);
|
||||
}
|
||||
String query = sb.toString();
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
|
||||
String[] res = query.split("&");
|
||||
for (String str : res) {
|
||||
String[] values = str.split("=");
|
||||
params.put(values[0], values[1]);
|
||||
}
|
||||
|
||||
String response = request(params);
|
||||
|
||||
|
||||
Headers h = httpExchange.getResponseHeaders();
|
||||
h.set("Content-Type", "application/json");
|
||||
httpExchange.sendResponseHeaders(200, 0);
|
||||
|
||||
OutputStream os = httpExchange.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract String request(HashMap<String, String> params);
|
||||
|
||||
}
|
37
src/com/wasteinformationserver/db/PostgreSQLConnector.java
Executable file
37
src/com/wasteinformationserver/db/PostgreSQLConnector.java
Executable file
@ -0,0 +1,37 @@
|
||||
package com.wasteinformationserver.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class PostgreSQLConnector extends Database {
|
||||
|
||||
static {
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver").newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public PostgreSQLConnector(String user, String password, String host, int port, String dbName) {
|
||||
super(user, password, host, port, dbName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
Connection con = null;
|
||||
|
||||
try {
|
||||
con = DriverManager.getConnection(
|
||||
"jdbc:postgresql://" + host + ":" + port + "/" + dbName,
|
||||
user,
|
||||
password);
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return con;
|
||||
}
|
||||
}
|
53
src/com/wasteinformationserver/db/jdcb.java
Normal file
53
src/com/wasteinformationserver/db/jdcb.java
Normal file
@ -0,0 +1,53 @@
|
||||
package com.wasteinformationserver.db;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.Database;
|
||||
import com.wasteinformationserver.db.MySQLConnector;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class jdcb {
|
||||
Connection conn;
|
||||
|
||||
public jdcb(String username, String password, String dbName) {
|
||||
Database db = new MySQLConnector(
|
||||
username,
|
||||
password,
|
||||
"192.168.65.15",
|
||||
3306,
|
||||
dbName);
|
||||
|
||||
try {
|
||||
conn = db.getConnection();
|
||||
} catch (SQLException e) {
|
||||
Log.error("no connection to Database! DB Server not started...?");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(String sql) {
|
||||
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
|
||||
return stmt.executeQuery();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int executeUpdate(String sql){
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
|
||||
return stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user