admin管理员组

文章数量:1585963

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

();}

本文标签: 方法重点使用重写在于