[英]Query works in SQLDeveloper but not in JDBC
I have the following query: 我有以下查询:
SELECT MAX(ID) FROM INGRESO_SALIDA_BUQUES WHERE ID_BUQUE = 1;
which works when I execute it in SQLDeveloper. 当我在SQLDeveloper中执行它时,它起作用。 It returns one row and just one column: the biggest ID where ID_BUQUE = 1.
它返回一行,只有一列:最大的ID,其中ID_BUQUE = 1。
Now, I'm using jdbc to connect to the database and do the same operation. 现在,我正在使用jdbc连接到数据库并执行相同的操作。 Here is the code:
这是代码:
String sql = "SELECT MAX(ID) AS MAXIMO FROM INGRESO_SALIDA_BUQUES ";
sql += "WHERE ID_BUQUE = " + registroSalida.getIdBuque();
PreparedStatement prepStmt = conn.prepareStatement(sql);
recursos.add(prepStmt);
ResultSet rs = prepStmt.executeQuery();
The las line of code is the one that throws the error. las的代码行是引发错误的代码。
The result from the eclipse console (For your reference "Nombre de columna no válido" means invalid column name): eclipse控制台的结果(供您参考的“ Nombre de columna noválido”表示无效的列名):
18:40:27,712 ERROR [stderr] (default task-2) SQLException:Nombre de columna no válido
18:40:27,712 ERROR [stderr] (default task-2) java.sql.SQLException: Nombre de columna no válido
18:40:27,713 ERROR [stderr] (default task-2) at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3724)
18:40:27,713 ERROR [stderr] (default task-2) at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2799)
18:40:27,713 ERROR [stderr] (default task-2) at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:498)
I tried deleting all rows except 1, in order to see hat would happen if I change MAX(ID) TO ID . 我尝试删除除1之外的所有行,以查看如果将MAX(ID)更改为ID会发生问题。 This query works.
该查询有效。 I tried again MAX(ID) with just one row and it failed again.
我仅用一行再次尝试了MAX(ID),但再次失败了。
The table has was created with this code: 该表已使用以下代码创建:
CREATE TABLE INGRESO_SALIDA_BUQUES (
ID NUMBER,
ID_BUQUE NUMBER NOT NULL,
FECHA_INGRESO DATE NOT NULL,
FECHA_SALIDA DATE,
CONSTRAINT PK_ISB PRIMARY KEY (ID),
CONSTRAINT FK_ISB_ID_BUQUE FOREIGN KEY (ID_BUQUE) REFERENCES BUQUE(ID));
Do you know how to solve this? 你知道如何解决吗?
recursos is just just an ArrayList: recursos只是一个ArrayList:
private ArrayList<Object> recursos;
And in the constructor I initialize it. 然后在构造函数中将其初始化。 recursos = new ArrayList();
recursos = new ArrayList();
I use it to save the resources necesary to the SQL statement, in order to close them later. 我用它来保存SQL语句所需的资源,以便以后关闭它们。
The method to close them is this: 关闭它们的方法是这样的:
public void cerrarRecursos() {
for(Object ob : recursos){
if(ob instanceof PreparedStatement)
try {
((PreparedStatement) ob).close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The stack trace shows the error is coming from 堆栈跟踪显示错误来自
oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:498)
... so it's being raised from an rs.getString()
call, not the prepStmt.executeQuery()
you think it's coming from. ...因此它是通过
rs.getString()
调用而不是您认为它来自的prepStmt.executeQuery()
引发的。
Since you said it works if you change you query to get ID
instead of MAX(ID)
, code you haven't shown is doing rs.getString("ID")
- otherwise it would still have failed after that change. 既然您说过,如果您更改查询以获取
ID
而不是MAX(ID)
,那么它是rs.getString("ID")
,那么您未显示的代码正在执行rs.getString("ID")
-否则更改后它仍然会失败。 If you use a column name in the getString
then it has to match the column alias from the query. 如果在
getString
使用列名,则它必须与查询中的列别名匹配。
You can either change your result set handling to do rs.getString("MAXIMO")
to use the alias you've shown in your query, or rs.getString(1)
to use positional notation; 您可以将结果集处理更改为使用
rs.getString("MAXIMO")
来使用查询中显示的别名,也可以使用rs.getString(1)
来使用位置表示法。 or change the query alias from MAXIMO
to ID
. 或将查询别名从
MAXIMO
更改为ID
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.