- 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
用戶可以在自定義默認(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