使用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 {
/**
* 程序驱动
*
* @author 冯申然
*/
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 连接数据库
*
* @author 冯申然
*/
public static Connection getconnection() {
String url = PropertiesUtil.getValue("jdbc.url");
String userName = PropertiesUtil.getValue("jdbc.userName");
String root = PropertiesUtil.getValue("jdbc.root");
// System.out.println(url);
// System.out.println(userName);
// System.out.println(root);
try {
return DriverManager.getConnection(url,userName, root);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 数据的增删改查
*
* @author 冯申然
*/
public static boolean updata(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 updata(String sql,Object...params) {//增删改
Connection connection =null;
PreparedStatement prepareStatement=null;
try {
connection=getconnection();
prepareStatement = connection.prepareStatement(sql);
for(int i=1;i<=params.length;i++) {
prepareStatement.setObject(i, params[i-1]);
}
int result = prepareStatement.executeUpdate();
return result>0;
} catch (Exception e) {
e.printStackTrace();
}finally {
close(prepareStatement,connection);
}
return false;
}
/**
* 登录设置
*
* @author 冯申然
*/
public static void select(String sql,IRowMapper rowMapper,Object...params) {//登录设置
ResultSet result =null;
Statement statement =null;
Connection connection =null;
PreparedStatement preparedStatement =null;
try {
connection=getconnection();
preparedStatement =connection.prepareStatement(sql);
for(int i=1;i<=params.length;i++) {
preparedStatement.setObject(i, params[i-1]);
}
result =preparedStatement.executeQuery();
rowMapper.rowMapper(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(result,statement,connection);
}
}
/**
* 检验数据是否存在
*
* @author 冯申然
*/
public static boolean exist (String sql) {//是否存在
class RowMapper implements IRowMapper{
boolean state;
@Override
public void rowMapper(ResultSet result) {
try {
state=result.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper =new RowMapper();
select(sql,rowMapper);
return rowMapper.state;
}
/**
* 检验数据是否存在
*
* @author 冯申然
*/
public static boolean exist (String sql,Object...params) {//是否存在
class RowMapper implements IRowMapper{
boolean state;
@Override
public void rowMapper(ResultSet result) {
try {
state=result.next();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper =new RowMapper();
select(sql,rowMapper,params);
return rowMapper.state;
}
/**
* 批量操作
*
* @author 冯申然
*/
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.setAutoCommit(true);
return true;
} catch (Exception e) {
e.printStackTrace();
if (connection!=null) {
try {
connection.rollback();
} catch (Exception e1) {
e1.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 冯申然
*/
public static void select(String sql,IRowMapper rowMapper) {//查询
Connection connection =null;
Statement statement =null;
ResultSet result =null;
try {
connection=getconnection();
statement = connection.createStatement();
result = statement.executeQuery(sql);
rowMapper.rowMapper(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(result,statement,connection);
}
}
/**
* 查询数据
*
* @author 冯申然
*/
public static void Select(String sql,IRowMapper rowMapper,Object...params) {//查询
Connection connection =null;
Statement statement =null;
ResultSet result =null;
PreparedStatement preparedStatement =null;
try {
connection=getconnection();
preparedStatement =connection.prepareStatement(sql);
for(int i=1;i<=params.length;i++) {
preparedStatement.setObject(i, params[i-1]);
}
result =preparedStatement.executeQuery() ;
rowMapper.rowMapper(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(result,statement,connection);
}
}
/**
* 批量处理
*
* @author 冯申然
*/
public static boolean batch(String sql,Object[]...parmas) {
Connection connection =null;
PreparedStatement prepareStatement =null;
try {
connection = getconnection();
connection.setAutoCommit(false);
prepareStatement =connection.prepareStatement(sql);
for (int i=0;i<parmas.length;i++) {
for(int j=1;j<=parmas[i].length;i++){
prepareStatement.setObject(j, parmas[i][j-1]);
}
prepareStatement.addBatch();
}
prepareStatement.executeBatch();
connection.setAutoCommit(true);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
close(prepareStatement,connection);
}
return false;
}
/**
* 释放资源
*
* @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 result,Statement statement,Connection connection){
if (result!=null) {
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}