DBUtils

apache提供的方便操作数据库的组件

核心

* QueryRunner对象

* query()/update()

* ResultSetHandler接口

* BeanHandler 针对查询一条记录的情况

* BeanListHandler 针对查询多条记录的情况

* ScalarHandler 针对获取第一条记录的指定列的情况

工具类 JdbcUtils

package com.wei.utils;

import java.sql.*;

public class JdbcUtils {

//数据库的驱动是需要执行一次

static {

try {

Class.forName("com.mysql.cj.jdbc.Driver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

/*

获取连接对象

*/

public static Connection getConnection() {

String url = "jdbc:mysql://localhost:3306/java2220?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";

try {

return DriverManager.getConnection(url, "root", "1234");

} catch (SQLException e) {

return null;

}

}

/**

* 关闭相关对象

* @param connection

* @param statement

*/

public static void close(Connection connection, Statement statement) {

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (connection!= null){

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* 关闭对象

* @param connection

* @param statement

* @param resultSet

*/

public static void close(Connection connection, Statement statement, ResultSet resultSet) {

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();

}

}

}

DBUtils

增删改查


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值