ResultSet & PreparedStatement & close

本文探讨了JDBC中Connection、PreparedStatement及ResultSet之间的关系,并强调了关闭PreparedStatement时对ResultSet的影响。通过Mysql的JDBC源码分析,揭示了数据库操作中的细节。

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


Connection --> PreparedStatement --> ResultSet -->



如果此时关闭PreparedStatement ,那么ResultSet 也将受到影响,
看看Mysql的JDBC源码还是有点意思的。

[table]
|DataBase |1:*|Connection|
|Connection| 1:* |PreparedStatement|
|PreparedStatement| 1:* |ResultSet|
[/table]
帮我优化这段代码,谢谢package com.neutech.utils; import java.sql.*; import java.util.ArrayList; import java.util.List; public class Jdbcutil { private static final String DRIVERNAME = "org.apache.hive.jdbc.HiveDriver"; private static final String URL = "jdbc:hive2://192.168.44.199:10000/test"; private static final String USERNAME = "root"; private static final String PASSWORD = "200812"; public static Connection getConnection() throws ClassNotFoundException { Class.forName(DRIVERNAME); Connection connection = null; try { connection = DriverManager.getConnection(URL,USERNAME,PASSWORD); } catch (SQLException throwables) { throwables.printStackTrace(); } return connection; } public static <T> List<T> execuetQuery(String sql, RowMap<T> rowMap, Object... objs) throws ClassNotFoundException, SQLException { // 建立连接 Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); // 注入额外条件 if(objs != null){ for(int i = 0;i <= objs.length;i++){ preparedStatement.setObject(i + 1,objs[i]); } } ResultSet resultSet = preparedStatement.executeQuery(); ArrayList<T> list = new ArrayList<>(); while(resultSet.next()){ T t = rowMap.RowMapping(resultSet); list.add(t); } return list; } public static void close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) throws SQLException { connection.close(); preparedStatement.close(); resultSet.close(); } }
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值