版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
package com.jd.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 数据库工具类
*
*@author 叶峻韬
*/
public class DBUtil {
/**
* 加载驱动
*/
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*建立连接
*
*@author 叶峻韬
*/
private static Connection getconnection() {
try {
String url = PropertiesUtil.getValue("jdbc.url");
String userName = PropertiesUtil.getValue("jdbc.userName");
String password = PropertiesUtil.getValue("jdbc.password");
return DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 查询数据
*
*@author 叶峻韬
*/
public static boolean select(String sql,IRowMapper rowMapper,Object...params) {
PreparedStatement prepareStatement=null;
Connection connection=null;
try {
connection=getconnection();
prepareStatement = connection.prepareStatement(sql);
for (int i = 1; i <= params.length; i++) {
prepareStatement.setObject(i, params[i-1]);
}
ResultSet resultSet=prepareStatement.executeQuery();
rowMapper.rowMapper(resultSet);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(prepareStatement, connection);
}
return false;
}
/**
* 修改数据
*
*@author 叶峻韬
*/
public static boolean update(String sql,Object...params){
PreparedStatement prepareStatement=null;
Connection connection=null;
try {
connection=getconnection();
prepareStatement = connection.prepareStatement(sql);
for(int i=1;i<=params.length;i++) {
prepareStatement.setObject(i, params[i-1]);
}
int effectRows=prepareStatement.executeUpdate();
return effectRows>0;
} catch (Exception e) {
e.printStackTrace();
}finally {
close(prepareStatement, connection);
}
return false;
}
/**
* 判断数据是否存在
*
*@author 叶峻韬
*/
public static boolean exist(String sql,Object...params) {
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,params);
return rowMapper.state;
}
/**
* 批量处理
*
*@author 叶峻韬
*/
public static boolean batch(String sql,Object...params) {
Connection connection=null;
PreparedStatement prepareStatement=null;
try {
connection = getconnection();
connection.setAutoCommit(false);
for(int i=0;i<params.length;i++) {
prepareStatement=connection.prepareStatement(sql);
for(int j=1;j<=params.length;j++) {
prepareStatement.setObject(j, params[j-1]);
}
prepareStatement.addBatch();
}
prepareStatement.executeBatch();
return true;
} catch (Exception e) {
e.printStackTrace();
if(connection!=null)
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}finally {
close(prepareStatement, connection);
}
}
return false;
}
/**
* 释放资源
*
*@author 叶峻韬
*/
private static void close(PreparedStatement prepareStatement, Connection connection) {
try {
if (prepareStatement != null) {
prepareStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.jd.util;
import java.sql.ResultSet;
/**
* 用于重写方法的接口
*
*@author 叶峻韬
*/
public interface IRowMapper{
void rowMapper(ResultSet resultSet);
}