今天下載了jdk1.6.0,以后要慢慢來學習1.6中的新特性和其中的一些經典實例。先看看關于java DB的這個最簡例子:
Simple JDBC application (源碼SimpleApp.java、文檔及derby.jar,derbynet.jar,derbyclient.ar文件請從jdk1.6.0中找)
以下是源碼:
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
|
新聞熱點
疑難解答