PreparedStatementCallback; bad SQL grammar [update person set name=? where age=?];
nested exception is java.sql.SQLException: ORA-00900: 无效 SQL 语句
错误原因:
用错方法:
int row=jt.queryForList(sql2,values2);
正确代码:
int row=jt.update(sql2,values2);
原代码:
package test; import java.math.BigDecimal; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.sql.DataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import entity.Person; public class TestJdbcTemplate { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource=(DataSource) context.getBean("dataSource"); JdbcTemplate jt=new JdbcTemplate(dataSource); //查询sql语句 String sql="select * from person where name=? and age=?"; //执行带有参数的查询语句 Object[] values={"张三",23}; List result=jt.queryForList(sql,values); Iterator it=result.iterator(); while(it.hasNext()){ System.out.println((Map)it.next()); } //更新语句: String sql2="update person set name= ? where age= ?"; Object[] values2={"zhangxiaosan",30}; //错误语句 //int row=jt.queryForList(sql2,values2); int row=jt.update(sql2,values2); System.out.println("受影响的行数"+row); //执行DDL语句 jt.execute("create table Test(test varchar(200))"); System.out.println("建表完毕"); } }