Java关闭数据库资源的两种方式

本文提供了两个使用 Java JDBC 进行数据库操作的示例,包括传统方式关闭资源及 try-with-resources 的现代做法,展示了如何插入数据。

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

第一种:

_______________________________________________________________________________

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 关闭资源的常用操作一
 * @author Joey
 */
public class JDBC_Test1 {

    private static String url = "jdbc:mysql://127.0.0.1:3306/study?characterEncoding=UTF-8";
    private static String userName = "root";
    private static String passWord = "joey";

    public static void main(String[] args) {

        Connection connection=null;
        Statement statement=null;
        String sql=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, userName, passWord);
            statement = connection.createStatement();
            sql = "insert into employee values(null," + "'Jack" + "'" + ",'jack@gmail.com'" + ",'5000')";
            statement.execute(sql);
            System.out.println("OK");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (statement!=null) {
                try {
                    statement.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null) {
                try {
                    statement.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



第二种:

_______________________________________________________________________________

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 关闭资源的常用操作二
 * try-with-resource
 * @author Joey
 */
public class JDBC_Test2 {

    private static String url = "jdbc:mysql://127.0.0.1:3306/study?characterEncoding=UTF-8";
    private static String userName = "root";
    private static String passWord = "joey";

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 将资源声明在try()中,当语句结束,资源将被关闭
        try (Connection connection = DriverManager.getConnection(url, userName, passWord);
                Statement statement = connection.createStatement();) {
            String sql = "insert into employee values(null," + "'Tom" + "'" + ",'tom@gmail.com'" + ",'5000')";
            statement.execute(sql);
            System.out.println("OK");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

完~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值