private void insertIntoMyTable (Myclass m) { String query = "INSERT INTO MYTABLE (NAME) VALUES (?)"; jdbcTemplate.update(query,m.getName()); }
解决方法
检查
this reference.您可以使用jdbcTemplate.update作为:
编辑
按要求添加导入
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.sqlException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder;
final String INSERT_sql = "insert into my_test (name) values(?)"; final String name = "Rob"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update( new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws sqlException { PreparedStatement ps = connection.prepareStatement(INSERT_sql,new String[] {"id"}); ps.setString(1,name); return ps; } },keyHolder); // keyHolder.getKey() now contains the generated key