关于java jdbc 数据连接类(dataBase)封装

package util;

 

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

 

public class DataBase {

 

 

/**

* 数据连接实例变量

*/

private Connection conn;

 

 

public Connection getConn() {

return conn;

}

 

 

/**

* 构造方法

* @param conn

*/

public DataBase(Connection conn){

this.conn =  conn;

}

 

/**

* 构造方法

* @param isAutoCommit  是否自动提交

* @param conn

* @throws Exception

*/

public DataBase(boolean isAutoCommit,Connection conn) throws Exception{

this.conn = conn;

this.conn.setAutoCommit(isAutoCommit);

}

 

/**

* 构造方法

* @param isAutoCommit

* @throws Exception

*/

public DataBase(boolean isAutoCommit) throws Exception{

try{

this.conn = DBConnection.getInstance().getConnection();

if(conn != null){

System.out.println("数据库连接成功");

}

}catch (Exception e) {

System.out.print("数据库连接失败(" + e.getLocalizedMessage() + ")");

throw new SQLException("Fail at create dababase connection!"+ e.getMessage());

}

//设置是否自动提交

try{

if(conn != null){

this.conn.setAutoCommit(isAutoCommit);

}else{

System.out.println("11");

}

}catch(Exception e){

e.printStackTrace();

}

}

 

/**

* 提交事务处理

* @throws Exception 

*/

public void commit() throws Exception{

try {

this.conn.commit();

} catch (SQLException e) {

throw new Exception("Fail at database commit!" + e.getMessage());

}

}

 

/**

* 事务回滚

*/

public void rollback(){

try {

this.conn.rollback();

} catch (SQLException e) {

e.printStackTrace();

}

}

 

 

 

/**

* 关闭ResultSet

* @param rs

*/

public void  close(ResultSet rs){

if(rs != null){

try {

Statement stmt =  rs.getStatement();

rs.close();

rs = null;

if(stmt != null){

stmt.close();

stmt = null;

}

} catch (SQLException e) {

e.printStackTrace();

System.out.println("关闭ResultSet失败");

}

}

}

 

 

/**

* 关闭连接

*/

public void cleanup(){

try{

if(this.conn != null){

if(!conn.isClosed()){

this.conn.close();

}

}

}catch(Exception e){

e.printStackTrace();

}

}

 

 

/**

* 执行update

* @param sql

* @return

* @throws SQLException

*/

public  int  execSqlUpdate(String sql) throws SQLException{

Statement stmt = null;

try{

int execResult;

stmt = this.conn.createStatement();

execResult = stmt.executeUpdate(sql);

//stmt.close();

return execResult;

}catch(Exception e){

String username  = this.conn.getMetaData().getUserName();

String url = this.conn.getMetaData().getURL();

throw new SQLException("在" + url + "的用户" + username + ",执行SQL失败("

+ e.getMessage() + ")!SQL:[" + sql + "]");

}finally{

try{

if(!stmt.isClosed()){

stmt.close();

}

}catch(Exception e){

e.printStackTrace();

}

}

}

 

 

 

/**

* 得到ResultSet记录

* @param sql

* @return

* @throws Exception

*/

public ResultSet getRS(String sql) throws SQLException {

Statement stmt = null;

ResultSet rs = null;

try {

stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

} catch (SQLException e) {

if (stmt != null) {

stmt.close();

stmt = null;

}

String userName = conn.getMetaData().getUserName();

String url = conn.getMetaData().getURL();

throw new SQLException("在" + url + "的用户" + userName + ",执行SQL失败("

+ e.getMessage() + ")!SQL:[" + sql + "]");

}

return rs;

 

}

 

 

/**

* @param args

*/

public static void main(String[] args) {

ResultSet rs = null;

try {

DataBase db = new DataBase(true);

String sql = "select id as ID,name as NAME from app_user";

rs = db.getRS(sql);

while(rs.next()){

long userId = rs.getLong(1);

String userName = rs.getString(2);

System.out.println("用户id:"+userId+"   用户名:"+userName);

}

db.close(rs);

} catch (Exception e) {

e.printStackTrace();

}

}

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值