Oracle DB 查询在 sqlDev 中运行,但不在 Java 程序中运行

发布于 2024-11-29 19:44:34 字数 1526 浏览 4 评论 0原文

我一直在搞乱从我的 JAVA 应用程序运行的 Oracle DB 查询。我可以成功地让它们全部在 SQL Developer 中运行。但是,当我尝试从 JAVA 应用程序执行它们时,我通常会在某些查询上收到 UpdatadbleResultSet Error/Exception

另外,有时我会收到ExhaustedResultset。正如我在底部提到的,我将重新解决问题以将其分解(当我有机会时)。我继续编辑,很快就会成为一本书。

这是为什么呢?我似乎无法查明问题所在。

一些查询成功运行,例如:

SELECT table_name 
FROM all_tables

SELECT column_name, data_length 
FROM all_tab_columns 
WHERE table_name = 'mytable'

但是当我尝试运行类似的东西时

SELECT length(<myColumnName>) 
FROM mytable 

,我收到 updateableResultSetError

我正在将查询作为单击按钮时调用的方法来运行(下面的示例)。

static void testQuery() {   
 String query = "SELECT blah from blah"
 String length;
 ResultSet rs = db.runQuery(query);
 Length = rs.getString("length(myCol)")
 System.out.println(length);
}

我也尝试过 while rs.next()

我只能认为由于某种原因我无法进入每个表,我只能拉出“更大”的图片。

编辑:解释数据库连接

我正在使用已添加到我的项目中的其他一些 jar 文件进行连接。

private static IDriver driver = null;
private static Database db = null;

然后,我在一个单独的方法中传递所有连接凭据。

private void connectDB(){
driver = new OracleDriver();
db = new Database(driver)   
driver.getPassword;
driver.getetc;
driver.getEtc;
}

编辑:

当我 getstacktrace 时,我返回的是。

Ljava.lang.StatckTraceElement;(assortment of random characters).

我可能无法正确获取堆栈跟踪,因此有人可以填写我的信息。毕竟我提供了赏金。

另外,当我有时间时,我会编辑这个问题并再次分解。

I have been messing with Oracle DB queries that run from my JAVA app. I can successfully get them all to run in SQL Developer. But when I am trying to execute them from my JAVA app I usually get UpdatadbleResultSet Error/Exception on certain queries.

Also, sometimes I receive, ExhaustedResultset. As I mention at the bottom I will re work the question to break it down(When I get a chance). I keep editing and pretty soon it'll be a book.

Why is this? I cannot seem to pinpoint the problem.

Some queries run successfully such as:

SELECT table_name 
FROM all_tables

SELECT column_name, data_length 
FROM all_tab_columns 
WHERE table_name = 'mytable'

But when I try and run something like

SELECT length(<myColumnName>) 
FROM mytable 

I get the updateableResultSetError

I am running my queries as methods called on button clicks (example below).

static void testQuery() {   
 String query = "SELECT blah from blah"
 String length;
 ResultSet rs = db.runQuery(query);
 Length = rs.getString("length(myCol)")
 System.out.println(length);
}

I have also tried while rs.next()

I can only think that for some reason I am unable to get into each table and I can only pull the "bigger" picture.

EDIT: Explained DB Connection

I am connecting using some other jarfiles that have been added to my project.

private static IDriver driver = null;
private static Database db = null;

I then pass in all my connection credentials in a separate method.

private void connectDB(){
driver = new OracleDriver();
db = new Database(driver)   
driver.getPassword;
driver.getetc;
driver.getEtc;
}

EDIT:

When I getstacktrace all I am returning is.

Ljava.lang.StatckTraceElement;(assortment of random characters).

I may not be getting stack traces right so someone can fill me in. After all I am offering a bounty.

Also I will edit this question and break it down again when I have the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强辩 2024-12-06 19:44:34

您的问题是您尝试更新无法更新的查询,因此出现可更新结果错误。似乎无论谁创建数据库连接或执行查询都正在创建可更新的结果集。

您不能在可更新结果集中使用某些类型的 select:不能使用聚合函数(例如 length、min、max),不能使用 select * 等。)

有关完整列表,请参阅 结果集限制和降级规则

Your problem is that you're trying to update a query that can't be updated, hence the updateable result error. It seems that whoever is creating your database connection or executing your query is creating an updatable result set.

You can't use certain types of select in an updatable result set: you can't use aggregated functions (such as length, min, max), you can't use select * etc.)

For the full list see Result Set Limitations and Downgrade Rules

染年凉城似染瑾 2024-12-06 19:44:34

尝试通过 columnIndex 而不是列名检索 select 语句中的值,看看是否有区别。

目前,很难判断您的 db.runQuery() 做了什么,因为该代码尚未发布。

String query = "SELECT length(myCol) FROM myTable";
String length;
ResultSet rs = db.runQuery(query);

while (rs.next()) {
 length = rs.getString(1);
 System.out.println(length);
}

Try retrieving the value in your select statement via the columnIndex instead of the column name and see if that makes a difference.

Currently, its hard to tell what your db.runQuery() does since that code is not posted.

String query = "SELECT length(myCol) FROM myTable";
String length;
ResultSet rs = db.runQuery(query);

while (rs.next()) {
 length = rs.getString(1);
 System.out.println(length);
}
汹涌人海 2024-12-06 19:44:34

我已经知道这里可能会发生什么(这可以解释为什么有些查询有效,而有些则无效)。根据jdbc ResultSet javadocs,使用结果集的getString()方法时,列标签。

使用 SQL AS 子句指定的列的标签。
如果未指定 SQL AS 子句,则标签是列的名称

因为“length(myCol)”既不是标签也不是列名,因此可能会因此而失败(但如果没有堆栈跟踪,则它是很难说出你的问题到底是什么)。

尝试

String query = "SELECT length(myCol) AS myCol_len FROM myTable"
ResultSet rs = db.runQuery(query);
String length = rs.getString("myCol_len");

虽然你确定,但你不想:

int length = rs.getInt("myCol_len");

或者(如 Kal 所写),你可以使用列索引从结果集中获取数据,这消除了对 SQL AS 标签的需要:

String query = "SELECT length(myCol) FROM myTable"
ResultSet rs = db.runQuery(query);
String length = rs.getString(1);

I've got an inkling what may be happening here (which would explain why some queries work, and some don't). Accoring to the jdbc ResultSet javadocs, when using the getString() method of the result set, the column label.

the label for the column specified with the SQL AS clause.
If the SQL AS clause was not specified, then the label is the name of the column

As "length(myCol)" is neither a label nor a column name, it may be that it fell over because of that (but without stacktrace it is difficult to say what your problem actually is).

Try

String query = "SELECT length(myCol) AS myCol_len FROM myTable"
ResultSet rs = db.runQuery(query);
String length = rs.getString("myCol_len");

Though are you sure, you didn't want:

int length = rs.getInt("myCol_len");

Alternatively (as written by Kal), you can use the column index to get the data from the result set, which oblivates the need for a SQL AS label:

String query = "SELECT length(myCol) FROM myTable"
ResultSet rs = db.runQuery(query);
String length = rs.getString(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文