隨筆-39  評論-33  文章-0  trackbacks-0
          一、通過數(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

















          默認(rèn)DAO類使用默認(rèn)數(shù)據(jù)源,默認(rèn)DAO類是net.newxy.dbm.BaseDAO,但也可由newxy.properties文件指定,如:
          ??????dao.default=common.MyDAO?##指定common.MyDAO為默認(rèn)DAO類
          默認(rèn)數(shù)據(jù)源是?jdbc/default,默認(rèn)數(shù)據(jù)源也可由newxy.properties文件指定,如:
          ??????ds.default=jdbc/xxxx?##指定jdbc/xxx為默認(rèn)數(shù)據(jù)源



          下面是一個例子:



          dao.gsgl=common.GsglDAO???##?1
          dao.sczt=common.ScztDAO???##?2

          dao.common.GsglDAO.dsJndi=jdbc/gsgl???##?3
          dao.common.ScztDAO.dsJndi=jdbc/sczt???##?4

          encoding.ds.default=GBK???##?5
          encoding.ds.jdbc/sczt=GBK???##?6
          encoding.ds.jdbc/gsgl=GBK???##?7

          ##?說明:
          ##?1:?common.GsglDAO是net.newxy.dbm.BaseDAO子類,別名是dao.gsgl,別名可在標(biāo)簽<nbean:formBean?name="..."?sql="..."?dao="dao.gsgl"/>中運用
          ##?2:?common.ScztDAO是net.newxy.dbm.BaseDAO子類,別名是dao.sczt

          ##?3:?DAO類common.GsglDAO以jdbc/gsgl作數(shù)據(jù)源
          ##?4:?DAO類common.ScztDAO以jdbc/sczt作數(shù)據(jù)源

          ##?5:?默認(rèn)數(shù)據(jù)源字符編碼是GBK
          ##?6:?數(shù)據(jù)源jdbc/gsgl字符編碼是GBK
          ##?7:?數(shù)據(jù)源jdbc/sczt字符編碼是GBK
          因為沒有dao.default句,所以默認(rèn)DAO類是net.newxy.dbm.BaseDAO
          posted on 2006-08-31 10:40 newxy新坐標(biāo) 閱讀(762) 評論(1)  編輯  收藏

          評論:
          # re: newxy新坐標(biāo)如何獲取數(shù)據(jù)庫連接 2008-03-29 18:55 | nexy_user
          你好,我想問一下配置文件的名稱一定要是newxy.properties嗎?能否改動?  回復(fù)  更多評論
            

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 黄陵县| 黄浦区| 新泰市| 桐乡市| 涿州市| 醴陵市| 景东| 兖州市| 探索| 藁城市| 鄂托克前旗| 乌鲁木齐县| 万年县| 九龙县| 澄迈县| 广南县| 榕江县| 商水县| 庆云县| 大宁县| 志丹县| 罗江县| 莆田市| 城固县| 民权县| 罗城| 郸城县| 富蕴县| 章丘市| 绥中县| 兴业县| 衡水市| 肥城市| 凤山县| 依安县| 蒙城县| 太湖县| 胶州市| 闽清县| 休宁县| 包头市|