用Java实现数据库应用系统
| 首 页 | 网页素材 | 网页特效 | 网页教程 | flash教程 | 3dmax教程 | ASP教程 | 常用范文 | 应用公文 |
教 程 | 频道首页 | flash教程 | 3dmax教程 | photoshop教程 | html/css | asp教程 | php教程 | fireworks教程 | dreamweaver教程|
资 料 | java教程 | 网页模版 | 搜索引擎 | 电脑技术 | 网络应用 | 操作系统 | 饮食文化 | 应用公文 | 贺词致辞 | 个人求职 |
频 道 | 明星资料 | 民俗风情 | 传统节日 | 人物传记 | 历史资料 | 常用范文 | 名胜古迹 | 体育资料 | 教育资料 | 综合资料 |
你的位置:首页 > 资料教程 > 正文 > 用Java实现数据库应用系统
用Java实现数据库应用系统

http://www.sucai123.com 资料教程 2004-10-4 21:59:01

    我们在做信息系统的时候,都要访问数据库,我最近接手一个项目,项目组决定使用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 Sql

此新闻共有41 2 3 4


-来源:互联网 关闭 用Java实现数据库应用系统】
∷∷ 相 关 文 章 ∷∷
·JSP内幕
·JSP编程进度条设计实例
·在JSP中使用JavaMail
·JSP中日期的用法
·Java教程_线程入门
·Java代码编写30条建议
·JSP避免Form重复提交三种方案
·用JSP操作Cookie
∷∷ 热 门 推 荐 ∷∷