国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

用Java實現(xiàn)數(shù)據(jù)庫應用系統(tǒng)

2019-11-18 13:39:31
字體:
供稿:網(wǎng)友

  我們在做信息系統(tǒng)的時候,都要訪問數(shù)據(jù)庫,我最近接手一個項目,項目組決定使用java編寫,我負責數(shù)據(jù)層的設計和編碼,為了提高代碼的重用性和提高項目的開發(fā)效率。我們開發(fā)了一個通用的數(shù)據(jù)庫連接和完成基本操作的類庫,個人認為這個類在做MIS系統(tǒng)時還是有一定的價值,所以總結(jié)出來,介紹給大家。
  連接工廠,實現(xiàn)了DataSource接口

package skydev.modules.data;
import java.sql.*;
import javax.sql.DataSource;
import java.io.PRintWriter;
public class ConnectionFactory implements DataSource {
private String userName;
private String passWord;
private String driverName;
private String url;
private java.sql.Connection connection;

/**
* 根據(jù)設置的連接參數(shù)創(chuàng)建一個新的連接實例
* @return
*/
private Connection getNewConnection() {
try {
this.connection.close(); //試圖關(guān)閉連接
}
finally {
this.connection = null; //釋放連接
try {
Class.forName(this.driverName); //加載驅(qū)動程序
//DriverManager.registerDriver(driver);
try {
this.connection = DriverManager.getConnection(this.url, this.userName,
this.password);
}
catch (SQLException e) {
throw e;
}
}
finally {
return this.connection; //返回新建立的連接
}
}
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getDriverName() {
return driverName;
}

public void setDriverName(String driverName) {
this.driverName = driverName;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public java.sql.Connection getConnection() {
if (connection != null) {
try {
if (connection.isClosed()) {
connection = null;
getNewConnection();
}
}
catch (SQLException ex) {
}
}
if (connection == null) { //沒有設置連接則創(chuàng)建一個連接
getNewConnection();
}
return connection;
}

public Connection getConnection(String userName, String password) throws
SQLException {
this.setUserName(userName);
this.setPassword(password);
return getConnection();
}

public PrintWriter getLogWriter() {
return null;
}

public void setLogWriter(PrintWriter printWriter) {
}

public void setLoginTimeout(int int0) {
}

public int getLoginTimeout() {

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐安县| 东山县| 磐安县| 兴海县| 乐清市| 新乡市| 蒙山县| 海伦市| 韶山市| 大邑县| 通榆县| 区。| 鲜城| 共和县| 大余县| 左贡县| 水富县| 剑阁县| 台中市| 苍梧县| 澎湖县| 沈丘县| 凌海市| 丘北县| 呼和浩特市| 沛县| 昆明市| 本溪| 保山市| 雅江县| 富蕴县| 曲靖市| 峡江县| 厦门市| 长宁区| 望谟县| 阿克| 馆陶县| 新野县| 左贡县| 石阡县|