开发可重用的数据库连接
2008-09-08 14:33
127 查看
1、定义该类需要用到的一些属性与实例
private static final String DRIVERCLASS="com.microsoft.jdbc.sqlserver.SQLServerDriver";
protect final String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_JSDQ10";
protect final String user="sa";
protect final String password="";
protect Connection conn;
2、负责加裁数据库驱动的静态方法
static{
try{
Class.forName(DRIVERCLASS);
}catch(ClassNotFoundException e){
System.out.println(" Load Class error.");
e.printStackTrace();
}
}
3、建立数据库的连接并设置自动提交模式
public boolean openConn(boolean autoCommint){
boolean isOpen=true;
try{
conn=DriverManager.getConnection(url, user, password);
if(!autoCommit)
conn.setAutoCommit(false);
}catch(SQLException e){
isOpen=false;
System.out.println(" Create connection error.");
e.printStackTrace();
}
}
4、关闭数据库连接
public boolean closeConn(){
boolean isCommit=true;
try{
conn.commit();
}catch(SQLException e){
isCommit=false;
System.out.println("Commit error.");
e.printStackTrace();
try{
conn.rollback();
}catch(SQLException e1){
System.out.println("Rollback error.");
e1.printStackTrace();
}
}final{
try{
stmt.close();
conn.close();
}catch(SQLException e){
System.out.println("Close Connection error.");
e.printStackTrace();
}
return isCommit;
}
}
5、获取数据库连接
public class TestJDBC extends JDBC{
public List<Object[]> selectBySQL(String sql){
List<Object[]> notes=new ArrayList<Object[]>();
this.openConn(true);
try{
this.stmt=conn.createStatement();
this.rs=stmt.executeQuery(sql);
int l=rs.getMetaData().getColumnCount();
while(rs.next()){
Object[] note=new Object[];
for(int i=0; i<l; i++){
note[i]=rs.getObject(i+1);
}
notes.add(note);
}
this.rs.close();
this.stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
this.closeConn();
return notes;
}
}
调用例:
List<Object[]> notes=testJDBC.selectBySQL("select * from tb_test_join");
for(int m=0; m<notes.size(); m++){
out.println("<tr align='center' bgcolor='#FFCC99'>");
Object[] note=notes.get(m);
for(int n=0; n<note.length; n++){
out.println("<td>"+note
+"</td>");
}
out.println("</tr>");
}
private static final String DRIVERCLASS="com.microsoft.jdbc.sqlserver.SQLServerDriver";
protect final String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_JSDQ10";
protect final String user="sa";
protect final String password="";
protect Connection conn;
2、负责加裁数据库驱动的静态方法
static{
try{
Class.forName(DRIVERCLASS);
}catch(ClassNotFoundException e){
System.out.println(" Load Class error.");
e.printStackTrace();
}
}
3、建立数据库的连接并设置自动提交模式
public boolean openConn(boolean autoCommint){
boolean isOpen=true;
try{
conn=DriverManager.getConnection(url, user, password);
if(!autoCommit)
conn.setAutoCommit(false);
}catch(SQLException e){
isOpen=false;
System.out.println(" Create connection error.");
e.printStackTrace();
}
}
4、关闭数据库连接
public boolean closeConn(){
boolean isCommit=true;
try{
conn.commit();
}catch(SQLException e){
isCommit=false;
System.out.println("Commit error.");
e.printStackTrace();
try{
conn.rollback();
}catch(SQLException e1){
System.out.println("Rollback error.");
e1.printStackTrace();
}
}final{
try{
stmt.close();
conn.close();
}catch(SQLException e){
System.out.println("Close Connection error.");
e.printStackTrace();
}
return isCommit;
}
}
5、获取数据库连接
public class TestJDBC extends JDBC{
public List<Object[]> selectBySQL(String sql){
List<Object[]> notes=new ArrayList<Object[]>();
this.openConn(true);
try{
this.stmt=conn.createStatement();
this.rs=stmt.executeQuery(sql);
int l=rs.getMetaData().getColumnCount();
while(rs.next()){
Object[] note=new Object[];
for(int i=0; i<l; i++){
note[i]=rs.getObject(i+1);
}
notes.add(note);
}
this.rs.close();
this.stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
this.closeConn();
return notes;
}
}
调用例:
List<Object[]> notes=testJDBC.selectBySQL("select * from tb_test_join");
for(int m=0; m<notes.size(); m++){
out.println("<tr align='center' bgcolor='#FFCC99'>");
Object[] note=notes.get(m);
for(int n=0; n<note.length; n++){
out.println("<td>"+note
+"</td>");
}
out.println("</tr>");
}
相关文章推荐
- 自行开发了个可重用的数据库连接框架
- Delphi 开发时运行出现 程序连接数据库错误 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
- 完整java开发中JDBC连接mysql或者其他数据库代码和步骤
- ADO数据库连接问题-win7下mfc开发程序在XP系统下连接问题
- 通过Maven配置测试环境和开发环境连接不同的数据库
- 数据库连接监控组件,避免日常开发中因为数据库连接长时间占用或业务完成后忘记关闭连接所带来的数据库问题
- 数据库开发基本操作-SQL Server 2005 不允许远程连接解决方法
- 持久化API(JPA)系列(三)实体Bean的开发技术-建立与数据库的连接
- java开发中JDBC连接数据库代码和步骤详解
- JScript 快速开发框架 Edk:DAO 数据库连接
- java开发中基于JDBC连接数据库实例总结
- node.js 开发指南 – Node.js 连接 MySQL 并进行数据库操作
- 完整java开发中JDBC连接数据库代码和步骤
- 用工厂模式开发多数据库连接类
- 完整java开发中JDBC连接数据库代码和步骤
- Delphi开发学习一:使用ADOConnection控件连接数据库SQL
- java开发中JDBC连接数据库代码和步骤
- java开发JDBC连接数据库
- java开发中JDBC连接数据库代码和步骤
- 完整java开发中JDBC连接数据库代码和步骤 .