隨筆 - 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)站導航:
           
          主站蜘蛛池模板: 河津市| 庄河市| 波密县| 黄龙县| 商洛市| 泰兴市| 桃源县| 三亚市| 巴塘县| 龙州县| 双柏县| 昌宁县| 榆社县| 明光市| 长子县| 新绛县| 彭水| 什邡市| 荔浦县| 徐汇区| 磴口县| 英超| 吴忠市| 延庆县| 奉节县| 锦州市| 镇雄县| 五原县| 武邑县| 双峰县| 泰兴市| 宁远县| 南投县| 华宁县| 海门市| 庐江县| 绍兴县| 莱阳市| 莲花县| 邹城市| 宣城市|