IRowMapper接口:
package com.jd.util;
import java.sql.ResultSet;
public interface IRowMapper {
void rowMapper(ResultSet resultSet);
}
DBUtil工具类:
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 赵斌
*/
public class DBUtil {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接
*
* @author 赵斌
*/
private static Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 修改数据
*
* @author 赵斌
*/
public static boolean update(String sql) {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
statement = connection.createStatement();
int result = statement.executeUpdate(sql);
return result > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
close(statement, connection);
}
return false;
}
/**
* 修改数据
*
* @author 赵斌
*/
public static boolean update(String sql, Object... param) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 1; i <= param.length; i++) {
preparedStatement.setObject(i, param[i - 1]);
}
int result = preparedStatement.executeUpdate();
return result > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
close(preparedStatement, connection);
}
return false;
}
/**
* 判断数据是否存在
*
* @author 赵斌
*/
public static boolean exist(String sql) {
class RowMapper implements IRowMapper {
boolean flag;
@Override
public void rowMapper(ResultSet resultSet) {
try {
flag = resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper = new RowMapper();
select(sql, rowMapper);
return rowMapper.flag;
}
/**
* 判断数据是否存在
*
* @author 赵斌
*/
public static boolean exist(String sql, Object... params) {
class RowMapper implements IRowMapper {
boolean flag;
@Override
public void rowMapper(ResultSet resultSet) {
try {
flag = resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper = new RowMapper();
select(sql, rowMapper, params);
return rowMapper.flag;
}
/**
* 查询数据
*
* @author 赵斌
*/
public static void select(String sql, IRowMapper rowmapper) {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = getConnection();
statement = connection.createStatement();
rs = statement.executeQuery(sql);
rowmapper.rowMapper(rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rs, statement, connection);
}
}
/**
* 查询数据
*
* @author 赵斌
*/
public static void select(String sql, IRowMapper rowmapper, Object... params) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 1; i <= params.length; i++) {
preparedStatement.setObject(i,params[i-1]);
}
rs = preparedStatement.executeQuery();
rowmapper.rowMapper(rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rs, preparedStatement, connection);
}
}
/**
* 释放资源
*
* @author 赵斌
*/
private static void close(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 释放资源
*
* @author 赵斌
*/
private static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
close(statement, connection);
}
}