Commit e087dd86 authored by Stefan Michel's avatar Stefan Michel

DatenBank query jetzt moeglich

parent ca1e1cf0
package model;
public class DatabaseManager {
DerbyDatabase database = new DerbyDatabase();
}
package model;
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/
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 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 queryExample = "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 (?)";
public static void main(String[] args) {
DerbyDatabase derbyDatabase = new DerbyDatabase();
derbyDatabase.setUpDB();
System.out.println(derbyDatabase.queryExamples("berg"));
}
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);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM EXAMPLE");
printResultSet(resultSet);
resultSet = stmt.executeQuery("SELECT * FROM TAGS");
printResultSet(resultSet);
printResultSet(stmt.executeQuery("SELECT * FROM EXAMPLETAGS"));
stmt.close();
conn.close();
System.out.println("Ende");
} catch (Exception th) {
th.printStackTrace();
}
}
/**
* erstellt die Tabellen in der Datenbank und fuellt diese mit initialen werten
* exceptions werden gehandlet, wenn zum beispiel die Tabelle bereits existiert
*
* @param stmt
*/
private void createTables(Statement stmt) {
try {
stmt.execute(createExampleTable);
stmt.executeUpdate(example1);
} catch (SQLException e) {
System.err.println("example Table already exists");
}
try {
stmt.execute(createTagTable);
stmt.executeUpdate(tag1);
} catch (SQLException e) {
System.err.println("tag Table already exists");
}
try {
stmt.execute(createExampleTagsTable);
stmt.executeUpdate(exampleTag1);
} catch (SQLException e) {
System.err.println("exampleTags Table already exists");
}
}
// https://coderwall.com/p/609ppa/printing-the-result-of-resultset
private void printResultSet(ResultSet resultSet) throws SQLException {
ResultSetMetaData rsmd = resultSet.getMetaData();
System.out.println("querying SELECT * FROM EXAMPLE");
int columnsNumber = rsmd.getColumnCount();
while (resultSet.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = resultSet.getString(i);
System.out.print(columnValue + " " + rsmd.getColumnName(i));
}
System.out.println("");
}
}
/**
* gibt eine liste von Strings zurueck, welche die IDs der Beispiele beinhaltet,
* die unter diesem Tag gespeichert wurden
*
* @param tag: string
* @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.setString(1, tag);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
exampleList.add(resultSet.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ENDE QUERY");
return exampleList;
}
}
......@@ -182,8 +182,19 @@ public class MainViewCreationFX extends VBox implements Observer {
Menu territoriumMenu = buildTerritoriumMenu();
Menu hamsterMenu = buildHamsterMenu();
Menu simulationMenu = buildSimulationMenu();
Menu beispielMenu = buildBeispielMenu();
menuBar.getMenus().addAll(editorMenu, territoriumMenu, hamsterMenu, simulationMenu);
menuBar.getMenus().addAll(editorMenu, territoriumMenu, hamsterMenu, simulationMenu, beispielMenu);
}
private Menu buildBeispielMenu() {
//MenuItems werden mit Grafiken und Accelelatoren gebaut
MenuItem speichern = new MenuItem("_Speichern...");
MenuItem laden = new MenuItem("_Laden...");
return new Menu("_Beispiele", null, speichern, laden);
}
// In den naechsten Methoden werden die Menues gebaut
......
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