DataBase connection & Configuration Class

This commit is contained in:
jeffcheasey88 2023-02-08 22:54:43 +01:00
parent 98533f91ea
commit a3b1c06455
5 changed files with 90 additions and 3 deletions

View file

@ -3,5 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="json-simple-1.1.1.jar"/>
<classpathentry exported="true" kind="lib" path="mysql-connector-java-8.0.28.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
.settings/
bin/
.project
.project
config.txt

Binary file not shown.

View file

@ -0,0 +1,76 @@
package be.jeffcheasey88.peeratcode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Field;
public class Configuration {
private String db_host;
private String db_port;
private String db_user;
private String db_database;
private String db_password;
private File _file;
public Configuration(String path){
this._file = new File(path);
System.out.println("Config on "+_file.getAbsolutePath());
}
public void load() throws Exception{
if(!this._file.exists()) return;
BufferedReader reader = new BufferedReader(new FileReader(this._file));
String line;
while((line = reader.readLine()) != null){
String[] split = line.split("=");
Field field = getClass().getDeclaredField(split[0]);
if(field == null) continue;
field.setAccessible(true);
field.set(this, split[1]);
}
reader.close();
}
public void save() throws Exception{
if(!_file.exists()){
File parent = _file.getParentFile();
if(!parent.exists()) parent.mkdirs();
_file.createNewFile();
}
Field[] fields = getClass().getDeclaredFields();
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
for(Field field : fields){
field.setAccessible(true);
if(field.getName().startsWith("_")) continue;
Object value = field.get(this);
writer.write(field.getName()+"="+value);
}
writer.flush();
writer.close();
}
public String getDbHost(){
return this.db_host;
}
public String getDbPort(){
return this.db_port;
}
public String getDbUser(){
return this.db_user;
}
public String getDbDatabase(){
return this.db_database;
}
public String getDbPassword(){
return this.db_password;
}
}

View file

@ -2,6 +2,8 @@ package be.jeffcheasey88.peeratcode;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -16,6 +18,12 @@ import be.jeffcheasey88.peeratcode.webserver.Router;
public class Main {
public static void main(String[] args) throws Exception {
Configuration config = new Configuration("config.txt");
config.load();
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://"+config.getDbHost()+":"+config.getDbPort()+"/"+config.getDbDatabase()+"",config.getDbUser(), config.getDbPassword());
Router router = new Router();
router.setDefault(new Response(){
@ -34,6 +42,7 @@ public class Main {
initRoutes(router);
ServerSocket server = new ServerSocket(80);
while(!server.isClosed()){
@ -41,8 +50,8 @@ public class Main {
Client client = new Client(socket, router);
client.start();
}
}
}
private static void initRoutes(Router router){
router.register(new PuzzleList());
}