}
System.out.println();
while(rs.next()){
for(int i=0;i
}
System.out.println();
}
// close the opened resources
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
// perform a clean database closing
Derby.shutdownDatabase("ch20");
Derby.shutdownAll();
}
}
}
[java]
package com.han;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
/**
* The Embedded mode is limited by that we can't run simultaneously
* two programs (two JVM instances) using a same database (databaseName is the same).
*
* But we can instead use the NetworkServer mode to avoid this case,
* it is to say the "Client/Server" mode.
* In this mode, you have to first start the NetworkServer by this command :
*
* java org.apache.derby.drda.NetworkServerControl start [-h hostIP -p portNumber]
*
* Or use the API :
*
* NetworkServerControl serverControl = new NetworkServerControl(InetAddress.getByName("myhost"),1621);
* serverControl.shutdown();
*
* So in MySQL, create database==create schema,
* but create database is not applicable to Java Derby.
*
* In Derby, schema is also equivalent to a user name.
* @author HAN
*
*/
public class Derby {
private static Connection con=null;
private static String port=null;
private static String ip=null;
/**
* The port will be set to default: 1527
*/
public static void setPortToDefault(){
port="1527";
}
public static void setPort(String port){
Derby.port=port;
}
public static void setServer(String ip){
Derby.ip=ip;
}
/**
* This express loading driver is not necessary for Java 6 and later, JDBC 4.0 and later.
* Because it can be added automatically by DriverManager when connecting to a database.
*/
public static void loadDriver(){
//load the driver
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, or else an embedded database will be automatically used");
}
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
System.out.println("The embedded driver is successfully loaded");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
System.out.println("The client driver is successfully loaded");