Explore Courses Blog Tutorials Interview Questions
0 votes
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?


import java.sql.*;

class OracleCon {

  public static void main(String args[]) {

    try {

        // step1 load the driver class


        // step2 create the connection object

        Connection con = DriverManager.getConnection(

                "jdbc:oracle:thin:@localhost:1521:xe", "system",



         * 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 (

            System.out.println(rs.getInt(1) + "  " + rs.getString(2) + "  "

                    + rs.getString(3));

        // step5 close the connection object


    } catch (Exception e) {






java.sql.SQLException: Fail to convert to internal representation 

  at oracle.jdbc.driver.DatabaseError.throwSqlException( 

  at oracle.jdbc.driver.DatabaseError.throwSqlException( 

  at oracle.jdbc.driver.DatabaseError.throwSqlException( 

  at oracle.jdbc.driver.CharCommonAccessor.getInt( 

  at oracle.jdbc.driver.OracleResultSetImpl.getInt( 

  at dbPrograms.OracleCon.main( 

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