Java 使用 InitialContext 以及 JDBC 工具类库
今天在看一个小的项目时候, 第一次见到 InitialContex 这个类, 感觉好神奇, 他的一个方法 loookup() 似乎好牛逼, 于是网上查了查, 原来是与 JNDI 有关的一个类, 由 javax 提供, 详细的使用我也不是很明白, 没有使用过, 简单的使用还是能够明白的, 下面就依据我遇到的小实例分析:
1 首先配置数据源以及数据库驱动, 这个必须啊, 否则, 在牛逼的类也找不到你的数据源;
2 配置数据源: 在 tomcat 的 context 中进行配置;
3 然后, 在你的项目使用 InitialContext 这个类便可以了:
- Context initContext = new InitialContext();
- DataSource ds = initContext.lookup(dataSourceName);
4 这样你的数据源便被找到了, 你就可以直接使用 ds 了;
使用 InitialContext 加载的数据源之后, 你可以下载 Apache 组织提供的一个开源 JDBC 工具类库; 这个类库的方法, 看上去使用和 Hibernante 多少有些类似, 悲哀的是这个依然不是面向对象的操作数据库, 下面介绍这个类库:
1QueryRunner qr = new QueryRunner(ds);
2 得到 QueryRunner 之后, 便可以调用这个类库中的方法了, 依照参数传值就可以;
一个详细的利用 InitialContext 以及 JDBC 工具类库的代码如下:
前提是你数据源已经配置, 有相关数据库的驱动, 你可以将这个代码作为工具来;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- package com.cjg.tool;
- import
- java.sql.SQLException;
- import
- javax.naming.Context;
- import
- javax.naming.InitialContext;
- import
- javax.naming.NamingException;
- import
- javax.sql.DataSource;
- import
- org.apache.commons.dbutils.QueryRunner;
- import
- org.apache.commons.dbutils.ResultSetHandler;
- public class OracleTool {
- private
- String dataSourceName;
- private DataSource ds;
- public
- OracleTool(String dataSourceName) {
- this
- .dataSourceName = dataSourceName;
- }
- public
- OracleTool() {
- }
- public void
- setDataSourceName(String dataSourceName) {
- this
- .dataSourceName = dataSourceName;
- }
- public void init() {
- Context initContext;
- try {
- initContext =new InitialContext();
- ds = (DataSource) initContext.lookup(dataSourceName);
- }catch
- (NamingException e) {
- e.printStackTrace();
- }
- }
- public int
- update(String sql, String[] param) {
- int result =0;
- QueryRunner qr =new QueryRunner(ds);
- try {
- result = qr.update(sql, param);
- }catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- public
- Object query(String sql, String[] param, ResultSetHandler rsh) {
- QueryRunner qr =new QueryRunner(ds);
- Object result =null;
- try {
- result = qr.query(sql, param, rsh);
- }catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- }
在添加一些 tomcat 配置数据源的信息, 网摘: http://www.douban.com/note/78355269/
1 修改 Tomcat_Home/conf/server.xml, 在 GlobalNamingResources 中加入:
- <Resource name="jdbc/DataSource" auth="Container"
- type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
- url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
- username="user" password="password" maxActive="20" maxIdle="10"
- maxWait="-1"/>
2 再在 Context 中加入引用:
<ResourceLink name="jdbc/DataSource" global="jdbc/DataSource" type="javax.sql.DataSource"/>
如果不加, 则在使用的时候会出现: Cannot create JDBC driver of class ''for connect URL'null' 错误
3 在 web.xml 定义:
- <resource-ref>
- <description>popuserDataSource</description>
- <res-ref-name>jdbc/DataSource</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
4 在 Spring 中引用:
- <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName" value="java:comp/env/jdbc/DataSource"/>
- <property name="expectedType" value="javax.sql.DataSource"/>
- </bean>
来源: http://www.92to.com/bangong/2018/02-09/33301589.html