简体   繁体   English

如何使用 Oracle 和 JDBC 获取 SQL 语句中的错误行号?

[英]How to get line number of error in SQL statement using Oracle and JDBC?

In sql developer if we type wrong query, eg delete from notablesql developer如果我们输入错误的查询,例如从显着中删除

it will show us exact line number and column它将向我们显示确切的行号和列

Error starting at line : 1 in command -
delete from notable
Error at Command Line : 1 Column : 13
Error report -
SQL Error: ORA-00942: table or view does not exist
00942. 00000 -  "table or view does not exist"
*Cause:    
*Action:

but if I do it in java code I got only但如果我用 java 代码来做,我只会得到

java.sql.SQLException: ORA-00942: table or view does not exist java.sql.SQLException: ORA-00942: 表或视图不存在

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) ~[ojdbc14-10.2.0.3.0.jar:Oracle JDBC Driver version - "10.2.0.3.0"]
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331) ~[ojdbc14-10.2.0.3.0.jar:Oracle JDBC Driver version - "10.2.0.3.0"]
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288) ~[ojdbc14-10.2.0.3.0.jar:Oracle JDBC Driver version - "10.2.0.3.0"]
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745) ~[ojdbc14-10.2.0.3.0.jar:Oracle JDBC Driver version - "10.2.0.3.0"]
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:207) ~[ojdbc14-10.2.0.3.0.jar:Oracle JDBC Driver version - "10.2.0.3.0"]

How can I obtain information about line and column?如何获取有关行和列的信息? is it possible via JDBC?可以通过JDBC吗?

I'm not aware of any third party library, which will help you to achieve this.我不知道有任何第三方库可以帮助您实现这一目标。 But, as far as I know about SQLException , you can't .但是,据我所知SQLException ,你不能

And BTW, You are getting above error, because you haven't created table/view, which you were trying to access.顺便说一句,您遇到了上述错误,因为您还没有创建您试图访问的表/视图。

SQLException.getCause() gives you the position of the character at which the exception originated: SQLException.getCause()为您提供SQLException.getCause()异常的字符的位置:

    select * from blahblahblah

    Error : 942, Position : 14, Sql = select * from blahblahblah, OriginalSql = select * from blahblahblah, Error Msg = ORA-00942: table or view does not exist

So you could parse the cause and get the character index, then compute the line number and the column number.因此,您可以解析原因并获取字符索引,然后计算行号和列号。

This goes for JDBC version 12.2.0.1 and Oracle 12c, not sure about other versions.这适用于 JDBC 版本 12.2.0.1 和 Oracle 12c,不确定其他版本。

From the Command Line, we can execute multiple SQL queries at a time.从命令行,我们可以一次执行多个 SQL 查询。 So, while execution if we get any error, command line processor gives "Line" and "Column" to help the user to know about the faulty line.因此,在执行时如果出现任何错误,命令行处理器会给出“行”和“列”以帮助用户了解错误行。 Anyways, Its a utility provided by Database.无论如何,它是数据库提供的实用程序。

However, In JDBC you can either execute a single query or execute the batch of statements.但是,在 JDBC 中,您可以执行单个查询或执行批处理语句。 In both cases you won't get any Line Number and Column Number because its not useful in any sense.在这两种情况下,您都不会得到任何行号和列号,因为它在任何意义上都没有用。 Though you can get the reason of error and code, based on your JDBC driver.虽然您可以根据您的 JDBC 驱动程序获得错误和代码的原因。

I solved this in Python by using the offset (# chars) to determine what line the error happened on.我在 Python 中通过使用偏移量(# chars)来确定错误发生在哪一行来解决这个问题。 This can be done by looping through the original sql statement and counting the lines while also keeping track of the number of characters seen so far.这可以通过循环遍历原始 sql 语句并计算行数来完成,同时还跟踪到目前为止看到的字符数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM