2017-03-24 95 views
0

我有兩個JComboboxes A和B,所以如果我選擇任何項目形式A,那麼與A中選擇的項目相關的值應填入JCombobox B.我試過這個,但得到一個錯誤:填充JCombobox值取決於另一個JCombobox

java.lang.ArrayIndexOutOfBoundsException: 0

pst.setString(1, client.getSelectedItem().toString());

try 
{ 
    String query="select `User_Name` from Client where `Client_Name`='?' "; 
    PreparedStatement pst=conn.prepareStatement(query); 
    pst.setString(1, client.getSelectedItem().toString()); 

    ResultSet rs=pst.executeQuery(); 

    user.addItem("--Select--"); 
    while(rs.next()) 
    { 
     user.addItem(rs.getString("User_Name"));    
    } 
//  return; 
    System.out.println(query); 

} 
catch(Exception g) 
{ 
    g.printStackTrace(); 
} 
+0

你會得到什麼錯誤?在什麼情況下執行這個代碼?在聽衆? –

+0

是先生,在itemListener ...得到錯誤ArrayIndexOutOfBoundsException在行pst.setString(1,client.getSelectedItem()。toString()); ....但如果我傳遞硬編碼字符串,而不是'?'所以它的工作正常....但它不是目標 – Vsal

+0

@ hunter,在這裏我從數據庫中獲取客戶端名稱...因此,從組合框中選擇此客戶端名稱給'?'並將價值傳遞給此?如果我設置0,那麼錯誤是java.lang.ArrayIndexOutOfBoundsException:-1 – Vsal

回答

0

的PresparedStatement需要報價參數的照顧,當你使用了setString()

嘗試

String query="select User_Name from Client where Client_Name = ?"; 
PreparedStatement pst=conn.prepareStatement(query); 
pst.setString(1, String.valueOf(client.getSelectedItem())); 

我想,當你使用'?'準備的語句不算以此爲參數,這是你爲什麼得到IndexOutOfBounce

+0

@ Thomas.Okay謝謝..我現在試了 – Vsal

+0

先生,非常感謝你:)它的工作原理 – Vsal

0

你做了一個錯誤,

沒有必要使用 '?' ,它應該只是Client_Name =?