jdbc学习3_JDBCTools

这个博客介绍了如何创建一个名为JDBCTools的Java工具类,用于执行SQL语句,包括更新操作。该类提供了使用PreparedStatement执行带有占位符的SQL的方法,以及执行insert、update或delete语句的方法。此外,还包含了释放数据库资源和获取数据库连接的辅助方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.atguigu.jdbc;
 
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
public class JDBCTools {
 
    /**
     * 执行 SQL 语句, 使用 PreparedStatement
     * @param sql
     * @param args: 填写 SQL 占位符的可变参数
     */
    public static void update(String sql, Object ... args){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        
        try {
            connection = JDBCTools.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            
            for(int i = 0; i < args.length; i++){
                preparedStatement.setObject(i + 1, args[i]);
            }
            
            preparedStatement.executeUpdate();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            JDBCTools.releaseDB(null, preparedStatement, connection);
        }
    }
    
    /**
     * 执行 SQL 的方法
     * 
     * @param sql: insert, update 或 delete。 而不包含 select
     */
    public static void update(String sql) {
        Connection connection = null;
        Statement statement = null;
 
        try {
            // 1. 获取数据库连接
            connection = getConnection();
 
            // 2. 调用 Connection 对象的 createStatement() 方法获取 Statement 对象
            statement = connection.createStatement();
 
            // 4. 发送 SQL 语句: 调用 Statement 对象的 executeUpdate(sql) 方法
            statement.executeUpdate(sql);
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5. 关闭数据库资源: 由里向外关闭.
            releaseDB(null, statement, connection);
        }
    }
 
    /**
     * 释放数据库资源的方法
     * 
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void releaseDB(ResultSet resultSet, Statement statement,
            Connection 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();
            }
        }
 
    }
 
    /**
     * 获取数据库连接的方法
     */
    public static Connection getConnection() throws IOException,
            ClassNotFoundException, SQLException {
        // 0. 读取 jdbc.properties
        /**
         * 1). 属性文件对应 Java 中的 Properties 类 2). 可以使用类加载器加载 bin 目录(类路径下)的文件
         */
        Properties properties = new Properties();
        InputStream inStream = ReviewTest.class.getClassLoader()
                .getResourceAsStream("jdbc.properties");
        properties.load(inStream);
 
        // 1. 准备获取连接的 4 个字符串: user, password, jdbcUrl, driverClass
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String driverClass = properties.getProperty("driverClass");
 
        // 2. 加载驱动: Class.forName(driverClass)
        Class.forName(driverClass);
 
        // 3. 调用
        // DriverManager.getConnection(jdbcUrl, user, password)
        // 获取数据库连接
        Connection connection = DriverManager.getConnection(jdbcUrl, user,
                password);
        return connection;
    }
 
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值