我给出了一个从Java应用程序连接到Oracle8 Release 8.0.5.0.0的任务。我的代码片段如下。
import java.sql.*;
class OracleConOldJava {
public static void main(String args[]) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Load the Oracle JDBC driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Establish the database connection
con = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.1.3:1521/DBNAME", "USERNAME", "PASSWORD");
// Create a statement
stmt = con.createStatement();
// Execute the SQL query with FETCH FIRST to limit the number of rows
rs = stmt.executeQuery(
"SELECT COL1, COL2, COL3 FROM TABLE_A FETCH FIRST 5 ROWS ONLY");
// Iterate through the result set and print the data
while (rs.next()) {
System.out.println(
rs.getInt("COL") + " " +
rs.getString("COL2") + " " +
rs.getString("COL3"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
// Close the resources in the reverse order of acquisition
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
我在java 1.4中使用ojdbc14.jar。
当我运行下面的代码得到错误。
C:\j2sdk1.4.2_18\bin\java.exe -cp .;ojdbc14.jar OracleConOldJava
java.sql.SQLException: ORA-00604: error occurred at recursive SQL level 1
ORA-02248: invalid option for ALTER SESSION
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:283)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:278)
at oracle.jdbc.driver.T4CTTIoauthenticate.receiveOauth(T4CTTIoauthenticate.java:785)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:362)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at OracleConOldJava.main(OracleConOldJava.java:14)
我用java 1.8和ojdbc8.jar,java 1.4和ojdbc14.jar。发生了相同的错误。这可能是什么原因呢?是因为驱动程序不匹配吗?如果是这样,我必须使用的驱动程序是什么?
2条答案
按热度按时间p1tboqfb1#
你有(至少)两个问题:
1.错误消息为:
这意味着,当您创建连接并且Oracle隐式启动会话时,将调用
ALTER SESSION
命令,而该命令无效。您没有显示任何ALTER SESSION
命令,因此它可能位于触发器中。您需要修复该命令,使其有效。FETCH FIRST n ROWS ONLY
语法在Oracle 12中引入。你正在使用Oracle 8,所以它不会工作。相反,您需要:但是,您可能还需要一个
ORDER BY
子句(否则您将得到5个随机行,而不是5个特定行)。mzmfm0qo2#
设法用Java 1.2和classes12.zip修复