用derby怎么不对
try {Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
.
.
.
catch (Throwable e) {
}
这直接就到catch里了,问题是出在加载驱动程序时候问题,不知道加载前需要怎么配置,我环境变量CLASSPATH中已将derby.jar
,derbytools.jar加进去了; --------------------编程问答-------------------- 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]
package com.han;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
import java.sql.Statement;
/**
* ROWID is the logical or physical address of a designated row in database.
* Different database providers have different supports for it.
* So before using it in SQL, it is always a good practice to check the support capacity
* of the database used.
* <p>
* The result is that Derby does not support ROWID, so it can not benefit the RowId class.
* <p>
* We have also done some tests of using the Derby. Found out that Derby is not bad and
* is competent enough to do a normal data exploring as others. Its advantages are free,
* small, embedded, and it is supported by many big companies like IBM, Oracle, Sun, and
* a lot of freedom developers. It has a not bad perspective.
*
* @author GAOWEN
*
*/
public class TestDerbyRowId {
static Connection con; // in fact, the object is null by default
static Statement s=null;
static ResultSet rs=null;
static ResultSetMetaData rsmd;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Derby.loadDriver();
try {
//System.out.println(con);
con=Derby.getConnection("dbName1","","");
con.setAutoCommit(false);// for that later con.commit(); is valid
DatabaseMetaData meta=con.getMetaData();
RowIdLifetime rowIdLifetime=meta.getRowIdLifetime();
System.out.println(rowIdLifetime);// To see if the Derby DBMS supports ROWID or not
Derby.listAllTables(con);
Derby.listAllSchemas(con);
Derby.listAllSchemasAndTables(con);
s=con.createStatement();
rs=s.executeQuery("select * from TABLERENAMED");
//ResultSetMetaData is useful to get information about the types and properties
//of the columns in a ResultSet object
//By the way, DatabaseMetaData is at a parallel level with ResultSetMetaData
rsmd=rs.getMetaData();// ResultSetMetaData has not the close() method
int numberOfColumns=rsmd.getColumnCount();
System.out.println(rsmd.isSearchable(1));
System.out.println(numberOfColumns);
System.out.println(rsmd.getTableName(3));// get the designated table name
for(int i=1;i<=numberOfColumns;i++){
System.out.println(rsmd.getColumnName(i)+" : "
+rsmd.getColumnTypeName(i)+"("
+rsmd.getPrecision(i)+")");
}
for(int i=1;i<=numberOfColumns;i++){
String columnName=rsmd.getColumnName(i);
if(columnName.length()>rsmd.getPrecision(i)){
columnName=rsmd.getColumnName(i).substring(0, rsmd.getPrecision(i));
}
System.out.printf("%-"+rsmd.getPrecision(i)+"s\t|",columnName);
}
System.out.println();
while(rs.next()){
for(int i=1;i<=numberOfColumns;i++){
System.out.printf("%-"+rsmd.getPrecision(i)+"s\t|",rs.getObject(i));
}
System.out.println();
}
//s.execute("alter table table1 add primary key(id)");
/*s.executeUpdate("insert into table1 values('016925', 'Ming XIAO', 50)");
s.addBatch("insert into table1 values('016945', 'Wang XIAO', 74)");
s.addBatch("insert into table1 values('016955', 'Zhang XIAO', 92)");
s.executeBatch();*/
//s.execute("drop table table1");
//s.execute("rename table table1 to tableRenamed");
// s.execute("rename column tableRenamed.id to identifiant");
//rs.close();
s.close();// It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources.
con.commit();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getErrorCode());
System.out.println(e.getSQLState());
System.out.println(e.getMessage());
e.printStackTrace();
}
Derby.shutdownDatabase("dbName1");
Derby.shutdownAll();
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
con=Derby.getConnection("dbName1","","");
s=con.createStatement();
rs=s.executeQuery("select * from TABLERENAMED");
//ResultSetMetaData is useful to get information about the types and properties
//of the columns in a ResultSet object
//By the way, DatabaseMetaData is at a parallel level with ResultSetMetaData
ResultSetMetaData rsmd=rs.getMetaData();// ResultSetMetaData has not the close() method
int numberOfColumns=rsmd.getColumnCount();
System.out.println(rsmd.isSearchable(1));
System.out.println(numberOfColumns);
System.out.println(rsmd.getTableName(3));// get the designated table name
for(int i=1;i<=numberOfColumns;i++){
System.out.println(rsmd.getColumnName(i)+" : "
+rsmd.getColumnTypeName(i)+"("
+rsmd.getPrecision(i)+")");
}
while(rs.next()){
for(int i=1;i<=numberOfColumns;i++){
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Derby.shutdownDatabase("dbName1");
Derby.shutdownAll();
}
}
package com.han;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JavaDBUse1 {
private static Connection con;
private static Statement s;
static void createTable(Statement s) throws SQLException{
s.execute("create table table1(" +
"id char(6) not null primary key," +
"name varchar(40)," +
"score int)");
s.execute("insert into table1 values('016954', 'San ZHANG', 86)");
s.execute("insert into table1 values('016934', 'Wu WANG', 45)");
s.execute("insert into table1 values('016908', 'Si LI', 97)");
}
public static void main(String[] args){
// Derby.setPort("1526");
// Derby.setServer("129.175.119.162");
/*NetworkServerControl serverControl = new NetworkServerControl(InetAddress.getByName("myhost"),1621);
serverControl.shutdown();*/
Derby.loadDriver();
try {
con=Derby.createDatabaseAndGetConnection("dbName1", "", "");
// con=Derby.getConnection("dbName3", "user2", "");
con.setAutoCommit(false);
s=con.createStatement();
if(Derby.isTableExists("table1", con)){
ResultSet rs=s.executeQuery("select * from table1 order by score");
System.out.println();
while(rs.next()){
StringBuilder sb=new StringBuilder(rs.getString("id"));
sb.append("\t");
sb.append(rs.getString("name"));
sb.append("\t");
sb.append(rs.getInt("score"));
System.out.println(sb.toString());
}
System.out.println();
rs.close();
s.close();
con.commit();
con.close();
}else{
createTable(s);
s.close();
con.commit();
con.close();
System.out.println("Table is created");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Derby.shutdownDatabase("dbName1");
Derby.shutdownAll();
}
}
补充:Java , Java SE