国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

簡單的JDBC應用程序for Java DB

2019-11-18 15:30:52
字體:
來源:轉載
供稿:網友

    今天下載了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



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 容城县| 钟山县| 西宁市| 潜江市| 卓尼县| 盱眙县| 霍城县| 二连浩特市| 恩施市| 青川县| 酒泉市| 泾源县| 平陆县| 阳泉市| 鄯善县| 安吉县| 怀柔区| 南华县| 红原县| 新民市| 梓潼县| 泗阳县| 绥江县| 集安市| 清远市| 和田县| 昌图县| 望都县| 车险| SHOW| 尚义县| 黎平县| 和平县| 苏尼特右旗| 灵璧县| 白银市| 高安市| 曲靖市| 旬邑县| 阜宁县| 日照市|