Commit 58f97409 authored by Stefan Michel's avatar Stefan Michel

es können jetzt beispiele unter verschiedenen tags angegeben werden, anbindung...

es können jetzt beispiele unter verschiedenen tags angegeben werden, anbindung an die GUI fehlt noch
parent 2f077ad8
......@@ -4,44 +4,53 @@ import java.sql.*;
import java.util.ArrayList;
import java.util.List;
// Datenbank Schema aus
// http://lekkerlogic.com/2016/02/site-tags-using-mysql-many-to-many-tags-schema-database-design/
/**
* Konkrete Implementierung der Datenbank-Anbindung
* <p>
* Datenbank Schema, und Queries aus
* http://lekkerlogic.com/2016/02/site-tags-using-mysql-many-to-many-tags-schema-database-design/
*/
public class DerbyDatabase {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName = "StefanHamsterDB";
String connectionURL = "jdbc:derby:" + dbName + ";create=true";
String createExampleTable = "CREATE TABLE Example (e_id INT NOT NULL, code VARCHAR(128), terrain VARCHAR(128))";
String createTagTable = "CREATE TABLE Tags (t_id INT NOT NULL , name VARCHAR (64))";
String createExampleTable = "CREATE TABLE Example (e_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), code VARCHAR(128), terrain VARCHAR(128))";
String createTagTable = "CREATE TABLE Tags (t_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR (64))";
String createExampleTagsTable = "CREATE TABLE ExampleTags (e_id INT NOT NULL , t_id INT NOT NULL)";
String example1 = "INSERT INTO Example VALUES (1, 'der code', 'das terrain')";
String tag1 = "INSERT INTO Tags VALUES (1, 'berg')";
String exampleTag1 = "INSERT INTO ExampleTags VALUES (1,1)";
String insertExample = "INSERT INTO Example(CODE, TERRAIN) VALUES (?, ?)";
String insertTag = "INSERT INTO Tags(NAME) VALUES (?)";
String insertExampleTag = "INSERT INTO ExampleTags VALUES (?,?)";
String queryExample = "SELECT DISTINCT e. e_id " +
String queryExampleByTag = "SELECT DISTINCT e. e_id " +
"FROM EXAMPLE e" +
" INNER JOIN EXAMPLETAGS et ON et.e_id = e.e_id" +
" INNER JOIN TAGS t ON t.t_id = et.t_id " +
"WHERE t.name IN (?)";
String queryExampleId = "SELECT E_ID FROM EXAMPLE WHERE CODE = ? AND TERRAIN = ?";
String queryTag = "SELECT T_ID FROM tags WHERE name = ?";
public static void main(String[] args) {
DerbyDatabase derbyDatabase = new DerbyDatabase();
derbyDatabase.setUpDB();
System.out.println(derbyDatabase.queryExamples("berg"));
boolean hase = derbyDatabase.updateExample("bullshit", "hase", "ski", "auto", "handy");
System.out.println(hase);
System.out.println("query result: " + derbyDatabase.queryExamples("ski"));
}
public void setUpDB() {
// https://stackoverflow.com/questions/16554545/create-table-if-not-exists-fails-with-table-already-exists
try {
System.out.println("Start");
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
createTables(stmt);
createTables(conn);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM EXAMPLE");
......@@ -64,26 +73,50 @@ public class DerbyDatabase {
/**
* erstellt die Tabellen in der Datenbank und fuellt diese mit initialen werten
* exceptions werden gehandlet, wenn zum beispiel die Tabelle bereits existiert
* <p>
* https://stackoverflow.com/questions/16554545/create-table-if-not-exists-fails-with-table-already-exists
*
* @param stmt
* @param conn
*/
private void createTables(Statement stmt) {
private void createTables(Connection conn) {
PreparedStatement preparedStatement;
try {
stmt.execute(createExampleTable);
stmt.executeUpdate(example1);
conn.prepareStatement(createExampleTable).execute();
preparedStatement = conn.prepareStatement(insertExample);
preparedStatement.setString(1, "der Code");
preparedStatement.setString(2, "das Terrain");
preparedStatement.execute();
preparedStatement = conn.prepareStatement(insertExample);
preparedStatement.setString(1, "anderer Code");
preparedStatement.setString(2, "anderes Terrain");
preparedStatement.execute();
} catch (SQLException e) {
System.err.println("example Table already exists");
}
try {
stmt.execute(createTagTable);
stmt.executeUpdate(tag1);
conn.prepareStatement(createTagTable).execute();
preparedStatement = conn.prepareStatement(insertTag);
preparedStatement.setString(1, "berg");
preparedStatement.execute();
} catch (SQLException e) {
System.err.println("tag Table already exists");
}
try {
stmt.execute(createExampleTagsTable);
stmt.executeUpdate(exampleTag1);
conn.prepareStatement(createExampleTagsTable).execute();
preparedStatement = conn.prepareStatement(insertExampleTag);
preparedStatement.setInt(1, 1);
preparedStatement.setInt(2, 1);
preparedStatement.execute();
preparedStatement = conn.prepareStatement(insertExampleTag);
preparedStatement.setInt(1, 2);
preparedStatement.setInt(2, 1);
preparedStatement.execute();
} catch (SQLException e) {
System.err.println("exampleTags Table already exists");
}
}
......@@ -111,13 +144,12 @@ public class DerbyDatabase {
* @return List von Strings
*/
public List<String> queryExamples(String tag) {
System.out.println("START QUERY");
List<String> exampleList = new ArrayList<>();
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectionURL);
PreparedStatement preparedStatement = conn.prepareStatement(queryExample);
PreparedStatement preparedStatement = conn.prepareStatement(queryExampleByTag);
preparedStatement.setString(1, tag);
ResultSet resultSet = preparedStatement.executeQuery();
......@@ -131,8 +163,74 @@ public class DerbyDatabase {
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ENDE QUERY");
return exampleList;
}
public boolean updateExample(String code, String terrain, String... tags) {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectionURL);
// check ob beispiel bereits in datenbank existiert
PreparedStatement preparedStatement = conn.prepareStatement(queryExampleId);
preparedStatement.setString(1, code);
preparedStatement.setString(2, terrain);
ResultSet resultSet = preparedStatement.executeQuery();
// wenn es bereits existiert wird false zurueckgeben und das speichern schlaegt fehl
if (resultSet.next()) {
return false;
}
// einfuegen des beispiels in tabelle example
preparedStatement = conn.prepareStatement(insertExample);
preparedStatement.setString(1, code);
preparedStatement.setString(2, terrain);
preparedStatement.executeUpdate();
// die eingefuegte Spalte als resultSet bekommen
preparedStatement = conn.prepareStatement(queryExampleId);
preparedStatement.setString(1, code);
preparedStatement.setString(2, terrain);
resultSet = preparedStatement.executeQuery();
resultSet.next();
int exampleId = resultSet.getInt(1);
for (String tag : tags) {
int tagId;
preparedStatement = conn.prepareStatement(queryTag);
preparedStatement.setString(1, tag);
resultSet = preparedStatement.executeQuery();
// wenn tag noch nicht existiert, wird er eingesetzt
if (!resultSet.next()) {
preparedStatement = conn.prepareStatement(insertTag);
preparedStatement.setString(1, tag);
preparedStatement.execute();
preparedStatement = conn.prepareStatement(queryTag);
preparedStatement.setString(1, tag);
resultSet = preparedStatement.executeQuery();
resultSet.next();
tagId = resultSet.getInt(1);
} else { // wenn tag bereits existiert
tagId = resultSet.getInt(1);
}
// relation bilden
preparedStatement = conn.prepareStatement(insertExampleTag);
preparedStatement.setInt(1, exampleId);
preparedStatement.setInt(2, tagId);
preparedStatement.execute();
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Markdown is supported
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