转:JDBC中关于PreparedStatement.setObject的一些细节说明

原文地址:https://blog.csdn.net/zhiyangxuzs/article/details/78657235 

 

JDBC中PreparedStatement.setObject(index,Object)方法,

1、index从1开始

2、在插入时间格式的字段时,此处的Object格式必须是java.sql.Date的对象

3、Oracle表中date格式可以表示年月日时分秒

4、从表中取出对象封装到JavaBean对象中,字段类型可以直接为java.util.Date
---------------------
作者:zhiyangxuzs
来源:CSDN
原文:https://blog.csdn.net/zhiyangxuzs/article/details/78657235?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/mumu122GIS/p/9776276.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码看起来已经很简洁了,但是还是可以做一些小优化: 1. 将常量放到配置文件,方便修改和维护,比如可以使用 Properties 或者 YAML 等配置方式。 2. 在连接数据库时,可以使用连接池技术,避免频繁地创建和销毁连接,提高性能。 3. 在执行 SQL 语句时,可以使用 PreparedStatement 的批处理功能,将多个 SQL 语句一次性发送到数据库,减少网络传输开销。 4. 在处理异常时,可以考虑使用日志框架,比如 Log4j 或者 SLF4J 等,将异常信息记录到日志,方便排查问题。 5. 在关闭资源时,可以使用 try-with-resources 语句,避免手动关闭资源时出现异常导致资源没有正确关闭的问题。 以下是修改后的代码: ``` package com.neutech.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Properties; public class JdbcUtil { private static final String CONFIG_FILE = "jdbc.properties"; private static Properties properties = new Properties(); private static String driverName; private static String url; private static String username; private static String password; static { try { properties.load(JdbcUtil.class.getClassLoader().getResourceAsStream(CONFIG_FILE)); driverName = properties.getProperty("jdbc.driverName"); url = properties.getProperty("jdbc.url"); username = properties.getProperty("jdbc.username"); password = properties.getProperty("jdbc.password"); Class.forName(driverName); } catch (Exception e) { throw new RuntimeException("Failed to initialize JdbcUtil.", e); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } public static <T> List<T> executeQuery(String sql, RowMap<T> rowMap, Object... objs) throws SQLException { try (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]); } } try (ResultSet resultSet = preparedStatement.executeQuery()) { List<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) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // ignore } try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // ignore } try { if (connection != null) { connection.close(); } } catch (SQLException e) { // ignore } } } ``` 以上修改参考了 Java 开发手册和阿里巴巴 Java 开发手册的相关规范。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值