Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException:

本文详细解析了使用Spring JdbcTemplate过程中遇到的BadSqlGrammarException异常,通过修改查询语句和更新语句的调用方式,成功解决了ORA-00900:无效SQL语句的问题,并提供了正确的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException:
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("建表完毕");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值