package com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* @auther:sun
* date:11-16-afternoon
* desc:数据库连接类
* */
public class DB {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost/log";
private static String user = "root";
private static String pwd = "mysql";
private static Connection conn = null;
private static Statement stmt = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;
/*构造方法*/
public DB(){
conn = getConnection();
}
/*内部方法,用于获得与数据库连接*/
private Connection getConnection(){
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, user, pwd);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/*内部方法,用于得到statement*/
private Statement getStatement(){
try {
stmt = getConnection().createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stmt;
}
/*外部方法,用于得到初始化完的preparedstatement*/
public PreparedStatement getPreStatement(String sql,Object...o){
try {
pstmt = getConnection().prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < o.length; i++) {
try {
pstmt.setObject(i+1, o[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return pstmt;
}
/*外部方法,用于stmt的查询*/
public ResultSet select(String sql){
try {
rs = getStatement().executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/*外部方法,用于preparedstatement的查询*/
public ResultSet select(String sql,Object...o){
try {
rs = getPreStatement(sql, o).executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/*外部方法,用于stmt的增删改*/
public int update(String sql){
int i = 0;
try {
i = getStatement().executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
/*外部方法,用于preparedstatement的增删改*/
public int update(String sql,Object...o){
int i = 0;
try {
i = getPreStatement(sql, o).executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return i;
}
/*外部方法,用于处理事务*/
public int update(PreparedStatement[] pstmt){
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int j = 0;
for (int i = 0; i < pstmt.length; i++) {
try {
j = pstmt[i].executeUpdate() + j;
if (j == 0) {
conn.rollback();
return 0;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return j;
}
//该方法主要用来关闭自动提交
public void closeAutoSubmit(){
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//该方法主要用来打开自动提交
public void openAutoSubmit(){
try {
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*外部方法,用于关闭连接*/
public void close(){
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}