隨筆-39  評論-33  文章-0  trackbacks-0
          newxy新坐標(biāo)如何獲取數(shù)據(jù)庫連接
          ?
          一、通過數(shù)據(jù)源設(shè)置獲得數(shù)據(jù)庫連接
          • 1.運用系統(tǒng)中的數(shù)據(jù)源jndi名設(shè)為 jdbc/default;
          • 2.如果系統(tǒng)中已有數(shù)據(jù)源的jndi名不是 jdbc/default,假設(shè)為 jdbc/xxx,則在 src/下的newxy.properties文件中加上一條:
                ds.default=jdbc/xxx
              
          二、通過編程獲得數(shù)據(jù)庫連接

          用戶可以在自定義默認(rèn)DAO類中通過java代碼獲取數(shù)據(jù)庫連接,只需覆蓋超類net.newxy.dbm.BaseDAO中public Connection getConnection(String dsJndi) throws Exception 方法,或?qū)崿F(xiàn)抽象超類net.newxy.dbm.DBM中public Connection getConnection(String dsJndi) throws Exception 方法,例如:

          package common;
          
          import net.newxy.dbm.DBM;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.SQLException;
          
          public class DefaultDao extends DBM{
              public Connection getConnection(String dsJndi) throws Exception {
                  Connection cn=null;
                  try {
                      Class.forName("com.mysql.jdbc.Driver").newInstance();
                      cn = DriverManager.getConnection(
                                  "jdbc:mysql://localhost:3306/line_order?user=root&password=mysql");
                  } catch (ClassNotFoundException ex) {
                  } catch (IllegalAccessException ex) {
                  } catch (InstantiationException ex) {
                  } catch (SQLException ex1) {
                      throw new Exception(ex1.getMessage());
                  }
                  return cn;
              }
          }
          
          在public Connection getConnection(String dsJndi) throws Exception 方法中參數(shù)String dsJndi被忽略。
          在src/下的newxy.properties文件中加入:
               dao.default=common.DefaultDAO
           
          三、通過設(shè)置newxy.properties文件獲得數(shù)據(jù)庫連接 如果系統(tǒng)中沒有數(shù)據(jù)源,則在src/下的newxy.properties文件中加入如下幾行:
          driver=com.mysql.jdbc.Driver
          url=jdbc:mysql://localhost:3306/line_order?user=root&password=mysql
          user=root
          pass=mysql
          posted on 2006-08-20 23:57 newxy新坐標(biāo) 閱讀(822) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 嘉祥县| 琼结县| 普兰店市| 恩施市| 临邑县| 项城市| 深水埗区| 新泰市| 元江| 含山县| 保山市| 新河县| 胶南市| 长顺县| 昌图县| 三门县| 乐安县| 尖扎县| 磐石市| 文化| 澜沧| 石柱| 保山市| 达州市| 沅陵县| 义乌市| 海淀区| 宕昌县| 永定县| 曲周县| 格尔木市| 永兴县| 昔阳县| 冕宁县| 新沂市| 临洮县| 芮城县| 龙岩市| 阳江市| 商洛市| 基隆市|