admin管理员组文章数量:1601628
2024年1月6日发(作者:)
JdbcTemplate 与 PreparedStatement
如果要使用preparedStatement就可以使用PreparedStatementCreator或者PreparedStatementSeter接口对于PreparedStatementCreator,重点在于从写它的createPreparedStatement方法,方法内部一般功能JDBC书写标准的。对于后者,重点在于重写setValues方法,参数是个PreparedStatement实例。二者用法举例如下:(new PreparedStatementCreator()
{public PreparedStatement
createPreparedStatement(Connection con) throws
SQLException {// 这里面创建一个PreparedStatement并返回之}}}("insert into user
values(?, ?)",new PreparedStatementSetter(){public void
setValues(PreparedStatement ps) throws
SLException{ing(1,name);(2, age);}});OK,上述写法还是太麻烦不是么?那么用数组的形式传值也是可以的。("insert into user
values(?, ?)",new Object[]{e(),
()});OK, 现在有一批用户的信息需要加入到数据库,怎么办?使用BatchPreparedStatmentSetter,用法与PreparedStatementSeter几乎一样public
voidetValues(PreparedStatement ps, int i) throws
SQLException{User user = (User)
(i);ing(1, e());(2,
().intValue());}public int getBatchSize() {return
();}
版权声明:本文标题:JdbcTemplate 与 PreparedStatement 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1704515336a94962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论