why SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index and toString

MariaP picture MariaP · Jan 12, 2014 · Viewed 9.1k times · Source

I have the following program in which the user must give the order code so to get the data of the client and his order. I have made a view in my database named Catalog which includes all the data needed. But when i run my program i get the following error SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index. Is it because of the view? Or there is a problem with my code? Also i wonder if rs.toString(); is the proper function for printing the results.

public class Orders {
    public static void main(String[] args) {

        int order_code;
        int cust_code;
        int quantity;
        double price;
        String name;

        Scanner input = new Scanner (System.in);   
        System.out.print("Please insert order code: ");  
        order_codej = input.nextInt();  
        String url = "jdbc:odbc:orders";  
        Connection dbcon;  
        Statement stmt;  
        ResultSet rs;

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } 
        catch(java.lang.ClassNotFoundException e) {
            System.out.print("ClassNotFoundException: ");
            System.out.println(e.getMessage());
        }
        try {
            dbcon = DriverManager.getConnection(url,"mybase", "mycode");
            stmt = dbcon.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Catalog WHERE o_code="+order_code);
            while (rs.next()) {
                order_code = rs.getInt("o_code");
                cust_code = rs.getInt("cust_code");
                quantity = rs.getInt("quantity");
                price = rs.getFloat("price");
                name = rs.getString("name");
            }
            rs.toString();
            rs.close();
            stmt.close();
            dbcon.close();
        } 
        catch(SQLException e:) {
            System.out.print("SQLException: "); 
            System.out.println(e.getMessage());  
        }
    }
}

Thanks in advance!

Answer

Tomás picture Tomás · Jan 12, 2014

it sounds like either "o_code","cust_code", "quantity" or "price" havent been aliased correctly in your view or at least that they are not included (the columns havent been aliased correctly) in your resultset.

Also, If you provided a little more information it would be more helpful ... like the struture of your view (column types, names etc.) Then we might get a clearer picture of what is going on.