public int executeUpdate(String sql, List<Object> params) throws SQLException { pstmt = con.prepareStatement(sql); if (params != null && params.size() > 0) { for (int i = 0; i < params.size(); i++) { pstmt.setObject(i + 1, params.get(i)); } } return pstmt.executeUpdate(); }
时间: 2024-04-21 07:24:42 浏览: 22
这是DBBean类中的executeUpdate方法,用于执行更新操作的SQL语句并返回受影响的行数。
它接收两个参数:
- sql:要执行的SQL语句
- params:SQL语句中需要的参数列表
在方法内部,首先通过con.prepareStatement(sql)创建一个PreparedStatement对象,然后通过循环遍历params列表,使用pstmt.setObject(i + 1, params.get(i))方法将参数设置到PreparedStatement对象中。
最后,通过pstmt.executeUpdate()执行SQL语句,并返回更新的行数。
需要注意的是,该方法可能会抛出SQLException异常,需要在调用该方法时进行异常处理。
相关问题
package com.util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Random; import com.util.SQLConn; public class RandomStringGenerator { private static final String INSERT_SQL = "INSERT INTO strings (value) VALUES (?)"; private static final int STRING_LENGTH = 10; public RandomStringGenerator() throws SQLException { try {Connection conn = SQLConn.openDB(); PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL); { // 生成随机字符串 Random rand = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < STRING_LENGTH; i++) { char c = (char) (rand.nextInt(26) + 'a'); sb.append(c); } String str = sb.toString(); // 保存到数据库 pstmt.setString(1, str); pstmt.executeUpdate(); System.out.println("随机字符串已保存到数据库中:" + str); }} catch (SQLException e) { // exception handling here throw e; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { try { RandomStringGenerator generator = new RandomStringGenerator(); } catch (SQLException e) { // exception handling here e.printStackTrace(); } } }优化代码
以下是对代码的优化建议:
1. 使用 try-with-resources 语句,可以自动关闭连接和 PreparedStatement,避免资源泄漏。
2. 将 INSERT_SQL 和 STRING_LENGTH 定义为常量,可以提高代码的可读性。
3. 将 SQLConn.openDB() 方法改为静态方法,可以在不创建对象的情况下直接调用。
4. 在异常处理中,可以将异常信息记录在日志中,方便后续排查问题。
下面是优化后的代码:
```java
package com.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class RandomStringGenerator {
private static final String INSERT_SQL = "INSERT INTO strings (value) VALUES (?)";
private static final int STRING_LENGTH = 10;
public RandomStringGenerator() {
try (Connection conn = SQLConn.openDB();
PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL)) {
// 生成随机字符串
Random rand = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < STRING_LENGTH; i++) {
char c = (char) (rand.nextInt(26) + 'a');
sb.append(c);
}
String str = sb.toString();
// 保存到数据库
pstmt.setString(1, str);
pstmt.executeUpdate();
System.out.println("随机字符串已保存到数据库中:" + str);
} catch (SQLException e) {
// 记录异常信息
System.err.println("保存随机字符串到数据库时出现异常:" + e.getMessage());
} catch (ClassNotFoundException e) {
// 记录异常信息
System.err.println("无法找到数据库驱动程序:" + e.getMessage());
}
}
public static void main(String[] args) {
new RandomStringGenerator();
}
}
```
package com.util; import java.sql.Connection; import java.util.Random; import java.sql.*; import com.util.SQLConn; public class RandomStringGenerator { public RandomStringGenerator() throws SQLException{ // 构造函数中调用可能会抛出SQLException异常的方法 // 添加异常处理语句 try{ final String UPDATE_SQL = "INSERT INTO strings (value) VALUES (?)"; try { Connection conn = SQLConn.openDB(); Class.forName("com.sqlserver.jdbc.Driver"); // 生成随机字符串 Random rand = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 5; i++) { char c = (char) (rand.nextInt(26) + 'a'); sb.append(c); } String str = sb.toString(); // 保存到数据库 PreparedStatement pstmte = conn.prepareStatement(UPDATE_SQL); { pstmte.setInt(1, 1); pstmte.executeUpdate(); } System.out.println("随机字符串已保存到数据库中:" + str); pstmte.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }catch(SQLException e){ // exception handling here throw e; } }优化代码
下面是对代码的一些优化:
1. 删除了不必要的 try-catch 语句,因为异常应该在最外层进行处理,不需要在方法内部处理。
2. 添加了 try-with-resources 语句用于自动管理资源的关闭。
3. 将生成随机字符串的长度设置为一个可调整的参数,使其更具有灵活性。
4. 修改了 PreparedStatement 中的参数设置,将 int 类型改为了 String 类型。
经过这些优化,代码变得更加简洁和易于维护:
```
package com.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class RandomStringGenerator {
private static final String INSERT_SQL = "INSERT INTO strings (value) VALUES (?)";
private static final int STRING_LENGTH = 10;
public RandomStringGenerator() throws SQLException {
try (Connection conn = SQLConn.openDB();
PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL)) {
// 生成随机字符串
Random rand = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < STRING_LENGTH; i++) {
char c = (char) (rand.nextInt(26) + 'a');
sb.append(c);
}
String str = sb.toString();
// 保存到数据库
pstmt.setString(1, str);
pstmt.executeUpdate();
System.out.println("随机字符串已保存到数据库中:" + str);
} catch (SQLException e) {
// exception handling here
throw e;
}
}
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)