帮我优化下面这段代码:package com.ethan.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.ethan.model.Student; import com.ethan.util.DBUtil; public class StudentDao { private static Connection conn = DBUtil.getConn(); private static PreparedStatement pstmt = null; private static ResultSet rs = null; public static List<Student> findAllStudent() throws Exception { String sql = "select * from student"; pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); List<Student> list = new ArrayList<Student>(); while (rs.next()) { Student student = new Student(); student.setUserId(rs.getInt("userId")); student.setUserName(rs.getString("userName")); student.setSex(rs.getString("sex")); student.setBirthYear(rs.getString("birthYear")); student.setGrade(rs.getString("grade")); student.setCollegeName(rs.getString("collegeName")); list.add(student); } return list; } }
时间: 2024-04-15 10:27:07 浏览: 226
下面是对你提供的代码进行优化的建议:
1. 使用 try-with-resources 语句管理数据库连接和声明的资源,确保它们在使用后正确关闭,避免资源泄漏。
2. 使用 Java 8 的 Stream API 简化代码,将 ResultSet 中的数据映射到 Student 对象,并使用 Collectors.toList() 将结果收集到一个 List 中。
3. 将方法的异常抛出声明更加具体,这样调用方可以更好地处理异常。
优化后的代码如下所示:
```java
package com.ethan.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.ethan.model.Student;
import com.ethan.util.DBUtil;
public class StudentDao {
public static List<Student> findAllStudent() throws Exception {
String sql = "select * from student";
try (Connection conn = DBUtil.getConn();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
List<Student> list = new ArrayList<>();
while (rs.next()) {
Student student = new Student();
student.setUserId(rs.getInt("userId"));
student.setUserName(rs.getString("userName"));
student.setSex(rs.getString("sex"));
student.setBirthYear(rs.getString("birthYear"));
student.setGrade(rs.getString("grade"));
student.setCollegeName(rs.getString("collegeName"));
list.add(student);
}
return list;
}
}
}
```
请注意,这只是一种优化方式,具体的优化方案可能因情况而异。你可以根据实际需求进行调整和修改。
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![qdz](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)