用了 Derby 數據庫好一段時間了,雁過留痕。
作為內存數據庫或嵌入式數據庫,Derby 是一個很好的選擇,完全由 Java 實現,在 JDK1.6 中 Derby 已經作為其一部分來發布了,稱之為 JavaDB。
Derby 數據庫通過 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 數據庫可以啟動為兩種不同的模式:嵌入式模式 或 服務器模式
1. 嵌入式模式(embedded):當 Java 應用程序第一次嘗試連接數據庫時啟動數據庫實例,當程序進程結束時數據庫實例也被關閉。當數據庫實例啟動后,僅僅啟動它的進程可以訪問,其它任何外部進程(運行在當前進程的JVM之外的程序)都不能訪問。
下面的命令用于啟動并連接一個嵌入式數據庫實例:
------------------------------- 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. 服務器模式(network server):數據庫實例只能由命令行啟動:"...\javadb\bin\startNetworkServer.bat",該實例始終有效,直到通過命令行停止:"...\javadb\bin\stopNetworkServer.bat"。任何運行在本地或遠程的JVM進程都可以訪問,不會隨著訪問它的程序的結束而關閉。
下面的命令用于連接(不能啟動)服務器數據庫實例:
------------------------------- 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 -------------------------------