①获取连接
②判断是否存在
③查找数据
④修改数据
⑤ 批量处理
⑥释放资源
package com.jd.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* 数据库工具类
*
* @author syy
*/
public class DBUtil {
/**
*
* 获取连接
*
* @author syy
*/
private static Connection getconnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = PropertiesUtil.getValue("jdbc.url");
String userName = PropertiesUtil.getValue("jdbc.userName");
String password = PropertiesUtil.getValue("jdbc.password");
connection = DriverManager.getConnection(url, userName , password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
/**
*
* 判断是否存在
*
* @author syy
*/
public static boolean exist(String sql, Object ...paras ) {
class RowMapper implements IRowMapper {
boolean state;
@Override
public void rowMapper(ResultSet resultSet) {
try {
state = resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper = new RowMapper();
select(sql, rowMapper, paras);
return rowMapper.state;
}
/**
*
* 判断是否存在
*
* @author syy
*/
public static boolean exist(String sql) {
class RowMapper implements IRowMapper {
boolean state;
@Override
public void rowMapper(ResultSet resultSet) {
try {
state = resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper = new RowMapper();
select(sql, rowMapper);
return rowMapper.state;
}
/**
*
* 查找数据
*
* @author syy
*/
public static void select(String sql, IRowMapper rowMapper) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = getconnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);// 执行语句
rowMapper.rowMapper(resultSet); // 处理结果
} catch (Exception e) {
e.printStackTrace();
} finally {
close(resultSet, statement, connection);
}
}
/**
*
* 查找数据
*
* @author syy
*/
public static void select(String sql, IRowMapper rowMapper, Object... paras) { // 上转型对象,接口的实现类创建对象,接口不能创建对象。
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try {
connection = getconnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 1; i <= paras.length; i++) {
preparedStatement.setObject(i, paras[i - 1]);
}
resultSet = preparedStatement.executeQuery(); //因为已经知道preparedStatement对象执行的语句是sql
rowMapper.rowMapper(resultSet);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(resultSet, preparedStatement, connection);
}
}
/**
* 修改数据
*
*
* @author syy
*/
public static void update(String ll, Object... paras) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getconnection();
preparedStatement = connection.prepareStatement(ll); // 创建语句
for (int i = 1; i <= paras.length; i++) {
preparedStatement.setObject(i, paras[i - 1]);
}
int result = preparedStatement.executeUpdate(); //不能添加ll
if (result > 0) {
System.out.println("成功");
} else {
System.out.println("失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(preparedStatement, connection);
}
}
/**
* 修改数据
*
*
* @author syy
*/
public static void update(String ll) {
Connection connection = null;
Statement statement = null;
try {
connection = getconnection();
statement = connection.createStatement(); //创建语句
int result = statement.executeUpdate(ll);
if (result > 0) {
System.out.println("成功");
} else {
System.out.println("失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(statement, connection);
}
}
/**
*
* 批量处理(同时成功或失败)
*
* @author syy
*/
public static boolean batch(String ...sqls) {
Connection connection = null;
Statement statement = null;
try {
connection = getconnection();
connection.setAutoCommit(false);
statement = connection.createStatement();
for(String sql:sqls) {
statement.addBatch(sql);
}
statement.executeBatch();
connection.commit();
return true;
} catch (Exception e) {
if (connection!=null) {
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}finally {
close(statement,connection);
}
return false;
}
/**
*
* preparedStatement(防止sql语句注入) 实现批量处理,只能对同一个SQL语句模板进行批量
*
* @author syy
*/
public static boolean batch(String sql, Object[] ... params) {
Connection connection = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
try {
connection = getconnection();
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql);
for(int i=0; i<params.length; i++) {
for(int j=1; j<=params[i].length ;j++) {
preparedStatement.setObject(j, params[i][j-1]);
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
connection.commit();
System.out.println("11");
return true;
} catch (Exception e) {
if (connection!=null) {
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}finally {
if (statement!=null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
/**
* 释放资源
*
*
* @author syy
*/
private static void close(ResultSet resultSet, Statement statement, Connection connection) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
close(statement, connection);
}
/**
* 释放资源
*
*
* @author syy
*/
private static void close(Statement statement, Connection connection) {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}