隨筆 - 40, 文章 - 0, 評論 - 20, 引用 - 0
          數據加載中……

          通過weblogic的數據源獲得數據庫連接的方法

          通過weblogic的數據源獲得數據庫連接的方法:

          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("查詢到空數據源!");
                }
                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 月亮 閱讀(1241) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 黔南| 昌黎县| 屯留县| 前郭尔| 武陟县| 龙江县| 东源县| 大姚县| 饶阳县| 锡林郭勒盟| 柳河县| 齐齐哈尔市| 三明市| 泰兴市| 安陆市| 富锦市| 陵川县| 水城县| 新野县| 长阳| 西乌珠穆沁旗| 吐鲁番市| 永靖县| 韶山市| 桃园市| 海宁市| 贺州市| 镇雄县| 维西| 西丰县| 福安市| 富源县| 广西| 三台县| 南充市| 横峰县| 渑池县| 岳阳市| 淮阳县| 锡林郭勒盟| 大同县|