} catch (SQLException se) {
if ( se.getSQLState().equals("08006") ) {
gotSQLExc = true;
}
}
if (!gotSQLExc) {
System.out.println("Database did not shut down normally");
} else {
System.out.println("Database: "+databaseName+" shut down normally");
}
}
}
/**
* shut down all opened databases and close the Derby engine.
* The effect is that after the execution of this method, we will not permitted to use Derby again in the rest of our program.
* Or else, an exception of "can't find a suitable driver for [a database URL]" will be thrown.
* However, you can still use another approach to resolve this problem: newInstance()
* For example,
*
* Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
*
*/
public static void shutdownAll(){
boolean gotSQLExc = false;
try {
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException se) {
// TODO Auto-generated catch block
if ( se.getSQLState().equals("XJ015") ) {
gotSQLExc = true;
}
}
if (!gotSQLExc) {
System.out.println("Database did not shut down normally");
} else {
}
}
/**
* Just connect to a database desired by providing the appropriate parameters.
* @param databaseName
* @param user
* @param password
* @return
* @throws SQLException
*/
public static Connection getConnection(String databaseName, String user, String password) throws SQLException{
if(port==null){
if(ip!=null){
System.out.println("You seem to have set an ip address, if so, you have also to assign a port before loading the driver, or else an embedded database is automatically used");
}
con=DriverManager.getConnection("jdbc:derby:"+databaseName,user,password);
System.out.println("Connection is sucessfully established, it uses an Embedded database");
}else if(ip==null){
con=DriverManager.getConnection("jdbc:derby://localhost:"+port+"/"+databaseName,user,password);
System.out.println("Connection is sucessfully established, it uses an network database but stored in the local host via the port: "+port);
}else{
con=DriverManager.getConnection("jdbc:derby://"+ip+":"+port+"/"+databaseName,user,password);
System.out.println("Connection is sucessfully established, it uses an network database whose host ip is: "+ip+" and via the port: "+port);
}
return con;
}
public static HashSet
DatabaseMetaData meta = con.getMetaData();
ResultSet res = meta.getTables(null, null, null, new String[]{"TABLE"});
HashSet
while (res.next()) {
set.add(res.getString("TABLE_NAME"));
/