JDBC工具类
作者:互联网
IRowMapper接口:
package com.jd.ntil.db;
import java.sql.ResultSet;
public interface IReowMapper {
void mapRow(ResultSet resultSet);
}
PropertiesUtil类:
package com.jd.ntil.db;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
static Properties properties = new Properties();
static {
InputStream inputStream =PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String value(String key){
return properties.getProperty(key);
}
}
db.properties文件
url=jdbc:mysql://localhost:3306/venususeSSL=false&serverTimezone=UTC
user=root
password=123456
DBUtil类:
package com.jd.ntil.db;
import java.sql.*;
public class
DBUtil {
static {
//1加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getconnection() throws SQLException {
//2.获取数据库连接
String url = PropertiesUtil.value("url");
String user=PropertiesUtil.value("user");
String password=PropertiesUtil.value("password");
return DriverManager.getConnection(url,user,password);
}
public static boolean update(String sql){
try (//6.释放资源
Connection connection = getconnection();
Statement statement = connection.createStatement();
) {
//3.创建sql语句
//4.执行sql语句
int effect = statement.executeUpdate(sql);
//5.处理结果
return effect>0;
} catch (SQLException throwables) {
}
return false;
}
public static boolean update(String sql,Object...object){
try (//6.释放资源
Connection connection = getconnection();
PreparedStatement prepareStatement = connection.prepareStatement(sql);
) {
//3.创建sql语句
for (int i = 0; i < object.length; i++) {
prepareStatement.setObject(i+1,object[i]);
}
//4.执行sql语句
int effect = prepareStatement.executeUpdate();
//5.处理结果
return effect>0;
} catch (SQLException throwables) {
}
return false;
}
// public static void select(String sql, IReowMapper IReowMapper){
// Connection connection =null;
// Statement statement = null;
// ResultSet resultSet = null;
// try {
// //2.获取数据库连接
// connection = getconnection();
// //3.创建语句
// statement = connection.createStatement();
// //4.执行sql语句
// resultSet = statement.executeQuery(sql);
// IReowMapper.mapRow(resultSet);
//
// }catch(SQLException throwables){
// throwables.printStackTrace();
// //6.释放资源
// }
// close(resultSet,connection,statement);
// }
public static void select(String sql, IReowMapper IReowMapper){
try ( //2.获取数据库连接
Connection connection = getconnection();
//3.创建语句
Statement statement = connection.createStatement();
//4.执行sql语句
ResultSet resultSet = statement.executeQuery(sql);){
IReowMapper.mapRow(resultSet);
}catch(SQLException throwables){
throwables.printStackTrace();
}
}
public static void select(String sql,IReowMapper iReowMapper,Object...objects){
Connection connection =null;
PreparedStatement preparedStatement =null;
ResultSet resultSet=null;
try {
connection =getconnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
preparedStatement.setObject(i+1,objects[i]);
}
resultSet=preparedStatement.executeQuery();
iReowMapper.mapRow(resultSet);
DBUtil.close(resultSet,connection,preparedStatement);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
close(resultSet, connection, preparedStatement);
}
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(resultSet,connection,statement);
return false;
}
public static boolean exist(String sql,Object...object){
Connection connection =null;
PreparedStatement preparedStatement =null;
ResultSet resultSet =null;
try {
connection = getconnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < object.length; i++) {
preparedStatement.setObject(i+1,object[i]);
}
resultSet = preparedStatement.executeQuery();
return resultSet.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
close(resultSet,connection,preparedStatement);
return false;
}
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(ResultSet resultSet,Connection connection,Statement statement){
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();
}
}
}
}
标签:JDBC,throwables,resultSet,connection,statement,sql,工具,null 来源: https://www.cnblogs.com/javaQu/p/14818832.html