JDBC中的命名参数 [英] Named parameters in JDBC
本文介绍了JDBC中的命名参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JDBC中是否有命名参数而不是位置参数,例如ADO中的 @name
, @city
下面的.NET查询?
Are there named parameters in JDBC instead of positional ones, like the @name
, @city
in the ADO.NET query below?
select * from customers where name=@name and city = @city
推荐答案
JDBC不支持命名参数。除非你一定要使用普通的JDBC(这会导致痛苦,让我告诉你),我建议使用Springs Excellent JDBCTemplate,它可以在没有整个IoC容器的情况下使用。
JDBC does not support named parameters. Unless you are bound to using plain JDBC (which causes pain, let me tell you that) I would suggest to use Springs Excellent JDBCTemplate which can be used without the whole IoC Container.
NamedParameterJDBCTemplate 支持命名参数,您可以像这样使用它们:
NamedParameterJDBCTemplate supports named parameters, you can use them like that:
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);
这篇关于JDBC中的命名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文