OSGi 系列(十六)之 JDBC Service
OSGi 系列(十六)之 JDBC Service
compendium 规范提供了 org.osgi.service.jdbc.DataSourceFactory 服务
1. 快速入门
1.1 环境准备
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.jdbc</groupId>
<artifactId>pax-jdbc</artifactId>
<version>0.8.0</version>
</dependency>
1.2 获取连接
import java.sql.*;
import java.util.Properties;
import javax.sql.DataSource;
import org.osgi.service.jdbc.DataSourceFactory;
public class JDBCServiceDemo {
private DataSourceFactory dataSourceFactory;
public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
this.dataSourceFactory = dataSourceFactory;
}
public void jdbc()throws Exception {
System.out.println(dataSourceFactory);
Properties props = new Properties();
props.put(DataSourceFactory.JDBC_URL, "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8");
props.put(DataSourceFactory.JDBC_USER, "root");
props.put(DataSourceFactory.JDBC_PASSWORD, "root");
DataSource ds = dataSourceFactory.createDataSource(props);
Connection conn = ds.getConnection();
//3、编写sql
String sql="select * from sys_user where id=?";
//4、创建语句执行者
PreparedStatement st = conn.prepareStatement(sql);
//5、设置参数
st.setString(1, "1");
//6、执行sql
ResultSet rs = st.executeQuery();
//7、处理结果
while(rs.next()){
System.out.println(rs.getString("id") + "=>" + rs.getString("username"));
}
//8、释放资源.
rs.close();
st.close();
System.out.println(conn);
System.out.println(conn.getCatalog());
conn.close();
}
}
1.3 OSGI-INF/blueprint 下配制 blueprint.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<reference id="dataSourceFactory" interface="org.osgi.service.jdbc.DataSourceFactory" filter="(osgi.jdbc.driver.class=com.mysql.jdbc.Driver)"/>
<bean class="com.edu.osgi.jdbc.JDBCServiceDemo" init-method="jdbc">
<property name="dataSourceFactory" ref="dataSourceFactory" />
</bean>
</blueprint>
1.4 karaf 测试
(1) 安装依赖
# 安装 mysql驱动
install -s mvn:mysql/mysql-connector-java/5.1.38
# 安装 osgi jdbc
install -s mvn:org.osgi/org.osgi.service.jdbc/1.0.0
# pax-jdbc 实现了 DataSourceFactory 服务
install -s mvn:org.ops4j.base/ops4j-base-spi/1.5.0
install -s mvn:org.ops4j.pax.jdbc/pax-jdbc/0.8.0
(2) 测试结果
安装 pax-jdbc 开启了 DataSourceFactory 服务
2. 补充说明
很多数据驱动已经实现了 DataSourceFactory 服务,如 postgresql
karaf 中安装 postgresql 的 DataSourceFactory 服务:
# osgi 规范
install -s mvn:org.osgi/org.osgi.service.jdbc/1.0.0
# 安装事务
feature:list | grep transaction
feature:install transaction
install -s mvn:org.postgresql/postgresql/9.4-1201-jdbc4
ls DataSourceFactory
可以看到 DataSourceFactory 服务已经开启。
posted on 2018-03-13 21:49 binarylei 阅读(1121) 评论(0) 编辑 收藏 举报