我们在做信息系统的时候,都要访问数据库,我最近接手一个项目,项目组决定使用java编写,我负责数据层的设计和编码,为了提高代码的重用性和提高项目的开发效率。我们开发了一个通用的数据库连接和完成基本操作的类库,个人认为这个类在做mis系统时还是有一定的价值,所以总结出来,介绍给大家。
连接工厂,实现了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;
/**
* 根据设置的连接参数创建一个新的连接实例
* @return
*/
private connection getnewconnection() {
try {
this.connection.close(); //试图关闭连接
}
finally {
this.connection = null; //释放连接
try {
class.forname(this.drivername); //加载驱动程序
//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) { //没有设置连接则创建一个连接
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() {
return 0;
}
}
实现连接sqlserver的连接工厂,这里因为我们的项目使用sqlserver2000所以只实现了sqlserverconnectionfactory。
package skydev.modules.data;
public final class sqlserverconnectionfactory extends connectionfactory {
private final string dbdriver ="com.microsoft.jdbc.sqlserver.sqlserverdriver";
private string host;//主机
private int port;//端口
private string databasename;//sql数据库名称
public sqlserverconnectionfactory() {
super.setdrivername(dbdriver);
}
/**
*
* @param host 数据库所在的主机名:如"localhost"
* @param port sql服务器运行的端口号,如果使用缺省值 1433,传入一个负数即可
* @param databasename 数据库名称
* @param username 用户名
* @param password 口令
*/
public sqlserverconnectionfactory(string host,
int port,
string databasename,
string username,
string password) {
this.sethost(host);
this.setport(port);
this.setdatabasename(databasename);
this.setusername(username);
this.setpassword(password);
init();
}
private void init() {
super.setdrivername(dbdriver);
super.seturl("jdbc:microsoft:sqlserver://" + host.trim() + ":" +
new integer(port).tostring() + ";databasename=" +
databasename.trim());
//super.seturl("jdbc:microsoft:sqlserver://localhost:1433;databasename=demo");
}
public void sethost(string host) {
//处理主机名称
if ( (host == null) || (host.equals("")) || (host.equals(".")) ||
(host.equals("local"))) {
host = "localhost";
}
int index = host.indexof("//", 0);
if (index == 0) {
host = host.substring(2); //去掉前面的"//"
}
index = host.indexof("//", 0);
if (index >= 0) {
try {
throw new exception("sql server主机名参数错误!");
}
catch (exception ex) {
}
}
this.host = host;
}
public void setport(int port) {
/**
* 缺省端口1433
*/
if (port < 0) {
port = 1433;
}
this.port = port;
}
public void setdatabasename(string databasename) {
this.databasename = databasename;
}
}
使用"sun.jdbc.odbc.jdbcodbcdriver"连接数据库的连接工厂
package skydev.modules.data;
public class jdbcodbcconnectionfactory extends connectionfactory {
private final static string drivename = "sun.jdbc.odbc.jdbcodbcdriver";
private string odbcname;
public jdbcodbcconnectionfactory() {
super.setdrivername(drivename);
}
/**
*使用指定的odbc数据源连接数据库服务器
* @param odbcname
*/
public jdbcodbcconnectionfactory(string odbcname) {
super.setdrivername(drivename);
setodbcname(odbcname);
}
public void setodbcname(string odbcname) {
this.odbcname = odbcname;
this.seturl("jdbc:odbc:" + odbcname);
}
}
数据基本操作类,使用连接工厂连接数据库。
package skydev.modules.data;
import java.sql.*;
import java.sql.preparedstatement;
import javax.sql.datasource;
public abstract class databaseobject {
protected connection connection = null;
protected resultset resultset = null;
protected resultsetmetadata resultsetmetadata = null;
private connectionfactory connectionfactory = null;
private java.sql.statement statement=null;
private javax.sql.datasource datasource;//=new statement();
public databaseobject(){
datasource=null;
connection=null;
}
public databaseobject(connectionfactory connectionfactory) {
this.setconnectionfactory(connectionfactory);
this.datasource=connectionfactory;//connectionfactory实现了datasource接口
}
/**
* 执行查询
* @param sql 要执行的sql语句
* @return 返回查询的结果集 ,查询失败返回null
*/
public resultset getresultset(string sql) {
try {
this.resultset = statement.executequery(sql); //保留内部指针
}
catch (sqlexception e) {
e.printstacktrace();
this.resultset = null;
}
finally {
return this.resultset;
}
}
/**
* 获取外部指定resltset的resultsetmetadata数据
* @param resultset 要获取的resultset
* @return 失败返回null
*/
public resultsetmetadata getresultsetmetadata(resultset resultset) {
resultsetmetadata resultsetmetadata = null;
try {
resultsetmetadata = resultset.getmetadata();
}
catch (sqlexception e) {
e.printstacktrace();
resultsetmetadata = null;
}
finally {
return resultsetmetadata;
}
}
/**
* 获取最近一次设置或者返回的resultset的resultmetadata数据,
* 比方说调用了:getresultset(sql)方法,然后调用getresultsetmetadata方法
* 可以获得相应的resultsetmetadata数据。
* @return
*/
public resultsetmetadata getresults
闽公网安备 35060202000074号