隨筆-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) 閱讀(821) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 千阳县| 齐河县| 罗江县| 日照市| 重庆市| 盘锦市| 茂名市| 武邑县| 清新县| 望都县| 赣榆县| 安吉县| 宽城| 宜黄县| 铜鼓县| 永宁县| 精河县| 宜兰县| 拜城县| 定结县| 乌兰县| 清镇市| 隆回县| 武义县| 米泉市| 秭归县| 嘉善县| 扎囊县| 巩义市| 大兴区| 都匀市| 本溪市| 碌曲县| 瑞昌市| 庆阳市| 张家港市| 三台县| 丘北县| 中西区| 乾安县| 南城县|