如何将jdbc封装成自己的工具类~
1.下面还会有一些其他的封装:比如增删改查,分页。。。
package wonderful.wzf;
import java.sql.*;
/**
* @author :wonderful_wzf
* @message:将jdbc封装成JdbcUtil工具类
* @Bolg :https://blog.csdn.net/wonderful_wzf
* @date :2020/5/18,19:54
*/
public class JdbcUtil {
/**
* Connection连接的对象
*/
static Connection connection = null;
/**
* PreparedStatement对象
*/
static PreparedStatement preparedStatement = null;
/**
* 结果集
*/
static ResultSet resultSet = null;
/**
* mysql的jdbc驱动
*/
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
/**
* 登录数据库的url
*/
static final String JDBC_URL = "jdbc:mysql://localhost:3306/wonderful";
/**
* 登录数据库的用户名
*/
static final String USER = "root";
/**
* 登录数据库的密码
*/
static final String PASSWORD = "root";
/**
* @function 获取Connection
* @return 数据库的Connection对象
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
return connection;
}
/**
* @function 防止sql注入,预编译得到PreparedStatement对象
* @param sql
* @param params
* @return
*/
public static PreparedStatement getPreparedStatement(String sql,Object[] params) throws SQLException, ClassNotFoundException {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
if(params!=null){
for(int i = 0 ;i<params.length;i++){
preparedStatement.setObject(i+1,params[i]);
}
}
return preparedStatement;
}
/**
* 关闭数据库连接
* @param connection
* @param statement
* @param resultSet
*/
public static void close( ResultSet resultSet, Statement statement,Connection connection){
/*connection,statement,resultSet关闭有先后顺序要求吗 ?
注:他们三者之间关闭没有任何关联,即先关闭谁没有任何先后顺序
,可以先关闭他们中的任何一个,且关闭其中的任何一个对象都不会
关闭其他其他对象,但一般养成按关闭ResultSet,Statement,
Connection的顺序关闭资源.*/
if(resultSet!=null){
try {
resultSet.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();
}
}
}
}
2.自己以前写的封装,经供参考~
//增删改
//注意数组字符串要用双引号而不是单引号
public boolean executeUpdate(String sql,Object[] params) {
int num =-1;
try {
pstm = getPreparedStatement(sql, params);
num=pstm.executeUpdate();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
finally {
closeAll(rs, pstm, con);
}
if(num>0) {
return true;
}
else {
return false;
}
}
//查
//展示所有
public ResultSet executeQuery(String sql,Object[] params) {
int num =0;
try {
pstm = getPreparedStatement(sql, params);
rs = pstm.executeQuery();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return rs;
}
//分页查询操作 currentPage:传入当前页和pageSize:页面大小
public ResultSet paginationQuery(int currentPage,int pageSize) throws ClassNotFoundException, SQLException {
con=getConnection();
String sql = "select * from user limit ?,?";
pstm = con.prepareStatement(sql);
pstm.setInt(1,currentPage*pageSize);
pstm.setInt(2, pageSize);
rs=pstm.executeQuery();
return rs;
}
据我现在了解,好像分页慢慢的不需要我们后台来做的,前端可以完成的~
ps:今天还学了:创建连接池,和c3p0连接池,已经很晚了,明天更新这点内容欧~
欢迎大家讨论学习,互相进步