0
    public static void baglanti(){

    OracleDataSource ods;
    //String driverName = "oracle.jdbc.driver.OracleDriver";

    try {
        ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:@//aaaaaa:2122/XXXXX");
        ods.setUser("aaaa");
        ods.setPassword("aaaa");

        Connection conn=ods.getConnection();
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("SELECT DESCRIPTION FROM example");

        while (rs.next()) {
            //rs.getString("ID");
            System.out.println("DESC : "+rs.getString("1"));

        }   

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

I have an error my connection java from to oracle.user name ,password are correct but I couldn't connect to db. Thanks.

2
  • Your query is to select the column DESCRIPTION but you try to get the columns ID(commented) and 1 from the ResultSet which is why you're getting the error I suppose. Commented Jun 16, 2014 at 7:05
  • Read Error StackTrace which will have column name which is invalid. Commented Jun 16, 2014 at 7:07

2 Answers 2

2

The way you use getString is wrong. You can pass an index or the column label:

System.out.println("DESC : "+rs.getString(1));

or

System.out.println("DESC : "+rs.getString("description"));

you pass the index as a String and that's wrong.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you say you have a connection error, try below code for connection part:

OracleDataSource ds = new OracleDataSource();
ds.setDriverType("thin");
ds.setServerName("dssw2k01");
ds.setPortNumber(1521);
ds.setDatabaseName("orcl"); // sid
ds.setUser("scott");
ds.setPassword("tiger");

If the connection is correct and the query is executed, even then you will have issues parsing the resultset unless you correct the below one as:

System.out.println("DESC : "+rs.getString(1));

OR System.out.println("DESC : "+rs.getString("DESCRIPTION"));

rs.getString(), rs.getInt() etc all have either

  1. The index as an integer parameter starting from 1

or

  1. The alias name of the resultset output as a String

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.