81

JDBC 中是否有命名参数而不是位置参数,例如下面的 ADO.NET 查询中的@name, ?@city

select * from customers where name=@name and city = @city
4

5 回答 5

82

JDBC 不支持命名参数。除非你一定要使用普通的 JDBC(这会导致痛苦,让我告诉你),否则我建议使用 Springs 优秀的 JDBCTemplate,它可以在没有整个 IoC 容器的情况下使用。

NamedParameterJDBCTemplate支持命名参数,你可以像这样使用它们:

 NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);

 MapSqlParameterSource paramSource = new MapSqlParameterSource();
 paramSource.addValue("name", name);
 paramSource.addValue("city", city);
 jdbcTemplate.queryForRowSet("SELECT * FROM customers WHERE name = :name AND city = :city", paramSource);
于 2010-02-22T09:40:58.000 回答
35

为了避免包含大型框架,我认为一个简单的自制类可以解决问题。

处理命名参数的类示例:

public class NamedParamStatement {
    public NamedParamStatement(Connection conn, String sql) throws SQLException {
        int pos;
        while((pos = sql.indexOf(":")) != -1) {
            int end = sql.substring(pos).indexOf(" ");
            if (end == -1)
                end = sql.length();
            else
                end += pos;
            fields.add(sql.substring(pos+1,end));
            sql = sql.substring(0, pos) + "?" + sql.substring(end);
        }       
        prepStmt = conn.prepareStatement(sql);
    }

    public PreparedStatement getPreparedStatement() {
        return prepStmt;
    }
    public ResultSet executeQuery() throws SQLException {
        return prepStmt.executeQuery();
    }
    public void close() throws SQLException {
        prepStmt.close();
    }

    public void setInt(String name, int value) throws SQLException {        
        prepStmt.setInt(getIndex(name), value);
    }

    private int getIndex(String name) {
        return fields.indexOf(name)+1;
    }
    private PreparedStatement prepStmt;
    private List<String> fields = new ArrayList<String>();
}

调用类的示例:

String sql;
sql = "SELECT id, Name, Age, TS FROM TestTable WHERE Age < :age OR id = :id";
NamedParamStatement stmt = new NamedParamStatement(conn, sql);
stmt.setInt("age", 35);
stmt.setInt("id", 2);
ResultSet rs = stmt.executeQuery();

请注意,上面的简单示例不处理使用命名参数两次。它也不处理使用引号内的 : 符号。

于 2013-12-17T21:10:46.230 回答
25

Vanilla JDBC 仅支持 a 中的命名参数CallableStatement(例如setString("name", name)),即便如此,我怀疑底层存储过程实现必须支持它。

如何使用命名参数的示例:

//uss Sybase ASE sysobjects table...adjust for your RDBMS
stmt = conn.prepareCall("create procedure p1 (@id int = null, @name varchar(255) = null) as begin "
        + "if @id is not null "
        + "select * from sysobjects where id = @id "
        + "else if @name is not null "
        + "select * from sysobjects where name = @name "
        + " end");
stmt.execute();

//call the proc using one of the 2 optional params
stmt = conn.prepareCall("{call p1 ?}");
stmt.setInt("@id", 10);
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
    System.out.println(rs.getString(1));
}


//use the other optional param
stmt = conn.prepareCall("{call p1 ?}");
stmt.setString("@name", "sysprocedures");
rs = stmt.executeQuery();
while (rs.next())
{
    System.out.println(rs.getString(1));
}
于 2010-02-22T09:55:15.103 回答
1

普通 JDBC 不支持命名参数。

如果您使用的是 DB2,那么直接使用 DB2 类:

  1. 将命名参数标记与 PreparedStatement 对象一起使用
  2. 将命名参数标记与 CallableStatement 对象一起使用
于 2011-10-29T17:29:13.647 回答
1

您不能在 JDBC 本身中使用命名参数。您可以尝试使用 Spring 框架,因为它有一些扩展允许在查询中使用命名参数。

于 2010-02-22T09:42:32.897 回答