隨筆 - 40, 文章 - 0, 評論 - 20, 引用 - 0
          數(shù)據(jù)加載中……

          通過weblogic的數(shù)據(jù)源獲得數(shù)據(jù)庫連接的方法

          通過weblogic的數(shù)據(jù)源獲得數(shù)據(jù)庫連接的方法:

          package com.moonsoft.datasource;

          import javax.naming.NamingException;
          import java.util.Hashtable;
          import javax.naming.InitialContext;
          import java.sql.Connection;
          import javax.sql.DataSource;
          import java.sql.SQLException;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;

          public class TestDataSource {

            public static String WEB_URL = "t3://localhost:9000";
            public static String DATA_SOURCE = "JDBCDS";
            public static String weblogic_context_factory =
                "weblogic.jndi.WLInitialContextFactory";
            public TestDataSource() {
            }
            public static Object lookUp() throws NamingException {
              Hashtable env = new Hashtable();
              env.put(InitialContext.INITIAL_CONTEXT_FACTORY, weblogic_context_factory);
              env.put(InitialContext.PROVIDER_URL, WEB_URL);
              InitialContext tempContext = new InitialContext(env);
              return tempContext.lookup(DATA_SOURCE);
            }
            public static Connection getConnection() throws SQLException {
              Connection conn = null;
              try {
                DataSource ds = (DataSource) lookUp();
                if (ds == null) {
                  throw new SQLException("查詢到空數(shù)據(jù)源!");
                }
                conn = ds.getConnection();
              }
              catch (NamingException ex) {
                ex.printStackTrace();
              }
              return conn;
            }
            public static void releaseConnection(Connection conn, PreparedStatement sta,
                                                 ResultSet rs) {
              try {
                if (rs != null) {
                  rs.close();
                }
                if (sta != null)
                  sta.close();
                if (conn != null)
                  conn.close();
              }
              catch (Exception ex) {
                ex.printStackTrace();
              }
            }
            public static void testSearch() {
              Connection conn = null;
              PreparedStatement sta = null;
              ResultSet rs = null;
              try {
                conn = getConnection();
                String sql = "select * from admin_config where config_name like ?";
                sta = conn.prepareStatement(sql);
                sta.setString(1,"%Sms%");
                rs = sta.executeQuery();
                if (rs != null) {
                  while (rs.next()) {
                    System.out.println(rs.getString(1));
                  }
                }
              }
              catch (Exception ex) {
                ex.printStackTrace();
              }
              finally {
                releaseConnection(conn,sta,rs);
              }
            }
            public static void main(String [] argv){
              testSearch();
            }
          }

          posted on 2006-01-05 10:51 月亮 閱讀(1245) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 名山县| 宣武区| 醴陵市| 武胜县| 巩留县| 定陶县| 龙井市| 金川县| 柏乡县| 芒康县| 莎车县| 星子县| 贵港市| 竹北市| 莲花县| 庆云县| 平阳县| 丰县| 包头市| 杭锦后旗| 蕲春县| 安化县| 宁国市| 麻城市| 瑞安市| 礼泉县| 万源市| 朝阳县| 哈密市| 缙云县| 玛沁县| 林州市| 金塔县| 西平县| 灯塔市| 东山县| 建始县| 平安县| 积石山| 玉环县| 罗田县|