http://www.meiriyouke.net/?p=258
public int insertTable(LabelForm f) throws SQLException,ParseException
{
String content = f.getSiteId();
final String sql = "insert into TAG_INFO(SITE_ID,NAME,CONTENT) values(?,?,'"+content+"')";
KeyHolder keyHolder = new GeneratedKeyHolder();
getJdbcTemplate().update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException
{
PreparedStatement ps = getJdbcTemplate().getDataSource()
.getConnection().prepareStatement(sql,new String[]{ "SITE_ID" ,"NAME"});
ps.setString(1, "站点号");
ps.setString(2, "我的名字");
return ps;
}
}, keyHolder);
System.out.println("自动插入id============================" + keyHolder.getKey().intValue());
return keyHolder.getKey().intValue();
}
本文介绍了一个使用Java进行数据库操作的方法,具体实现了向TAG_INFO表中批量插入数据的功能。通过PreparedStatement预编译语句来提高SQL执行效率,并利用KeyHolder获取自动生成的主键。
384

被折叠的 条评论
为什么被折叠?



