隨筆-39  評(píng)論-33  文章-0  trackbacks-0
          一、通過(guò)數(shù)據(jù)源設(shè)置獲得數(shù)據(jù)庫(kù)連接
          • 1.運(yùn)用系統(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
              
          二、通過(guò)編程獲得數(shù)據(jù)庫(kù)連接

          用戶可以在自定義默認(rèn)DAO類中通過(guò)java代碼獲取數(shù)據(jù)庫(kù)連接,只需覆蓋超類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
           
          三、通過(guò)設(shè)置newxy.properties文件獲得數(shù)據(jù)庫(kù)連接
          如果系統(tǒng)中沒(méi)有數(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ù)源



          下面是一個(gè)例子:



          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

          ##?說(shuō)明:
          ##?1:?common.GsglDAO是net.newxy.dbm.BaseDAO子類,別名是dao.gsgl,別名可在標(biāo)簽<nbean:formBean?name="..."?sql="..."?dao="dao.gsgl"/>中運(yùn)用
          ##?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
          因?yàn)闆](méi)有dao.default句,所以默認(rèn)DAO類是net.newxy.dbm.BaseDAO
          posted on 2006-08-31 10:40 newxy新坐標(biāo) 閱讀(759) 評(píng)論(1)  編輯  收藏

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 正阳县| 钦州市| 鄂伦春自治旗| 增城市| 长沙市| 剑河县| 文登市| 常熟市| 井陉县| 炉霍县| 吕梁市| 镇原县| 平谷区| 清远市| 彰化县| 湖州市| 郓城县| 浦北县| 梅州市| 清远市| 库车县| 开封市| 临安市| 池州市| 灌南县| 汽车| 伊通| 大渡口区| 岑溪市| 新建县| 佛学| 景德镇市| 鄂伦春自治旗| 新巴尔虎右旗| 游戏| 利川市| 曲沃县| 吉隆县| 霍林郭勒市| 军事| 穆棱市|