Java 在JDBC中编写SQL查询的最佳实践是什么

Java 在JDBC中编写SQL查询的最佳实践是什么,java,sql,servlets,jdbc,Java,Sql,Servlets,Jdbc,我通过使用JSP和servlet以及JDBC开发一个电子商务Web应用程序来学习Java Web开发。我在GitHub、GoogleCode等网站上查看了一些项目,发现了一些不同寻常的代码,比如在如下界面中声明select、update、insert方法: public interface DBDriver { public void init( DBConnection connection ); public ResultSet selectQuery( String sqlStateme

我通过使用JSP和servlet以及JDBC开发一个电子商务Web应用程序来学习Java Web开发。我在GitHub、GoogleCode等网站上查看了一些项目,发现了一些不同寻常的代码,比如在如下界面中声明select、update、insert方法:

public interface DBDriver {

public void init( DBConnection connection );
public ResultSet selectQuery( String sqlStatement );
public int updateQuery( String sqlStatement );
public ResultSet select( String table, String[] fields, String where );
public ResultSet select( String table, String[] fields, String where, String[] join, String groupBy[], String having, String orderBy[], int start, int limit );
public int insert( String table, HashMap<String, String> fields );
public int update( String table, HashMap<String, String> fields, String where );
public int update( String table, HashMap<String, String> fields, String where, String orderBy[], int start, int limit );
public int delete( String table, String where );
public int delete( String table, String where, String orderBy[], int start, int limit );
public DBConnection getConnection();
我的问题是,与标准JDBC过程相比,这种方法是否被认为是一种良好的实践,如:

String sql = "select SetID,SetName,SetPrice,SetQuality from setdetails  where heroID = " + id;

        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        while (rs.next()) {

            lists.add(new Set(rs.getInt("SetID"), rs.getString("SetName"), rs.getString("SetPrice"), rs.getString("SetQuality")));
        }
        return lists;

使用
PreparedStament
时,传递原始参数没有意义。我想做一些小的修改,使其更符合
Preparedstatement

String sql = "select SetID,SetName,SetPrice,SetQuality from setdetails  where heroID = ?";

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,intVal); 

我建议您使用标准的JDBC方式(或者将SQL作为存储过程移动到数据库中,如果它足够复杂和/或用于项目的许多部分)。我已经使用连接编写了SQL语句,这种连接持续了好几页,使用DBDriver方法看起来非常糟糕。我建议您坚持让所有代码易于阅读的目标,而不要尝试任何难以阅读的代码,这样可以避免您再键入几行代码。simliar参数适用于结构不良的代码或通过修改代码来获得较小的性能增益

注意,许多SQL模板的目标是避免为每个SQL查询反复编写锅炉代码(try/catch/finally/handle异常)。您提供的示例没有做到这一点。它只帮助您构建sql语句。


我建议您使用try/catch/finally块,在该块中,您以创建连接、preparedStatement和resultSet的相反顺序关闭finally块中的连接、preparedStatement和resultSet(在关闭它们之前,首先检查它们是否为null)。

如果引发SQLException,请捕获它,并添加导致问题的记录的主键值(出于日志记录的目的)到SQLException,并重新抛出它。

对于您的普通SQL语句,不需要上述框架。您可以将查询外部化到您知道的属性文件中
String sql = "select SetID,SetName,SetPrice,SetQuality from setdetails  where heroID = " + id;

        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        while (rs.next()) {

            lists.add(new Set(rs.getInt("SetID"), rs.getString("SetName"), rs.getString("SetPrice"), rs.getString("SetQuality")));
        }
        return lists;
String sql = "select SetID,SetName,SetPrice,SetQuality from setdetails  where heroID = ?";

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,intVal);