worked on dbcontroller part 2

fixed a few bugs but still a lot to do
This commit is contained in:
Seil0 2016-12-18 20:11:50 +01:00
parent 77f37955ae
commit a8e2f589ab
10 changed files with 74 additions and 81 deletions

Binary file not shown.

View File

@ -1,12 +1,12 @@
/** /**
* DBController for Project HomeFlix * DBController for Project HomeFlix
* *
* TODO fav and defav * connection is in manual commit!
*/ */
package application; package application;
import java.sql.Connection; //für Datenbank import java.sql.Connection; //für Datenbank
import java.sql.DriverManager; //für Datenbank import java.sql.DriverManager; //für Datenbank
import java.sql.PreparedStatement; //für Datenbank import java.sql.PreparedStatement; //für Datenbank
import java.sql.ResultSet; //für Datenbank import java.sql.ResultSet; //für Datenbank
@ -22,59 +22,46 @@ import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
public class DBController { public class DBController {
public DBController(MainWindowController m){ public DBController(MainWindowController m) {
mainWindowController=m; mainWindowController = m;
} }
private MainWindowController mainWindowController; private MainWindowController mainWindowController;
private String DB_PATH = System.getProperty("user.home") + "\\Documents\\HomeFlix" + "\\" + "Homeflix.db"; // der Pfad der Datenbank-Datei private String DB_PATH = System.getProperty("user.home") + "\\Documents\\HomeFlix" + "\\" + "Homeflix.db"; // der Pfad der Datenbank-Datei
Connection connection = null; Connection connection = null;
public void main()
{
System.out.println("Hallo");
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:"+DB_PATH);
// Statement statement = connection.createStatement();
// statement.setQueryTimeout(30); // set timeout to 30 sec. TODO don't know wath to do with this
public void main() {
try {
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
// Statement statement = connection.createStatement();
// statement.setQueryTimeout(30); // set timeout to 30 sec. TODO don't know wath to do with this
connection.setAutoCommit(false); //Autocommit to false -> manual commit is active!
fuelleDatenbank(); fuelleDatenbank();
// ausgeben(); // ausgeben();
getFavStatus("House of Cards"); // getFavStatus("House of Cards");
favorisieren("House of Cards"); // favorisieren("House of Cards");
getFavStatus("House of Cards"); // getFavStatus("House of Cards");
defavorisieren("House of Cards"); // defavorisieren("House of Cards");
getFavStatus("House of Cards"); // getFavStatus("House of Cards");
} } catch (SQLException e) {
catch(SQLException e) // if the error message is "out of memory", it probably means no database file is found
{ System.err.println(e.getMessage());
// if the error message is "out of memory", }
// it probably means no database file is found // finally {
System.err.println(e.getMessage()); // try {
} // if (connection != null)
finally // connection.close();
{ // } catch (SQLException e) {
try // // connection close failed.
{ // System.err.println(e);
if(connection != null) // }
connection.close(); // }
} }
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
}
public void fuelleDatenbank() { public void fuelleDatenbank() {
@ -92,7 +79,6 @@ public class DBController {
PreparedStatement psS = connection.prepareStatement("insert into film_streaming values (?, ?, ?, ?, ?, ?, ?)"); // SQL Befehl PreparedStatement psS = connection.prepareStatement("insert into film_streaming values (?, ?, ?, ?, ?, ?, ?)"); // SQL Befehl
String[] entries = new File(mainWindowController.getPath()).list(); String[] entries = new File(mainWindowController.getPath()).list();
System.out.println("Size: "+entries.length);
for(int i=0;i!=entries.length;i++) // Geht alle Dateien im Verzeichniss durch for(int i=0;i!=entries.length;i++) // Geht alle Dateien im Verzeichniss durch
{ {
@ -104,9 +90,7 @@ public class DBController {
} }
System.out.println("Erstelle Einträge streaming"); System.out.println("Erstelle Einträge streaming \n");
System.out.println(mainWindowController.getStreamingPath());
if(mainWindowController.getStreamingPath().equals("")||mainWindowController.getStreamingPath().equals(null)){ if(mainWindowController.getStreamingPath().equals("")||mainWindowController.getStreamingPath().equals(null)){
System.out.println("Kein Pfad angegeben"); //falls der Pfad null oder "" ist System.out.println("Kein Pfad angegeben"); //falls der Pfad null oder "" ist
}else{ }else{
@ -131,16 +115,16 @@ public class DBController {
} }
} }
} }
connection.setAutoCommit(false); // connection.setAutoCommit(false);
ps.executeBatch(); // scheibt alle Einträge in die Datenbank ps.executeBatch(); // scheibt alle Einträge in die Datenbank
psS.executeBatch(); psS.executeBatch();
connection.setAutoCommit(true); connection.commit();
ps.close(); ps.close();
psS.close(); psS.close();
//connection.close(); //connection.close();
} catch (SQLException e) { } catch (SQLException ea) {
System.err.println("Konnte nicht ausgeführt werden"); System.err.println("Konnte nicht ausgeführt werden");
e.printStackTrace(); ea.printStackTrace();
} }
} }
@ -200,16 +184,18 @@ public class DBController {
try{ try{
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
stmt.executeUpdate("UPDATE film_local SET rating=0 WHERE titel='"+name+"';"); stmt.executeUpdate("UPDATE film_local SET rating=0 WHERE titel='"+name+"';");
connection.commit(); //TODO hier kommt es zu fehlern mit dem autocommit connection.commit();
}catch(SQLException e){ }catch(SQLException e){
try { System.out.println("Ups! an error occured!");
Statement stmtS = connection.createStatement(); e.printStackTrace();
stmtS.executeUpdate("UPDATE film_streaming SET rating=0 WHERE titel='"+name+"';"); }
connection.commit(); try {
} catch (SQLException e1) { Statement stmtS = connection.createStatement();
System.out.println("Ups! an error occured!"); stmtS.executeUpdate("UPDATE film_streaming SET rating=0 WHERE titel='"+name+"';");
e1.printStackTrace(); connection.commit();
} } catch (SQLException e1) {
System.out.println("Ups! an error occured!");
e1.printStackTrace();
} }
} }
//setzt die Favorisierung eines bestimmten Films //setzt die Favorisierung eines bestimmten Films
@ -218,16 +204,18 @@ public class DBController {
try{ try{
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
stmt.executeUpdate("UPDATE film_local SET rating=1 WHERE titel='"+name+"';"); stmt.executeUpdate("UPDATE film_local SET rating=1 WHERE titel='"+name+"';");
connection.commit(); //TODO hier kommt es zu fehlern mit dem autocommit connection.commit();
}catch(SQLException e){ }catch(SQLException e){
try { System.out.println("Ups! an error occured!");
Statement stmtS = connection.createStatement(); e.printStackTrace();
stmtS.executeUpdate("UPDATE film_streaming SET rating=1 WHERE titel='"+name+"';"); }
connection.commit(); try {
} catch (SQLException e1) { Statement stmtS = connection.createStatement();
System.out.println("Ups! an error occured!"); stmtS.executeUpdate("UPDATE film_streaming SET rating=1 WHERE titel='"+name+"';");
e1.printStackTrace(); connection.commit();
} } catch (SQLException e1) {
System.out.println("Ups! an error occured!");
e1.printStackTrace();
} }
} }

View File

@ -425,7 +425,12 @@ public class MainWindowController {
@FXML @FXML
private void debugBtnclicked(){ private void debugBtnclicked(){
dbController.main(); dbController.main(); //TODO das muss vor einen anderen Befehl warum auch immer
dbController.getFavStatus("House of Cards");
dbController.favorisieren("House of Cards");
dbController.getFavStatus("House of Cards");
// dbController.defavorisieren("House of Cards");
// dbController.getFavStatus("House of Cards");
//for testing //for testing
} }
@ -514,7 +519,7 @@ public class MainWindowController {
System.out.println("Mode: "+mode); System.out.println("Mode: "+mode);
// dbController.ausgeben(); // dbController.main();
debugBtn.setDisable(false); //debugging btn for tests debugBtn.setDisable(false); //debugging btn for tests
debugBtn.setVisible(true); debugBtn.setVisible(true);

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 261 B