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 qiangzhogshou
*/
public class DBUtil {
static {
try {
Class.forName("com.mysql.jdbc.Driver");//1、加载驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 连接数据库
*
* @author qiangzhogshou
*/
private static Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");//2、连接数据库
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 判断数据是否存在
*
* @author qiangzhogshou
*/
public static boolean exist(String sql) {
class Rom implements IrowMapper{
boolean stat;
@Override
public void rowMapper(ResultSet result) {
try {
stat=result.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Rom rom =new Rom();
select(sql, rom);
return rom.stat;
}
/**
* 判断数据是否存在
*
* @author qiangzhogshou
*/
public static boolean exist(String sql,Object...obj) {
class Rom implements IrowMapper{
boolean stat;
@Override
public void rowMapper(ResultSet result) {
try {
stat=result.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Rom rom =new Rom();
select(sql, rom,obj);
return rom.stat;
}
/**
* 修改数据
*
* @author qiangzhogshou
*/
public static boolean update(String sql) {
Statement statement = null;
Connection connection = null;
try {
connection = getConnection();
statement = connection.createStatement();//3、创建语句
int result = statement.executeUpdate(sql);//4、执行语句
return result > 0;//5、处理结果
} catch (Exception e) {
e.printStackTrace();
} finally {
close(statement, connection);
}
return false;
}
/**
* 修改数据
*
* @author qiangzhogshou
*/
public static boolean update(String sql,Object...array) {
PreparedStatement preparedStatement=null;
Connection connection = null;
try {
connection = getConnection();
preparedStatement =connection.prepareStatement(sql);
for (int i = 1; i <=array.length; i++) {
preparedStatement.setObject(i, array[i-1]);
}
int result = preparedStatement.executeUpdate();
return result > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
close(preparedStatement, connection);
}
return false;
}
/**
* 查询数据
*
* @author qiangzhogshou
*/
public static void select(String sql, IrowMapper rowMapper) {
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try {
connection = getConnection();
statement = connection.createStatement();//3、创建语句
result = statement.executeQuery(sql);//4、执行语句
rowMapper.rowMapper(result);//5、处理结果
} catch (Exception e) {
e.printStackTrace();
} finally {
close(result, statement, connection);
}
}
/**
* 查询数据
*
* @author qiangzhogshou
*/
public static void select(String sql, IrowMapper irowMapper, Object... array) {
ResultSet result = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 1; i <= array.length; i++) {
preparedStatement.setObject(i, array[i - 1]);
}
result = preparedStatement.executeQuery();
irowMapper.rowMapper(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(result, preparedStatement, connection);
}
}
/**
* 批量操作
*
* @author qiangzhogshou
*/
public static boolean batch(String...sql) {
Connection connection=null;
Statement statement=null;
try {
connection = getConnection();
connection.setAutoCommit(false);
statement=connection.createStatement();
for (String string : sql) {
statement.addBatch(string);
}
statement.executeBatch();
connection.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
if(connection!=null) {
try {
connection.rollback();
System.out.println(1);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}finally {
close(statement, connection);
}
return false;
}
/**
* 批量操作
*
* @author qiangzhogshou
*/
public static boolean batch(String sql,Object[]...objs) {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection = getConnection();
connection.setAutoCommit(false);
preparedStatement=connection.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
for (int j = 1; j <= objs[i].length; j++) {
preparedStatement.setObject(j, objs[i][j - 1]);
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
connection.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
if(connection!=null) {
try {
connection.rollback();
System.out.println(1);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}finally {
close(preparedStatement, connection);
}
return false;
}
/**
* 释放资源
*
* @author qiangzhogshou
*/
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 qiangzhogshou
*/
private static void close(ResultSet result, Statement statement, Connection connection) {
if (result != null) {
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
close(statement, connection);
}
}
Java:jdbc:DBUtil工具类
最新推荐文章于 2022-01-19 16:43:16 发布