derby數據庫的一些總結


 本文主要是針對在osgi開發過程中的一些問題進行總結,其中dbcp數據源的配置是在SpringDM下配置的。
一,derby數據源的內嵌模式
       該模式的主要應用是嵌入式程序,因為其小巧,且不用開啟1527端口(derby數據庫默認的端口),就可直接使用程序與數據庫相連。其在SpringDM下的配置如下:
①絕對路徑數據庫配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" init-method="createDataSource" destroy-method="close"> 
              <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>    
              <property name="url" value="jdbc:derby:F:\\Java\\derby\\Database\\firstdb;create=true"/>   
       </bean>  
②相對數據庫路徑配置(相對於項目的總目錄)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  init-method="createDataSource" destroy-method="close"> 
              <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>    
              <property name="url" value="jdbc:derby:..\\darwin;create=true"/>   
       </bean> 
注意:1.當Eclipse運行以后,說明應用程序正在使用derby數據庫,那么是喲ij工具再次連接數據庫會報錯,因為一個derby數據庫只能被一個應用程序所占用。
2.如果寫一個db.bundle來連接數據庫,那么一定在其pom文件中添加  <DynamicImport-Package>*</DynamicImport-Package>,否則,會報錯:無法加載驅動類(沒有動態引入包,當然無法加載了!)
3. 如果應用程序結束了,那么要記得關閉數據庫。
      public static void  closeDatabase(Connection conn){
try {
if(conn!=null){
conn.close();
}
//提示:在關閉數據庫成功后,getConnection()方法將拋出異常來進行通知
DriverManager.getConnection("jdbc:derby:;shutdown=true");
System.out.println("數據庫已經關閉");
} catch (SQLException se) {
if (((se.getErrorCode() == 50000) && ("XJ015".equals(se
.getSQLState())))) {
// we got the expected exception
System.out.println("Derby shut down normally");
// Note that for single database shutdown, the expected
// SQL state is "08006", and the error code is 45000.
} else {
System.err.println("Derby did not shut down normally");
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM