我的代码在下面根据用户输入查询数据库中的一组行。我已经尝试并测试了SQL Developer中的查询,它可以很好地返回正确的行。输入的示例是:2013-01-22
但由于某种原因,在java内部我收到了这个错误:
java.sql.SQLException: Invalid column index
控制台指示它在此处开火:
preparedStatement.setString(1, to);
完整的连接代码:
ResultSet rs = null;
PreparedStatement preparedStatement = null;
try {
String strQuery =
"SELECT homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft,"
+ " listagg(features.feature_name, '\n') WITHIN GROUP(ORDER BY features.feature_name) features, home_type.type_name"
+ " FROM homes"
+ " INNER JOIN bookings ON bookings.home_id <> homes.home_id"
+ " INNER JOIN home_feature ON homes.home_id = home_feature.home_id"
+ " INNER JOIN home_type ON home_type.type_code = homes.type_code"
+ " INNER JOIN features ON home_feature.feature_id = features.feature_id"
+ " WHERE bookings.booking_end < date '?'"
+ " OR bookings.booking_start > date '?'"
+ " GROUP BY homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft, home_type.type_name";
preparedStatement = conn.prepareStatement(strQuery);//prepare the statement
preparedStatement.setString(1, to);//insert
preparedStatement.setString(2, from);//insert
rs = preparedStatement.executeQuery();//execute query
完整堆栈跟踪如下所示:
SEVERE: java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5317)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5305)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:248)
at DB.DatabaseConnector.getPropertiesSearch(DatabaseConnector.java:258)
at DB.SearchServlet.doPost(SearchServlet.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
SEVERE: at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5317)
SEVERE: at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5305)
SEVERE: at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:248)
SEVERE: at DB.DatabaseConnector.getPropertiesSearch(DatabaseConnector.java:258)
SEVERE: at DB.SearchServlet.doPost(SearchServlet.java:47)
SEVERE: at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
SEVERE: at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
SEVERE: at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
SEVERE: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
SEVERE: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE: at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE: at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE: at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
SEVERE: at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
SEVERE: at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
SEVERE: at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE: at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE: at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE: at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE: at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE: at java.lang.Thread.run(Thread.java:722)
我不太清楚为什么错误会被抛出,因为查询工作正常是SQL开发人员。我正在使用Oracle
编辑:
结果集在同一方法中读入。然后传回homes对象:
HomeList homes = new HomeList();
homes = new HomeList(rs);
构造函数如下所示:
public HomeList(ResultSet rs) {
this();
try {
while (rs.next()) {
list.add(new Home(rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getInt(4),
rs.getInt(5),
rs.getInt(6),
rs.getInt(7),
rs.getInt(8),
rs.getString(9),
rs.getString(10)
));
}//end while
} catch (SQLException e) {
e.printStackTrace();
}//end try
}
答案 0 :(得分:30)
查询字符串中的所有位置,通配符应为?
而不是'?'
。这应该可以解决问题。
编辑:
要添加,您需要将date '?'
更改为to_date(?, 'yyyy-mm-dd')
。请尝试一下,让我知道。
答案 1 :(得分:2)
正如@TechSpellBound建议删除周围的报价?迹象。然后在连接字符串的每一行末尾添加一个空格字符。否则整个查询将作为示例发送(仅使用其中的一部分作为示例):.... WHERE bookings.booking_end < date ?OR bookings.booking_start > date ?GROUP BY ....
?
和OR
需要用空格字符分隔。在查询字符串中的任何需要的位置执行。
答案 2 :(得分:0)
在date '?'
中,'?'
是一个值为?
的文字字符串,而不是参数占位符,因此您的查询没有任何参数。 date
是从(文字)字符串到目前为止的简写。您需要将date '?'
替换为?
以实际拥有参数。
此外,如果您知道这是日期,请使用setDate(..)
而不是setString(..)
来设置参数。