java.sql.SQLException:一般错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9856719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:22:46  来源:igfitidea点击:

java.sql.SQLException: General error

javajdbcoracle10godbcdatabase-connection

提问by Touchstone

Getting java.sql.SQLException

得到 java.sql.SQLException

java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6986)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3110)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:253)
at com.test.Temp.main(Temp.java:29)

I am using following code

我正在使用以下代码

Connection con=null;
ResultSet rs=null;
Statement stmt=null;

try {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  con=DriverManager.getConnection("jdbc:odbc:locator","locator","locator");

  stmt=con.createStatement();
  System.out.println("Before query");
  String query=null;
  query="select * from user_location_table";
  System.out.println("after query12");

  rs=stmt.executeQuery(query);
  //perform certain operation....
  rs.close();
  stmt.close();
  con.close();
} catch(Exception e) {
  e.printStackTrace();
}

The exception is thrown at stmt.executeQuery(query).

在 处抛出异常stmt.executeQuery(query)

user_location_table contains following fields

user_location_table 包含以下字段

user_id:number  not null,
latitude:number,
longitude:number,
update_time:timestamp(6)

Thanks in advance

提前致谢

采纳答案by Touchstone

I get it.

我得到它。

The error is thrown due to the use of datatype timestamp(6) in update_time.The exception is thrown whenever we try to execute the select statement containing a column with timestamp as the datatype.

由于在update_time 中使用了数据类型timestamp(6) 引发了该错误。每当我们尝试执行包含以timestamp 作为数据类型的列的select 语句时,就会抛出异常。

Instead of the previous code we can use the following code for selecting

我们可以使用以下代码代替之前的代码进行选择

    query="select latitude,longitude,to_char(update_time,'HH24:MI:SS'),to_char(update,time,'DD-MON-YY') from user_location_table";    

This works fine,I have tested it.

这很好用,我已经测试过了。

Cheers!!

干杯!!

回答by DavidB

A couple of thoughts. If it's only happening on the first execute, it may be that there is something invalid in the db and it's being compiled. Secondly, you may have something leaking because you aren't closing your connection and statement in a finally block. So if you get an exception, you aren't closing the connections and potentially you're creating a lock on your DB.

一些想法。如果它仅在第一次执行时发生,则可能是数据库中存在无效内容并且正在编译。其次,您可能会泄漏一些东西,因为您没有在 finally 块中关闭连接和语句。因此,如果您遇到异常,则不会关闭连接,并且可能会在您的数据库上创建锁。

Add a finally block and move your close statements there checking for null:

添加一个 finally 块并将您的 close 语句移到那里检查空值:

try {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  con=DriverManager.getConnection("jdbc:odbc:locator","locator","locator");

  stmt=con.createStatement();
  System.out.println("Before query");
  String query=null;
  query="select * from user_location_table";
  System.out.println("after query12");

  rs=stmt.executeQuery(query);
  //perform certain operation....

} catch (Exception e) {
  e.printStackTrace();
}
finally {
  try {
    if (rs!=null)
      rs.close();
  } catch (SQLException e) {
    e.printStackTrace();
  }
  try {
      if (stm!=null)
        stmt.close();
      } catch (SQLException e) {
      e.printStackTrace();
    }
  try {
    if (con!=null)
      con.close();
  } catch (SQLException e) {
    e.printStackTrace();
  }
}