1

Using standard JDBC code for JAVA-ORACLE connection however not working.

This may be asked earlier but i am not able to debug it myself so asking here. Request for help.

Code:

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) 
nik
  • 85
  • 2
  • 13
  • You select a single column but access three columns in the result. – wero Apr 08 '16 at 10:36
  • Show the full stacktrace with `e.printStackTrace();`. Your way loses all the important information in the exception. – Kayaman Apr 08 '16 at 10:36
  • posting the 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) – nik Apr 08 '16 at 10:39
  • @NikhilKumar then the column `TEST_NAME` is not a `int` – wero Apr 08 '16 at 10:40
  • Thanks everyone, the problem is resolved. – nik Apr 08 '16 at 10:43

1 Answers1

2

You selected column TEST_NAME and extracted its values as int. This operation fails because Oracle cannot convert the values accordingly. Use the ResultSet method suitable for the column type.

Also you select one column and access three columns in the ResultSet so you might want to adjust the select command.

wero
  • 32,544
  • 3
  • 59
  • 84
  • Hi Wero, one more doubt. Here in the code I am not selecting my 'Emloyee' database. So, how to select the database. Suppose if I have same table in two databases then how to retrieve the values.'xe' is the SID. – nik Apr 08 '16 at 10:55
  • @NikhilKumar not an expert in oracle. Guess you specify the database in the connection url as you already did. http://stackoverflow.com/questions/4832056/java-jdbc-how-to-connect-to-oracle-using-service-name-instead-of-sid might help. – wero Apr 08 '16 at 11:01