import java.sql.*;
import java.util.ResourceBundle;
public class DBUtils {
private static final String DRIVER;
private static final String USERNAME;
private static final String PASSWORD;
private static final String URL;
static {
ResourceBundle bundle = ResourceBundle.getBundle("db");
DRIVER = bundle.getString("jdbc.driver");
USERNAME = bundle.getString("jdbc.user");
PASSWORD = bundle.getString("jdbc.password");
URL = bundle.getString("jdbc.url");
}
/**
* 获取连接方法
*
* @return 连接
*/
public static Connection getConnection() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取Statement语句对象
*
* @param connection 连接
* @return Statement对象
*/
public static Statement createStatement(Connection connection) {
try {
return connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 执行增加删除修改的操作
*
* @param statement
* @param sql
* @return
*/
public static int executeUpdate(Statement statement, String sql) {
try {
return statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
/**
* 执行增加删除和修改的操作
*
* @param sql 要执行的sql语句
* @return 影响的行数
*/
public static int executeUpdate(String sql) {
Connection connection = getConnection();
Statement statement = createStatement(connection);
int i = executeUpdate(statement, sql);
DBUtils.close(statement, connection);
return i;
}
public static int executeUpdatePrepared(String sql, Object... args) {//0...n
Connection connection = getConnection();
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
int index=1;
for (Object arg : args) {
statement.setObject(index++,arg);
}
return statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return -1;
}finally{
DBUtils.close(statement,connection);
}
}
/**
* 查询方法
*
* @param sql
* @return DBObject对象
*/
public static DBObject executeQuery(String sql) {
Connection connection = getConnection();
Statement statement = createStatement(connection);
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return new DBObject(connection, statement, resultSet);
}
public static DBObject executeQueryPrepared(String sql,Object...args){
Connection connection = getConnection();
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
int index=1;
for (Object arg : args) {
statement.setObject(index++,arg);
}
ResultSet resultSet=statement.executeQuery();
return new DBObject(connection,statement,resultSet);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void close(DBObject dbObject) {
close(dbObject.getResultSet(), dbObject.getStatement(), dbObject.getConnection());
}
/**
* 关闭连接
*
* @param connection
*/
public static void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭语句对象
*
* @param statement
*/
public static void close(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭结果集对象
*
* @param resultSet
*/
public static void close(ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭连接和语句对象
*
* @param statement
* @param connection
*/
public static void close(Statement statement, Connection connection) {
close(statement);
close(connection);
}
/**
* 关闭连接、语句对象和结果集
*
* @param statement
* @param connection
* @param resultSet
*/
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
close(resultSet);
close(statement);
close(connection);
}
}
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBObject {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
public DBObject() {
}
public DBObject(Connection connection, Statement statement, ResultSet resultSet) {
this.connection = connection;
this.statement = statement;
this.resultSet = resultSet;
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public Statement getStatement() {
return statement;
}
public void setStatement(Statement statement) {
this.statement = statement;
}
public ResultSet getResultSet() {
return resultSet;
}
public void setResultSet(ResultSet resultSet) {
this.resultSet = resultSet;
}
}