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