解释一下public List<Products> queryAll() { Connection connection = null; PreparedStatement pst = null; ResultSet rSet = null; List<Products> productsList= new ArrayList<Products>(); try { connection = DButil.getConnection(); String sql = "select * from products"; pst = connection.prepareStatement(sql); rSet = pst.executeQuery(); while(rSet.next()){ String id = rSet.getString("id"); String category = rSet.getString("category"); String description = rSet.getString("description"); String imgurl = rSet.getString("imgurl"); String name = rSet.getString("name"); int pnum = rSet.getInt("pnum"); double price = rSet.getDouble("price"); Products products = new Products(id, name, price, category, pnum, imgurl, description); productsList.add(products); } } catch (Exception e) { e.printStackTrace(); }finally{ DButil.close( rSet,pst,connection); } return productsList; }
时间: 2024-01-23 07:01:35 浏览: 79
这段代码是一个查询所有产品信息的方法,返回一个 List<Products> 对象。首先,它获取数据库连接,然后执行 SQL 查询语句,将查询结果存储到 ResultSet 对象中。接着,通过 while 循环遍历 ResultSet 对象,将每一行记录转换成 Products 对象,并将其添加到列表中。最后,关闭 ResultSet、PreparedStatement 和 Connection 对象,返回产品列表。这段代码使用了 JDBC API 进行数据库操作。
相关问题
帮我优化这段代码,谢谢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(); } }
这段代码看起来已经很简洁了,但是还是可以做一些小优化:
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 开发手册的相关规范。
阅读全文