2014-01-12 104 views
0

我有以下程序,其中用戶必須提供訂單代碼才能獲取客戶端及其訂單的數據。我在我的數據庫Catalog中看到了一個包含所有需要的數據的視圖。但是當我運行我的程序時,出現以下錯誤SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index。是因爲這個觀點嗎?或者我的代碼有問題?我也想知道rs.toString();是否適合打印結果。爲什麼SQLException:[Microsoft] [ODBC SQL Server驅動程序] Invalid Descriptor Index and toString

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()); 
     } 
    } 
} 

在此先感謝!

+0

CREATE VIEW目錄AS SELECT Orders.o_code,Orders.cust_code,名稱,價格,Consists.quantity 從接單,客戶,產品,下設 WHERE Orders.cust_code = Customers.cust_code AND Orders.o_code =組成。 o_code AND Consists.product_code = Product.product_code – MariaP

回答

1

它聽起來像「o_code」,「cust_code」,「數量」或「價格」在您的視圖中沒有被正確別名,或者至少它們沒有包括在內(列沒有被正確別名)。另外,如果你提供了更多的信息,它會更有幫助......就像你視圖的結構(列類型,名稱等)。然後我們可以更清楚地瞭解發生了什麼。

+0

是的你是對的。對不起!我將我的觀點發布爲對我的問題的評論。但別名看起來好吧.. – MariaP

+1

我沒有提出這個建議的基礎,但它只是一個預感......你可以試着按照它們出現在你的select語句中的順序來獲得這些字段嗎?即rs.GetString(「name」),然後是rs.getFloat(「price」),然後是rs.getInt(「quantity」)。 –

+0

你的預感是對的!謝謝!!!! – MariaP

相關問題