JDBC工具类
package db;
import db.IRowMapper;
import java.sql.*;
public class DBUtill {
public static void main(String[] args) {
}
public static void load() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取sql连接
*
* @author csq
*/
private static Connection getConnection() throws SQLException {
String url = PropertiesUtil.value("url");
String userName = PropertiesUtil.value("userName");
String password = PropertiesUtil.value("password");
return DriverManager.getConnection(url, "root", "123");
}
public static void select(String sql, IRowMapper iRowMapper) {
//1.加载驱动类
load();
//2.连接数据库venus
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
//3.创建语句
statement = connection.createStatement();
//4.执行sql语句
resultSet = statement.executeQuery(sql);
//5.处理结果
iRowMapper.rowMapper(resultSet);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//6.释放资源
close(resultSet, statement, connection);
}
}
public static boolean update(String sql) {
load();
//2.连接Mysql数据venus
Statement statement = null;
Connection connection = null;
try {
connection = getConnection();
//3.创建SQL语句
statement = connection.createStatement();
int effect = statement.executeUpdate(sql);
return effect > 0;
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(statement, connection);
}
return false;
}
public static boolean update(String sql, Object... params) {
load();
//2.连接Mysql数据venus
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = getConnection();
//3.创建SQL语句
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
int effect = preparedStatement.executeUpdate();
return effect >0;
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(preparedStatement, connection);
}
return false;
}
public static boolean exists(String sql) {
load();
ResultSet resultSet = null;
Statement statement = null;
Connection connection = null;
try {
connection = getConnection();
statement = connection.createStatement();
//4.执行sql语句
resultSet = statement.executeQuery(sql);
//5.处理结果
return resultSet.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//6.释放资源
close(resultSet, statement, connection);
}
return false;
}
public static boolean exists(String sql, Object... parms) {
load();
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < parms.length; i++) {
preparedStatement.setObject(i + 1, parms[i]);
}
//4.执行sql语句
resultSet = preparedStatement.executeQuery();
//5.处理结果
return resultSet.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//6.释放资源
close(resultSet, preparedStatement, connection);
}
return false;
}
public static void login(String sql, IRowMapper irowMapper, Object... parmms) {
//1.加载驱动类
load();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
//3.创建语句
//4.执行sql
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < parmms.length; i++) {
preparedStatement.setObject(i + 1, parmms[i]);
}
resultSet = preparedStatement.executeQuery();
irowMapper.rowMapper(resultSet);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(resultSet, preparedStatement, connection);
}
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
IRowMapper接口
interface IRowMapper {
void rowMapper(ResultSet resultSet);
}

浙公网安备 33010602011771号