程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 簡單的JDBC應用程序for Java DB

簡單的JDBC應用程序for Java DB

編輯:關於JAVA

今天下載了jdk1.6.0,以後要慢慢來學習1.6中的新特性和其中的一些經典實例。先看看關於Java DB的這個最簡例子:

Simple JDBC Application (源碼SimpleApp.Java、文檔及derby.jar,derbynet.jar,derbyclIEnt.ar文件請從jdk1.6.0中找)

這個例子是一個最小限度的JDBC 應用程序。 關於這個程序:

以內嵌式模式(缺省的)或作為一個服務器環境中的客戶端運行,這依賴於傳遞給程序的參數

如果運行在內嵌式模式,則啟動Derby 引擎

如果運行在客戶端模式,則連接到 Derby 網絡服務器

創建並連接到數據庫

創建一個表

插入數據

更新數據

查詢數據

刪除表

關閉連接

如果運行在內嵌式模式,則關閉 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

{

/* 缺省的模式是內嵌式的*/

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)

{

/* 處理參數,確定這個程序作為內嵌式使用還是作為客戶端使用*/

parseArguments(args);

System.out.println("SimpleApp starting in " + framework + " mode.");

try

{

/*

裝載驅動程序,如果是內嵌式模式,這將啟動Derby, 因為它還沒有運行.

*/

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將創建數據庫derbyDB

conn = DriverManager.getConnection(protocol +"derbyDB;create=true", props);

System.out.println("Connected to and created database derbyDB");

conn.setAutoCommit(false);//設置自動提交模式

Statement s = conn.createStatement();

/*

創建一個表,加入幾條記錄並更新一條.

*/

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");

/*

查詢並校驗結果.

*/

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");//關閉數據庫服務

}

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");//這樣可以設置數據庫數據的存放目錄

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/";

}

}

}

}

下面是如何運行這個程序:

一、怎樣在內嵌式環境(集成到桌面應用)中運行這個例子

我的工作目錄是c:\java,先將derby.jar復制到c:\Java\jar下。再寫一個批處理文件run.bat,內容如下:

set CLASSPATH=c:\Java\jar\derby.jar;%CLASSPATH%

打開Windows XP的命令行窗口,轉入工作目錄。運行:

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

程序運行後將在當前目錄下生成

derbyDB (目錄)

這個目錄構成了 derbyDB 數據庫目錄. 你不能修改這個目錄中的任何文件。

derbyDB\log (目錄)

這個目錄保存了數據庫的事務日志.

derbyDB\seg0 (目錄)

這個目錄保存了數據庫derbyDB的數據

derbyDB\service.propertIEs

一個內部文件,保存了一些配置參數

derby.LOG

日志文件

二、 怎樣在服務器環境中運行這個例子

(1)啟動Derby Network Server

將derbynet.jar復制到c:\Java\jar中,run.bat文件改為:

set CLASSPATH=c:\java\jar\derby.jar;c:\Java\jar\derbynet.jar;%CLASSPATH%

新開一個DOS命令行窗口,在這個窗口中啟動 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

服務器准備在端口 1527 上接受連接。

(2)用 Derby 客戶端模式運行這個程序:

將 derbyclIEnt.jar 復制到c:\Java\jar中,run.bat文件改為:

set CLASSPATH=c:\Java\jar\derbyclIEnt.jar;%CLASSPATH%

新開一個DOS命令行窗口,然後以Derby 客戶端模式啟動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運行這個例子

在Derby的10.0版本, IBM DB2 JDBC Universal Driver 是Derby Network Server唯一的客戶端驅動.驅動程序需要從 IBM的站點下載 (IBM DB2 JDBC Universal Driver, for apache Derby Network Server). 從10.1開始, Derby網絡客戶端驅動程序與Derby一起分發,並且是被推薦的客戶端驅動程序,但 DB2 Universal Driver還被支持並且可以象下面一樣被使用:

下載 db2jcc.jar和 db2jcc_license_c.jar,放到c:\Java\jar目錄下,run.bat的內容改為:

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved