-2

I have the following code:

package connectivity;

import java.sql.ResultSet;
import java.sql.Statement;

public class Connectivity {

    public static void main(String[] args) {
       cls o=new cls();
       try{
            Statement s=o.getct();
             ResultSet rs;

            rs=s.executeQuery("select * from REGISTRATION1");
           while(rs.next()){
                String st=rs.getString("FACULTY_ID");
                String st2=rs.getString("STUDENT_ID");


             }

        }catch(Exception e){
            System.out.println(e);
        }
    }
}

The output is:

java.sql.SQLException:
[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"

Why?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Please don't shout - there's no need to use all those capitals – Jon Skeet Mar 18 '15 at 15:09
  • 1
    What's this `cls` class? – kryger Mar 18 '15 at 15:27
  • possible duplicate of [java.sql.SQLException:\[Microsoft\]\[ODBC Driver Manager\] Invalid descriptor index](http://stackoverflow.com/questions/6361340/java-sql-sqlexceptionmicrosoftodbc-driver-manager-invalid-descriptor-in) – user902383 Mar 18 '15 at 15:35

2 Answers2

0

Please explain connection statement in method getct()?? Make sure you have added mysql-connector-java-xyz-bin jar file in your library. The following code may be helpful:

   public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("Driver Setup");

        String connectionUrl = "jdbc:mysql://localhost:3306/db";
            String connectionUser = "root";
            String connectionPassword = "";//password if any
            conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
            System.out.println("Connection setup");
            stmt = (Statement) conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM table_name");
            while (rs.next()) {
                String col1 = rs.getString("column1name");
                String col2 = rs.getString("column2name");

                System.out.println("First Value"+col1+"Second Value"+col2);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

 }
s_ag
  • 82
  • 1
  • 7
0

It looks like FACULTY_ID or STUDENT_ID is not there. Please check the names of columns in ResultSet or try reading by index.

szefuf
  • 500
  • 3
  • 14