本文主要是针对在osgi开发过程中的一些问题进行总结,其中dbcp数据源的配置是在SpringDM下配置的。
一,derby数据源的内嵌模式
       该模式的主要应用是嵌入式程序,因为其小巧,且不用开启1527端口(derby数据库默认的端口),就可直接使用程序与数据库相连。其在SpringDM下的配置如下:
①绝对路径数据库配置
<bean > 
              <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>    
              <property name="url" value="jdbc:derby:F:\\Java\\derby\\Database\\firstdb;create=true"/>   
       </bean>  
②相对数据库路径配置(相对于项目的总目录)
<bean > 
              <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");
}
}

 

相关文章:

  • 2021-05-14
  • 2022-01-23
  • 2022-12-23
  • 2021-06-27
  • 2021-10-29
  • 2021-05-17
猜你喜欢
  • 2021-11-26
  • 2022-01-12
  • 2021-10-19
  • 2022-01-19
  • 2022-12-23
  • 2021-10-02
相关资源
相似解决方案