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();
}
}
}