newxy新坐標如何獲取數據庫連接
?
一、通過數據源設置獲得數據庫連接
?
- 1.運用系統中的數據源jndi名設為 jdbc/default;
- 2.如果系統中已有數據源的jndi名不是 jdbc/default,假設為 jdbc/xxx,則在 src/下的newxy.properties文件中加上一條:
ds.default=jdbc/xxx
用戶可以在自定義默認DAO類中通過java代碼獲取數據庫連接,只需覆蓋超類net.newxy.dbm.BaseDAO中public Connection getConnection(String dsJndi) throws Exception 方法,或實現抽象超類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 方法中參數String dsJndi被忽略。
在src/下的newxy.properties文件中加入:
dao.default=common.DefaultDAO三、通過設置newxy.properties文件獲得數據庫連接 如果系統中沒有數據源,則在src/下的newxy.properties文件中加入如下幾行:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/line_order?user=root&password=mysql user=root pass=mysql