Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Commit

Permalink
added configfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobin Rosenau committed Dec 14, 2017
1 parent d66054b commit e9f8ee2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
5 changes: 5 additions & 0 deletions config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config>
<port>6001</port>
<database-path>telefonbuch.db</database-path>
</config>
17 changes: 9 additions & 8 deletions src/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.sql.*;

public class Database {

public Database() throws FileNotFoundException, UnsupportedEncodingException {
File db = new File("telefonbuch.db");
String sqlPath;
public Database(String dbPath) throws FileNotFoundException, UnsupportedEncodingException {
File db = new File(dbPath);
if (!db.exists()) {
try {
db.createNewFile();
Expand All @@ -16,14 +16,15 @@ public Database() throws FileNotFoundException, UnsupportedEncodingException {
e1.printStackTrace();
}
}
sqlPath = "jdbc:sqlite:"+dbPath;
}

public void createTable() {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:telefonbuch.db");
c = DriverManager.getConnection(sqlPath);
System.out.println("Datenbank erfolgreich geöffnet");

stmt = c.createStatement();
Expand All @@ -48,7 +49,7 @@ public String readTable() {
try {
//Connect to the Database
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:telefonbuch.db");
c = DriverManager.getConnection(sqlPath);
c.setAutoCommit(false);
System.out.println("Datenbank erfolgreich geöffnet");
stmt = c.prepareStatement("SELECT * FROM Telefonbuch;");
Expand Down Expand Up @@ -116,7 +117,7 @@ public String deleteItem(int id) {
try {
//Connect to the Database
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:telefonbuch.db");
c = DriverManager.getConnection(sqlPath);
c.setAutoCommit(false);
System.out.println("Datenbank erfolgreich geöffnet");
//Run SQLInjection safe Delete Command
Expand Down Expand Up @@ -144,7 +145,7 @@ public String searchItem(String querry) {
try {
//Connect to the Database
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:telefonbuch.db");
c = DriverManager.getConnection(sqlPath);
c.setAutoCommit(false);
System.out.println("Datenbank erfolgreich geöffnet");
//Run SQLInsertion safe Search command
Expand Down Expand Up @@ -224,7 +225,7 @@ public String createKontakt(String vorname, String nachname, String strasse, Str
try {
//Connect to the Database
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:telefonbuch.db");
c = DriverManager.getConnection(sqlPath);
c.setAutoCommit(false);
System.out.println("Datenbank erfolgreich geöffnet");
//Create the Kontakt
Expand Down
32 changes: 28 additions & 4 deletions src/Server.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.json.*;

public class Server {

static ServerSocket anschluss;
public static void main(String[] args) throws IOException {

// Start ServerSocket on port 6000
int port = 6000;
String dbPath = "telefonbuch.db";
//Read config file
try {
File file = null;
file = new File("config.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
try {
port = Integer.parseInt(document.getElementsByTagName("port").item(0).getTextContent());
}catch(Exception e) {
System.out.println("Bitte Überprüfen sie ob in der Konfigurationsdatei eine Zahl als Port eingegeben ist.");
}
dbPath = document.getElementsByTagName("database-path").item(0).getTextContent();
}catch(Exception e) {
e.printStackTrace();
}

// Start ServerSocket
try {
anschluss = new ServerSocket(6000);
anschluss = new ServerSocket(port);
} catch (BindException e) {
e.printStackTrace();
}
Expand All @@ -32,7 +56,7 @@ public void run() {
}
}
});
Database dataBase = new Database();
Database dataBase = new Database(dbPath);
// Starts an endless loop for listening for Client Input
while (true) {
// Start listening
Expand Down

0 comments on commit e9f8ee2

Please sign in to comment.