论坛首页 Java企业应用论坛

JBoss DB2 数据库连接

浏览 1390 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-11-12  
Jboss的连接上次调通了这次又折腾了一下午,发个贴做个笔记     
参照 $(JBOSS_HOME)\setup下的数据库连接创建如下文件
# <?xml version="1.0" encoding="UTF-8"?>
#
# <!-- ===================================================================== -->
# <!--                                                                       -->
# <!--  JBoss Server Configuration                                           -->
# <!--                                                                       -->
# <!-- ===================================================================== -->
#
# <!-- $Id: db2-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->
#
#
# <datasources>
#   <local-tx-datasource>
#     <jndi-name>DB2DS</jndi-name>
#     <connection-url>jdbc:db2:sample</connection-url>
#     <driver-class>COM.ibm.db2.jdbc.app.DB2Driver</driver-class>
#     <user-name>db2admin</user-name>
#     <password>db2admin</password>
#     <min-pool-size>0</min-pool-size>
#         <!-- sql to call when connection is created
#         <new-connection-sql>some arbitrary sql</new-connection-sql>
#         -->
#
#         <!-- sql to call on an existing pooled connection when it is obtained from pool 
#         <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
#         -->
#
#       <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
#       <metadata>
#          <type-mapping>DB2</type-mapping>
#       </metadata>
#   </local-tx-datasource>
#
# </datasources>

如果服务器已经启动需要重新启动,使得jndi绑定成功
    *写数据库连接的code 如下:
  
   1. package com.ibm.util;
   2.
   3. import java.sql.Connection;
   4. import java.sql.ResultSet;
   5. import java.sql.SQLException;
   6.
   7. import javax.naming.InitialContext;
   8. import javax.naming.NamingException;
   9. import javax.sql.DataSource;
  10.
  11. public class DatabaseOPs {
  12.
  13.     public static Connection getConnection(){
  14.         InitialContext cxt = null;
  15.         DataSource ds=null;
  16.         Connection conn=null;
  17.         try {
  18.             cxt = new InitialContext();
  19.             ds = (DataSource) cxt.lookup("java:DB2DS");
  20.             conn = ds.getConnection();
  21.         } catch (NamingException e) {
  22.             // TODO Auto-generated catch block
  23.             System.err.println("NamingException");
  24.             e.printStackTrace();
  25.         } catch (SQLException e) {
  26.             // TODO Auto-generated catch block
  27.             e.printStackTrace();
  28.         }
  29.         return conn;
  30.     }
  31.     
  32.     /**
  33.      * 
  34.      * @param rs
  35.      * @param conn
  36.      */
  37.     public static void closeAll(ResultSet rs,Connection conn){
  38.         try {
  39.             rs.close();
  40.             conn.close();
  41.         } catch (SQLException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.     }
  46. }

这样数据库就能够连接成功了,可以看出用来连接的是

<jndi-name>DB2DS</jndi-name>

标签中的部分,不要弄错哦。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics