Derby現(xiàn)在是apache的一個(gè)項(xiàng)目(http://db.apache.org/derby/index.html)。
寫了一段簡單的代碼。以后在項(xiàng)目用到Derby的時(shí)候可以查一下。
????public
static
void main(String[] args) {
????????try {
????????????// Class.forName("com.ibm.db2.jcc.DB2Driver");
????????????// String url =
????????????// "jdbc:derby:net://localhost:1527/address:user=libo;password=iamlibo;create=true:retrieveMessagesFromServerOnGetMessage=true;";
????????????Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
????????????System.out.println("load driver success");
????????????String url = "jdbc:derby:/ddd;create=true;upgrade=true;";
????????????Connection con = DriverManager.getConnection(url);
????????????System.out.println("con is " + con);
????????????Statement stmt = con.createStatement();
????????????String sql ="SELECT * FROM app.groups";
????????????System.out.println(sql);
????????????ResultSet rs = stmt.executeQuery(sql);
????????????while (rs.next()) {
????????????????System.out.print(rs.getInt(1) + ", ");
????????????????System.out.println(rs.getString(2));
????????????}
????????????rs.close();
????????????stmt.close();
????????} catch (ClassNotFoundException e) {
????????????// TODO Auto-generated catch block
????????????e.printStackTrace();
????????} catch (SQLException e) {
????????????// TODO Auto-generated catch block
????????????e.printStackTrace();
????????}
????}
這段代碼很簡單,只是建立一個(gè)連接,如果有表的就可以查詢出來數(shù)據(jù),但這里沒有建立表。
參考了下面一篇:http://www.java3z.com/cwbwebhome/article/article2/1101.html?id=1152
這個(gè)例子是一個(gè)最小限度的JDBC 應(yīng)用程序。 關(guān)于這個(gè)程序:
- 以內(nèi)嵌式模式(缺省的)或作為一個(gè)服務(wù)器環(huán)境中的客戶端運(yùn)行,這依賴于傳遞給程序的參數(shù)
- 如果運(yùn)行在內(nèi)嵌式模式,則啟動(dòng)Derby 引擎
- 如果運(yùn)行在客戶端模式,則連接到 Derby 網(wǎng)絡(luò)服務(wù)器
- 創(chuàng)建并連接到數(shù)據(jù)庫
- 創(chuàng)建一個(gè)表
- 插入數(shù)據(jù)
- 更新數(shù)據(jù)
- 查詢數(shù)據(jù)
- 刪除表
- 關(guān)閉連接
- 如果運(yùn)行在內(nèi)嵌式模式,則關(guān)閉 Derby。
以下是源碼:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/*
* @author janet
*/
public class SimpleApp
{
/* 缺省的模式是內(nèi)嵌式的*/
public String framework = "embedded";
public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
public String protocol = "jdbc:derby:";
public static void main(String[] args)
{
new SimpleApp().go(args);
}
void go(String[] args)
{
/* 處理參數(shù),確定這個(gè)程序作為內(nèi)嵌式使用還是作為客戶端使用*/
parseArguments(args);
System.out.println("SimpleApp starting in " + framework + " mode.");
try
{
/*
裝載驅(qū)動(dòng)程序,如果是內(nèi)嵌式模式,這將啟動(dòng)Derby, 因?yàn)樗€沒有運(yùn)行.
*/
Class.forName(driver).newInstance();
System.out.println("Loaded the appropriate driver.");
Connection conn = null;
Properties props = new Properties();
props.put("user", "user1");
props.put("password", "user1");
//create=true將創(chuàng)建數(shù)據(jù)庫derbyDB
conn = DriverManager.getConnection(protocol +"derbyDB;create=true", props);
System.out.println("Connected to and created database derbyDB");
conn.setAutoCommit(false);//設(shè)置自動(dòng)提交模式
Statement s = conn.createStatement();
/*
創(chuàng)建一個(gè)表,加入幾條記錄并更新一條.
*/
s.execute("create table derbyDB(num int, addr varchar(40))");
System.out.println("Created table derbyDB");
s.execute("insert into derbyDB values (1956,'Webster St.')");
System.out.println("Inserted 1956 Webster");
s.execute("insert into derbyDB values (1910,'Union St.')");
System.out.println("Inserted 1910 Union");
s.execute(
"update derbyDB set num=180, addr='Grand Ave.' where num=1956");
System.out.println("Updated 1956 Webster to 180 Grand");
s.execute(
"update derbyDB set num=300, addr='Lakeshore Ave.' where num=180");
System.out.println("Updated 180 Grand to 300 Lakeshore");
/*
查詢并校驗(yàn)結(jié)果.
*/
ResultSet rs = s.executeQuery(
"SELECT num, addr FROM derbyDB ORDER BY num");
if (!rs.next())
{
throw new Exception("Wrong number of rows");
}
if (rs.getInt(1) != 300)
{
throw new Exception("Wrong row returned");
}
if (!rs.next())
{
throw new Exception("Wrong number of rows");
}
if (rs.getInt(1) != 1910)
{
throw new Exception("Wrong row returned");
}
if (rs.next())
{
throw new Exception("Wrong number of rows");
}
System.out.println("Verified the rows");
s.execute("drop table derbyDB");//刪除表
System.out.println("Dropped table derbyDB");
?
rs.close();
s.close();
System.out.println("Closed result set and statement");
conn.commit();
conn.close();
System.out.println("Committed transaction and closed connection");
?
boolean gotSQLExc = false;
if (framework.equals("embedded"))
{
try
{
DriverManager.getConnection("jdbc:derby:;shutdown=true");//關(guān)閉數(shù)據(jù)庫服務(wù)
}
catch (SQLException se)
{
gotSQLExc = true;
}
if (!gotSQLExc)
{
System.out.println("Database did not shut down normally");
}
else
{
System.out.println("Database shut down normally");
}
}
}
catch (Throwable e)
{
System.out.println("exception thrown:");
if (e instanceof SQLException)
{
printSQLError((SQLException) e);
}
else
{
e.printStackTrace();
}
}
System.out.println("SimpleApp finished");
}
static void printSQLError(SQLException e)
{
while (e != null)
{
System.out.println(e.toString());
e = e.getNextException();
}
}
private void parseArguments(String[] args)
{
// System.setProperty("derby.system.home", "c:\\DBdata");//這樣可以設(shè)置數(shù)據(jù)庫數(shù)據(jù)的存放目錄
int length = args.length;
for (int index = 0; index < length; index++)
{
if (args[index].equalsIgnoreCase("jccjdbcclient"))
{
framework = "jccjdbc";
driver = "com.ibm.db2.jcc.DB2Driver";
protocol = "jdbc:derby:net://localhost:1527/";
}
if (args[index].equalsIgnoreCase("derbyclient"))
{
framework = "derbyclient";
driver = "org.apache.derby.jdbc.ClientDriver";
protocol = "jdbc:derby://localhost:1527/";
}
}
}
}
下面是如何運(yùn)行這個(gè)程序:
一、怎樣在內(nèi)嵌式環(huán)境(集成到桌面應(yīng)用)中運(yùn)行這個(gè)例子
我的工作目錄是c:\java,先將derby.jar復(fù)制到c:\java\jar下。再寫一個(gè)批處理文件run.bat,內(nèi)容如下:
set CLASSPATH=c:\java\jar\derby.jar;%CLASSPATH%
打開windows XP的命令行窗口,轉(zhuǎn)入工作目錄。運(yùn)行:
C:\java>run.bat
C:\java>set CLASSPATH=c:\java\jar\derby.jar;
C:\java>javac SimpleApp.java
C:\java>java SimpleApp
SimpleApp starting in embedded mode.
Loaded the appropriate driver.
Connected to and created database derbyDB
Created table derbyDB
Inserted 1956 Webster
Inserted 1910 Union
Updated 1956 Webster to 180 Grand
Updated 180 Grand to 300 Lakeshore
Verified the rows
Dropped table derbyDB
Closed result set and statement
Committed transaction and closed connection
Database shut down normally
SimpleApp finished
程序運(yùn)行后將在當(dāng)前目錄下生成
- derbyDB (目錄)
這個(gè)目錄構(gòu)成了 derbyDB 數(shù)據(jù)庫目錄. 你不能修改這個(gè)目錄中的任何文件。
- derbyDB\log (目錄)
這個(gè)目錄保存了數(shù)據(jù)庫的事務(wù)日志.
- derbyDB\seg0 (目錄)
這個(gè)目錄保存了數(shù)據(jù)庫derbyDB的數(shù)據(jù)
- derbyDB\service.properties
一個(gè)內(nèi)部文件,保存了一些配置參數(shù)
- derby.LOG
日志文件
二、 怎樣在服務(wù)器環(huán)境中運(yùn)行這個(gè)例子
(1)啟動(dòng)Derby Network Server
將derbynet.jar復(fù)制到c:\java\jar中,run.bat文件改為:
set CLASSPATH=c:\java\jar\derby.jar;c:\java\jar\derbynet.jar;%CLASSPATH%
新開一個(gè)DOS命令行窗口,在這個(gè)窗口中啟動(dòng) Derby Network Server,如:
C:\java>run.bat
C:\java>set CLASSPATH=c:\java\jar\derby.jar;c:\java\jar\derbynet.jar;
C:\java>java org.apache.derby.drda.NetworkServerControl start
服務(wù)器準(zhǔn)備在端口 1527 上接受連接。
(2)用 Derby 客戶端模式運(yùn)行這個(gè)程序:
將 derbyclient.jar 復(fù)制到c:\java\jar中,run.bat文件改為:
set CLASSPATH=c:\java\jar\derbyclient.jar;%CLASSPATH%
新開一個(gè)DOS命令行窗口,然后以Derby 客戶端模式啟動(dòng)SimpleApp
C:\java>run.bat
C:\java>set CLASSPATH=c:\java\jar\derbyclient.jar;
C:\java>java SimpleApp derbyclient
SimpleApp starting in derbyclient mode.
Loaded the appropriate driver.
Connected to and created database derbyDB
Created table derbyDB
Inserted 1956 Webster
Inserted 1910 Union
Updated 1956 Webster to 180 Grand
Updated 180 Grand to 300 Lakeshore
Verified the rows
Dropped table derbyDB
Closed result set and statement
Committed transaction and closed connection
SimpleApp finished
(3)使用 IBM DB2 JDBC Universal Driver運(yùn)行這個(gè)例子
??????在Derby的10.0版本, IBM DB2 JDBC Universal Driver 是Derby Network Server唯一的客戶端驅(qū)動(dòng).驅(qū)動(dòng)程序需要從 IBM的站點(diǎn)下載 (IBM DB2 JDBC Universal Driver, for Apache Derby Network Server). 從10.1開始, Derby網(wǎng)絡(luò)客戶端驅(qū)動(dòng)程序與Derby一起分發(fā),并且是被推薦的客戶端驅(qū)動(dòng)程序,但 DB2 Universal Driver還被支持并且可以象下面一樣被使用:
下載 db2jcc.jar和 db2jcc_license_c.jar,放到c:\java\jar目錄下,run.bat的內(nèi)容改為:
set CLASSPATH=c:\java\jar\db2jcc.jar;c:\java\jar\db2jcc_license_c.jar;%CLASSPATH%
C:\java>run.bat
C:\java>set CLASSPATH=c:\java\jar\db2jcc.jar;c:\java\jar\db2jcc_license_c.jarr;
c:\java>java SimpleApp jccjdbcclient
A successful run produces the following output:
SimpleApp starting in jccjdbc mode.
Loaded the appropriate driver.
Connected to and created database derbyDB
Created table derbyDB
Inserted 1956 Webster
Inserted 1910 Union
Updated 1956 Webster to 180 Grand
Updated 180 Grand to 300 Lakeshore
Verified the rows
Dropped table derbyDB
Closed result set and statement
Committed transaction and closed connection
SimpleApp finished
posted on 2007-04-07 15:28
Libo 閱讀(1011)
評(píng)論(0) 編輯 收藏