2016-03-08 45 views
0

我需要從一個jsp頁面更新一個字段到一個mysql表。如何使用jsp更新mysql表?

我用這個代碼:

int id=Integer.parseInt(request.getParameter("r")); 

    switch(id) 
    { 
     case 0: 
      ruolo="null"; 
      break; 
     case 1: 
      ruolo="Direttore"; 
      break; 
     case 2: 
      ruolo="Operaio"; 
      break; 
     case 3: 
      ruolo="Capo Reparto"; 
      break; 
    } 


    query="UPDATE dipendenti SET ruolo=? WHERE ID_dipendente="+caratteristiche[1]+""; 
    PreparedStatement statement = null; 
    statement=connessione.prepareStatement(query); 
    statement.setString(1,ruolo); 
    statement.executeUpdate(); 
    out.println("Successfully Updating Ruolo"); 

它不給我錯誤,但該表沒有更新。

caratteristiche[1] 

是一個字符串,被稱爲ID_dipendente。

有什麼不對?

+0

嘗試在WHERE ID_dipendente = '+ caratteristiche [1] +' 使用單引號 –

回答

0

statement.executeUpdate()返回受影響記錄的數量。您應該檢查回報值是否符合您的預期。

int count = statement.executeUpdate(); 
if (count != 1) // assuming one record should be updated 
     ... // handle or log the error 

現在既然你已經使用了PreparedStatement,你也應該使用參數爲的ID_dipendente PARAM:

query="UPDATE dipendenti SET ruolo=? WHERE ID_dipendente=?"; 
PreparedStatement statement = null; 
statement=connessione.prepareStatement(query); 
statement.setString(1,ruolo); 
statement.setString(2,caratteristiche[1]); 
int count = statement.executeUpdate();