java JDBC中的命名参数[重复]

6yt4nkrj  于 2023-02-11  发布在  Java
关注(0)|答案(5)|浏览(131)
    • 此问题在此处已有答案**:

Using a variable instead of a parameter index with a JDBC prepared statement(3个答案)
两年前关闭了。
JDBC中是否有命名参数而不是位置参数,如下面ADO.NET查询中的@name@city

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

rmbxnbpk1#

JDBC不支持命名参数。除非您绑定使用普通JDBC(这会带来痛苦,让我告诉您),否则我建议使用Springs Excellent 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);
ttcibm8c

ttcibm8c2#

为了避免包含一个大的框架,我认为一个简单的自制类就可以做到这一点。
处理命名参数的类示例:

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();

请注意,上面的简单示例不处理两次使用命名参数的情况,也不处理使用以下参数的情况:在引号内签名。

vcirk6k6

vcirk6k63#

VanillaJDBC只支持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));
}
yks3o0rb

yks3o0rb4#

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

相关问题