【发布时间】:2017-03-26 03:36:41
【问题描述】:
我已经手动输入查询并通过复制粘贴,它运行良好。当我对 PreparedStatement 对象进行查询时,出现错误:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][10145][10844][4.22.29] Invalid parameter 1: Parameter index is out of range. ERRORCODE=-4461, SQLSTATE=42815
我已经阅读了 PreparedStatement 对象的 JavaDoc,但我的想法很新鲜。我的其他相同查询(但使用 int 而不是 String)工作正常。有什么想法吗?
一个sn-p的代码:
String queryText = "";
PreparedStatement querySt = null;
ResultSet answers = null;
queryText = "SELECT title, year, language, weight FROM yrb_book WHERE title = '?' AND cat = '?' ";
try
{
querySt = DbConn.prepareStatement(queryText);
}
catch(SQLException e)
{
System.out.println(e);
System.exit(0);
}
// Execute the query.
try
{
querySt.setString(1, title);
querySt.setString(2, category);
answers = querySt.executeQuery();
}
下面是我正在使用的表格:
create table yrb_book (
title varchar(25) not null,
year smallint not null,
language varchar(10),
cat varchar(10) not null,
weight smallint not null,
constraint yrb_book_pk
primary key (title, year),
constraint yrb_book_fk_cat
foreign key (cat) references yrb_category,
constraint yrb_book_weight
check (weight > 0)
);
我研究了这个答案 30 分钟,但看不出它如何适用于我的案例。 Getting SQL Exception while using prepared statement for select query
【问题讨论】:
标签: java db2 sqlexception