Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (7k points)

I am using standard JDBC code for the JAVA-ORACLE connection but it is not working.

I am not able to debug the code. Can someone help me with this?

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) 

1 Answer

0 votes
by (13.1k points)

You have selected the 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 are selecting one column and access three columns in ResultSet so you might want to adjust the select command.

Want to learn Java? Check out the core Java course from Intellipaat.

Browse Categories

...