Ciao a tutti! Oggi ho un quesito interessante da proporre: derby+ netBenas
Praticamente devo gestire una serie di dati ed è molto comodo per me usare un DB incorporato nel programma, fatto qualche ricerca e ho scoperto Derby, ho anche scoperto che net Benas lo implementa nativamente (EVVIVA!!)
Adesso.... ho creato le tabelle con net Beans con una connessione a "jdbc:derby://localhost:1527/Ship_simulator"
poi ho creato una classe che gestisce connessione e disconnessione al DB tramite i metodi
codice:
// VARIABILI GLOBALI PER GESTIRE ERRORE
public static String Errore = null;
public static boolean stato = true;
// VARIABILI gesione connessione
private static String dbURL = "Ship_simulator";
private static String user = "ship";
private static String pass = "simulator";
private static Connection conn = null;
private static Statement stmt = null;
//----- connessione al DB --------------
private static boolean connetti(){
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
} catch(Exception e) {
Errore = "Errore nel caricamento del DB: " + e.getMessage();
return false;
}
try {
conn = DriverManager.getConnection("jdbc:derby:"+dbURL+";create=true",user,pass);
}catch (Exception e){
Errore = "Errore di connessione: " + e.getMessage();
return false;
}
return true;
}
private static boolean shutdown(){
try{
if (stmt != null) stmt.close();
if (conn != null){
conn = DriverManager.getConnection("jdbc:derby:"+ dbURL + ";shutdown=true",user,pass);
conn.close();
}
} catch (SQLException e){
/* TODO Attenzione, stando a quanto detto nel forum il
* DB è disconnesso ma lo vede come errore
*/
e.printStackTrace();
return true;
}
return true;
}
Ed ho eseguito questa semplice classe
codice:
public static void prova(){
connetti();
try {
System.out.println("Schema: " + conn.getSchema());
PreparedStatement mio = conn.prepareStatement("SELECT ID FROM MARE");
mio.executeQuery();
ResultSet set=mio.getResultSet();
while(set.next()){
System.out.println(set.getString(1));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
shutdown();
}
ma l'output è questo
codice:
Schema: SHIP
java.sql.SQLSyntaxErrorException: lo schema 'SHIP' non esiste.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at wave.simulator.tabelle.DB.prova(DB.java:214)
at wave.simulator.WaveSimulator.main(WaveSimulator.java:20)
Caused by: java.sql.SQLException: lo schema 'SHIP' non esiste.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
invece se metto private static String dbURL = "//localhost:1527/Ship_simulator"; tutto va bene, così però il DB non risulta embedded.
Net Beans inoltre mi impedisce di creare una DB nella posizione (embdded) jdbc:derby:Ship_simulator
Aiuto! Devo risolvere questo problema altrimenti il programma (tutto) è da buttare!!