其他分享
首页 > 其他分享> > JDBC工具类

JDBC工具类

作者:互联网

package com.jd.util.db;

import java.sql.*;
import java.util.Properties;

public class DBUtil {

static {
//1、加载驱动类
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

//2.获取MySQL连接
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,userName,password);
}

private static void close(Connection connection,Statement statement){
if(statement!=null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}

private static void close(Connection connection,Statement statement,ResultSet resultSet){
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 select(String sql,IRowMapper rowMapper){

Connection connection =null;
Statement statement =null;
ResultSet resultSet =null;
try {
//2、获取MySQL连接
connection = getConnection();
//3、创建语句
statement = connection.createStatement();
//4、执行SQL语句
resultSet = statement.executeQuery(sql);
//5、处理结果
rowMapper.mapRow(resultSet);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//6、释放资源
close(connection,statement,resultSet);
}
}

public static boolean exist(String sql){
Connection connection= null;
Statement statement =null;
ResultSet resultSet =null;
try {
connection = getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
return resultSet.next();

} catch (SQLException throwables) {
throwables.printStackTrace();
}
close(connection,statement,resultSet);
return false;
}

public static boolean exist(String sql,Object...params){
Connection connection= null;
PreparedStatement preparedStatement=null;
ResultSet resultSet =null;
try {
connection = getConnection();
//创建sql语句
preparedStatement = connection.prepareStatement(sql);
//运行sql语句
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1,params[i]);
}
resultSet = preparedStatement.executeQuery();
return resultSet.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
close(connection,preparedStatement,resultSet);
return false;
}

public static boolean update(String sql){
Connection connection =null;
Statement statement =null;
int effect=0;
try {
//2.连接MySQl数据库 venus
connection = getConnection();
//3.创建SQL语句
statement = connection.createStatement();
//4.执行SQL
effect = statement.executeUpdate(sql);
//5.处理结果
return effect>0;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {//6.释放资源
close(connection,statement);
}
return false;
}

public static boolean update(String sql,Object...params){
Connection connection =null;
PreparedStatement preparedStatement =null;
int effect=0;
try {
//2.连接MySQl数据库 venus
connection = getConnection();
//3.创建SQL语句
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1,params[i]);
}
//4.执行SQL
effect = preparedStatement.executeUpdate();
//5.处理结果
return effect>0;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {//6.释放资源
close(connection,preparedStatement);
}
return false;
}

public static void select(String sql,IRowMapper rowMapper,Object...params){
Connection connection =null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
//2、获取连接
connection= getConnection();
//3、创建语句
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1,params[i]);
}
//4、执行SQL
resultSet= preparedStatement.executeQuery();
//5、处理结果
rowMapper.mapRow(resultSet);
//6、释放资源

} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
close(connection,preparedStatement,resultSet);
}
}
}

标签:JDBC,throwables,resultSet,connection,preparedStatement,statement,工具,null
来源: https://www.cnblogs.com/lezai0514/p/14818433.html