<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 23,comments - 66,trackbacks - 0

    學習使用Java DataBase (Derby) -- 嵌入式數據庫

    學習使用Java DataBase

    在前幾天,轉貼了java網站上一篇介紹使用Java DB的文章,http://blog.matrix.org.cn/page/icess?entry=using_java_db_in_desktop 里面介紹了如何在桌面程序中使用java DB,今天使用了一下感覺很不錯.

    但上面那篇文章中,是通過一個桌面程序來介紹java DB的,看的時候可能會感到比較混亂,今天我就按照他的例子,從中拿出 介紹java db的代碼, 來看看如何使用它吧!

    在看如何使用java db以前,先看看java db 是什么, 有什么特點.

    ?

    What's Java DB?

    要使用Java DB, 就不免想問起什么是java db, 其實java db就是在ibm 捐贈給Apache開源項目組的一個用java實現的輕量級嵌入式數據庫代碼基礎上發展起來的. sun Java DB主頁的描述如下:

    ?

    Java DB is Sun's supported distribution of the open source Apache Derby 100% Java technology database. It is fully transactional, secure, easy-to-use, standards-based -- SQL, JDBC API, and Java EE -- yet small, only 2MB. The Apache Derby project has a strong and growing community that includes developers from large companies such as Sun Microsystems and IBM as well as individual contributors.

    Apache Derby 項目主頁:http://db.apache.org/derby/

    Java DB主頁: http://developers.sun.com/prodtech/javadb/

    Java DB 的特性以及優點:

    Features & Benefits
    ?
    Sun Java DB is based on the open source Apache Derby project. It provides a full-featured, robust, small-footprint Java database management system that is cost effective and simple to deploy.

    ?
    Pure Java
    Java DB is written in Java to take advantage of Java's write once, run anywhere promise. Java DB supports J2SE, J2EE and J2ME standards.

    ?
    Standards- based
    Java DB technology adheres to database standards such as JDBC and ANSI SQL standards. This means Java DB provides the functionality expected of a sophisticated relational database, including SQL syntax, transaction management, concurrency, triggers, and backups. It also means that it is easy to upgrade an application using Java DB to other standards-based databases, such as Oracle and DB2.

    ?
    Easy to use
    Java DB is easy to use and requires zero-administration. This eliminates the need for a database administrator.

    ?
    Application-embedded or client-server mode
    The flexibility to support both embedded and client-server mode allows Java DB to adapt to diverse deployment scenarios. In embedded mode, Java DB runs on the same JVM as the application and users may not even be aware that they are accessing a relational database.

    ?
    Secure
    Java DB provides a numbers of security mechanisms including database file encryption, authentication through either external LDAP directory or built-in repository, and authorization. In addition, access to the database can also be controlled at read-only or read-write level.

    ?
    Sophisticated – with triggers and stored procedures
    Java DB allows users to define trigger to ensure referential integrity. It also supports Java stored procedures.

    ?
    Small foot-print
    With a foot-print of 2MB, Java DB is a perfect fit for desktop applications, mobile solutions and small devices that require a full featured, robust database.

    ?
    Cost-effective
    Java DB is available for free under the Apache license and requires no administration if embedded and little if used in client-server mode.

    ?
    Full support from Sun
    Sun offers full, 24 x 7 support for Java DB.

    可以看出Java DB的優點還是很多的.以后在sun 的軟件中都帶有Java DB了.

    下面來看看如何使用Java? DB吧.

    嵌入式數據庫和一般的數據庫不同,在使用以前,需要指定一個存放數據的目錄和指定數據庫的配置文件.

    配置文件可以很簡單:不用解釋了 如下:

    # Sample ResourceBundle properties file

    user=addressuser

    password=addressuser

    derby.driver=org.apache.derby.jdbc.EmbeddedDriver

    derby.url=jdbc:derby:

    db.table=ADDRESS

    db.schema=APP

    當然了 以上配置信息,也可以用一個Property 屬性對象來構造,然后在得到數據庫連接的時候把該對象傳遞進去也是可以的. 不過使用配置文件是為了方便修改相關配置信息而設定的.

    通過如下方法來設置 數據庫數據的存放目錄:

    private?void?setDBSystemDir()?{
    ????????//?decide?on?the?db?system?directory
    ????????//String?userHomeDir?=?System.getProperty("user.home",?".");
    ????????String?userHomeDir?=?System.getProperty("user.dir",?".");
    ????????String?systemDir?=?userHomeDir?+?"/.addressbook";
    ????????System.setProperty("derby.system.home",?systemDir);
    ????????
    ????????//?create?the?db?system?directory
    ????????File?fileSystemDir?=?new?File(systemDir);
    ????????fileSystemDir.mkdir();
    ?}

    然后就是加載數據庫驅動了, Java DB的數據庫驅動是集成在一起的,也是通過 Class.forName() 的方式加載的,內嵌的驅動名字是org.apache.derby.jdbc.EmbeddedDriver 本例子中使用下面的方法加載驅動.
    ?

    ????private?void?loadDatabaseDriver(String?driverName)?{
    ????????//?load?Derby?driver
    ????????try?{
    ????????????Class.forName(driverName);
    ????????}?catch?(ClassNotFoundException?ex)?{
    ????????????ex.printStackTrace();
    ????????}
    ????????
    ????}

    這樣,加載了驅動以后,只有得到Connection就可以操作數據了.

    在操作數據以前,我們先建立一個 數據表, 使用的sql 語句如下:

    ????private?static?final?String?strCreateAddressTable?=
    ????????????"create?table?APP.ADDRESS?("?+
    ????????????"????ID??????????INTEGER?NOT?NULL?PRIMARY?KEY?GENERATED?ALWAYS?AS?IDENTITY?(START?WITH?1,?INCREMENT?BY?1),"?+
    ????????????"????LASTNAME????VARCHAR(30),?"?+
    ????????????"????FIRSTNAME???VARCHAR(30),?"?+
    ????????????"????MIDDLENAME??VARCHAR(30),?"?+
    ????????????"????PHONE???????VARCHAR(20),?"?+
    ????????????"????EMAIL???????VARCHAR(30),?"?+
    ????????????"????ADDRESS1????VARCHAR(30),?"?+
    ????????????"????ADDRESS2????VARCHAR(30),?"?+
    ????????????"????CITY????????VARCHAR(30),?"?+
    ????????????"????STATE???????VARCHAR(30),?"?+
    ????????????"????POSTALCODE??VARCHAR(20),?"?+
    ????????????"????COUNTRY?????VARCHAR(30)?"?+
    ????????????")";

    然后使用如下方法來建立數據庫和數據表:

    ????private?boolean?createTables(Connection?dbConnection)?{
    ????????boolean?bCreatedTables?=?false;
    ????????Statement?statement?=?null;
    ????????try?{
    ????????????statement?=?dbConnection.createStatement();
    ????????????//?創建表格...
    ????????????statement.execute(strCreateAddressTable);
    ????????????bCreatedTables?=?true;
    ????????}?catch?(SQLException?ex)?{
    ????????????ex.printStackTrace();
    ????????}
    ????????
    ????????return?bCreatedTables;
    ????}
    ????private?boolean?createDatabase()?{
    ????????boolean?bCreated?=?false;
    ????????Connection?dbConnection?=?null;
    ????????
    ????????String?dbUrl?=?getDatabaseUrl();
    ????????dbProperties.put("create",?"true");
    ????????
    ????????try?{
    ????????????dbConnection?=?DriverManager.getConnection(dbUrl,?dbProperties);
    ????????????bCreated?=?createTables(dbConnection);
    ????????}?catch?(SQLException?ex)?{
    ????????}
    ????????dbProperties.remove("create");
    ????????return?bCreated;
    ????}

    由于是嵌入式數據庫,創建表格也要編程來實現,當然也有和IDE配合使用的插件可以使用.

    在創建數據庫和表時, 需要指定 一個create 屬性為true, 就象上面的那樣, 同樣,在斷開連接 關閉數據庫時,我們也要指定屬性 用編程的方法來關閉數據庫, 這是和普通數據庫不同的地方. 下面是關閉數據庫的代碼:

    ????public?void?disconnect()?{
    ????????if(isConnected)?{
    ????????????String?dbUrl?=?getDatabaseUrl();
    ????????????dbProperties.put("shutdown",?"true");
    ????????????try?{
    ????????????????DriverManager.getConnection(dbUrl,?dbProperties);
    ????????????}?catch?(SQLException?ex)?{
    ????????????}
    ????????????isConnected?=?false;
    ????????}
    ????}

    然后我們就可以通過得到Connection 象操作普通數據庫一樣來操作Java DB了. 如下:

    dbConnection = DriverManager.getConnection(dbUrl, dbProperties);
    stmtSaveNewRecord = dbConnection.prepareStatement(strSaveAddress, Statement.RETURN_GENERATED_KEYS);

    ?

    性能如何呢: 經過我的測試 插入 100 行(每行有11個字符串) 數據 要2100 多毫秒. 取出100 行需要 1500多毫秒 不知道大家認為如何.

    本文用到的全部代碼: 可以在此出下載:? http://icess.my.china.com/downloads/index.htm?

    posted on 2006-07-06 22:08 rd2pm 閱讀(1041) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    主站蜘蛛池模板: 亚洲激情在线观看| 久久精品国产精品亚洲色婷婷| 2020久久精品亚洲热综合一本| 久久国产精品免费专区| 亚洲综合精品一二三区在线| 久久精品视频免费| 亚洲a在线视频视频| 99免费视频观看| 亚洲hairy多毛pics大全| 妞干网免费视频在线观看| 午夜在线a亚洲v天堂网2019| 日韩成人在线免费视频| 亚洲精品久久久久无码AV片软件| 成年男女男精品免费视频网站| 亚洲国产精品网站在线播放| 亚洲精品456播放| 国产成人无码区免费网站| 久久久久亚洲av无码专区导航| 亚洲无砖砖区免费| 亚洲乱亚洲乱妇24p| 国产成人亚洲综合无码| 精品在线免费观看| 亚洲制服丝袜第一页| 全亚洲最新黄色特级网站| 二级毛片免费观看全程| 亚洲国产二区三区久久| 青娱乐免费视频在线观看| 国产亚洲精品免费视频播放| 永久免费av无码入口国语片| 亚洲国产精品综合一区在线| 精品少妇人妻AV免费久久洗澡| caoporm碰最新免费公开视频| 中国在线观看免费国语版| 亚洲AV电影天堂男人的天堂| 亚洲日韩乱码中文无码蜜桃臀网站| 99蜜桃在线观看免费视频网站| 亚洲五月综合缴情婷婷| 亚洲福利精品一区二区三区| 久久午夜羞羞影院免费观看| 亚洲a无码综合a国产av中文| 亚洲影院在线观看|