用了 Derby 數(shù)據(jù)庫好一段時間了,雁過留痕。
作為內(nèi)存數(shù)據(jù)庫或嵌入式數(shù)據(jù)庫,Derby 是一個很好的選擇,完全由 Java 實現(xiàn),在 JDK1.6 中 Derby 已經(jīng)作為其一部分來發(fā)布了,稱之為 JavaDB。
Derby 數(shù)據(jù)庫通過 derby.properties 文件來配置,這個文件需要放在程序的 classpath 或者 jdk 的 javadb classpath 路徑中。下面是一些配置的例子:
---------------------------- Derby derby.properties information ----------------------------
--### Derby System Home Directory
derby.system.home=D:/workspace/Boulevard/BoulevardDB
--### Derby Authentication Configuration and Turn On
derby.connection.requireAuthentication=true
derby.authentication.provider=BUILTIN
derby.database.propertiesOnly=true
--### User/Password Definition (System Level)
derby.user.alfred=alfred
--### User Authorization Definition (System Level)
derby.database.defaultAccessMode=fullAccess
derby.fullAccessUsers=cube
--### Some Others Configuration
derby.infolog.append=true
derby.storage.pageSize=4096
derby.storage.pageReservedSpace=60
derby.locks.deadlockTimeout=20
derby.locks.waitTimeout=30
---------------------------- Derby derby.properties information ----------------------------
Derby 數(shù)據(jù)庫可以啟動為兩種不同的模式:嵌入式模式 或 服務(wù)器模式
1. 嵌入式模式(embedded):當(dāng) Java 應(yīng)用程序第一次嘗試連接數(shù)據(jù)庫時啟動數(shù)據(jù)庫實例,當(dāng)程序進(jìn)程結(jié)束時數(shù)據(jù)庫實例也被關(guān)閉。當(dāng)數(shù)據(jù)庫實例啟動后,僅僅啟動它的進(jìn)程可以訪問,其它任何外部進(jìn)程(運行在當(dāng)前進(jìn)程的JVM之外的程序)都不能訪問。
下面的命令用于啟動并連接一個嵌入式數(shù)據(jù)庫實例:
------------------------------- Derby connection information -------------------------------
-- ### Use ij command to connect embeded database; if not exist, create it.
CONNECT 'jdbc:derby:data;create=true;user=alfred;password=alfred' AS CUBE;
-- ### Use ij command to connect embeded database; if not exist, don't create it, throw out error.
CONNECT 'jdbc:derby:data;user=alfred;password=alfred' AS CUBE;
------------------------------- Derby connection information -------------------------------
2. 服務(wù)器模式(network server):數(shù)據(jù)庫實例只能由命令行啟動:"...\javadb\bin\startNetworkServer.bat",該實例始終有效,直到通過命令行停止:"...\javadb\bin\stopNetworkServer.bat"。任何運行在本地或遠(yuǎn)程的JVM進(jìn)程都可以訪問,不會隨著訪問它的程序的結(jié)束而關(guān)閉。
下面的命令用于連接(不能啟動)服務(wù)器數(shù)據(jù)庫實例:
------------------------------- Derby connection information -------------------------------
-- ### Use ij command to connect network server database, reading default repository;
-- ### If not exist, create it.
CONNECT 'jdbc:derby://localhost:1527/data;create=true;user=alfred;password=alfred' AS BOULEVARD;
-- ### Use ij command to connect network server database; reading default repository;
-- ### If not exist, don't create it, throw out error.
CONNECT 'jdbc:derby://localhost:1527/data;user=alfred;password=alfred' AS BOULEVARD;
-- ### Use ij command to connect network server database, reading specific repository;
-- ### If not exist, create it.
CONNECT 'jdbc:derby://localhost:1527/D:/workspace/Boulevard/BoulevardDB/data;create=true;user=alfred;password=alfred' AS BOULEVARD;
-- ### Use ij command to connect network server database, reading specific repository;
-- ### If not exist, don't create it, throw out error.
CONNECT 'jdbc:derby://localhost:1527/D:/workspace/Boulevard/BoulevardDB/data;user=alfred;password=alfred' AS BOULEVARD;
------------------------------- Derby connection information -------------------------------
