使用标准JDBC代码进行JAVA-ORACLE连接但不起作用。
这可能会提前询问,但我自己无法调试,所以在这里问。 请求帮助。
代码:
import java.sql.*;
class OracleCon {
public static void main(String args[]) {
try {
// step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
// step2 create the connection object
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system",
"nikhil.nik");
/*
* Hostname: Host system for the Oracle database. For your Express
* Edition database, the hostname is localhost. Port: Listener port.
* The default is 1521. SID: Database name. The default for Express
* Edition is xe.
*/
// step3 create the statement object
Statement stmt = con.createStatement();
// step4 execute query
ResultSet rs = stmt.executeQuery("SELECT TEST_NAME FROM TEST1");
// System.out.println(rs);
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " "
+ rs.getString(3));
// step5 close the connection object
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Stacktrace:
java.sql.SQLException: Fail to convert to internal representation
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.CharCommonAccessor.getInt(CharCommonAccessor.java:132)
at oracle.jdbc.driver.OracleResultSetImpl.getInt(OracleResultSetImpl.java:521)
at dbPrograms.OracleCon.main(OracleCon.java:31)
答案 0 :(得分:2)
您选择了列TEST_NAME
并将其值提取为int
。此操作失败,因为Oracle无法相应地转换值。使用适合列类型的ResultSet
方法。
您还可以选择一列并访问ResultSet
中的三列,以便您可能需要调整选择命令。