2014-11-14 77 views
1

我想通過JDBC更新/刪除在sqlite3中設置的給定表的用戶指定字段。爲此我使用了PreparedStatements。JDBC SQLite PreparedStatement更新和刪除

現在情況3:刪除sql查詢沒有語法錯誤,但它不會刪除該行。

而情況2:更新SQL查詢有語法錯誤 - 我希望它接受一個字段,更新值和條件。

我在這裏做錯了什麼?

[編輯]下面的代碼片段:http://pastebin.com/8naiXZ2v

String temp; 
switch (n){ 
    case 1: // Insert 
     System.out.println("Okay!"); 
     PreparedStatement inPst = con.prepareStatement("insert into " + dob.tbName + " (roll, branch, fname, lname, cgpa)" + " values(?,?,?,?,?)"); 
     System.out.print("Roll: "); 
     inPst.setString(1, in.nextLine()); 
     System.out.print("Branch: "); 
     inPst.setString(2, in.nextLine()); 
     System.out.print("Name: "); 
     inPst.setString(3, in.nextLine()); 
     System.out.print("Surname: "); 
     inPst.setString(4, in.nextLine()); 
     System.out.print("CGPA: "); 
     inPst.setString(5, in.nextLine()); 

     inPst.executeUpdate(); 
     break; 

    case 2: // Update 
     System.out.println("Okay!"); 
     System.out.println("Fields: roll\tbranch\tfname\tlname\tcgpa"); 
     PreparedStatement upPst = con.prepareStatement("update " + dob.tbName + " set ? = ? where ? ;"); 
     System.out.print("Enter field name: "); 
     temp = in.nextLine(); 
     upPst.setString(1, temp); 
     System.out.print("Enter field value: "); 
     temp = in.nextLine(); 
     temp = "'" + temp + "'"; 
     upPst.setString(2, temp); 
     System.out.print("Enter condition: "); 
     temp = in.nextLine(); 
     upPst.setString(3, temp); 

     upPst.executeUpdate(); 
     break; 

    case 3: //Delete 
     System.out.println("Okay!"); 
     System.out.println("Fields: roll\tbranch\tfname\tlname\tcgpa"); 
     PreparedStatement delPst = con.prepareStatement("delete from " + dob.tbName + " where ? = ? ;"); 
     System.out.print("Enter key field: "); 
     temp = in.nextLine(); 
     delPst.setString(1, temp); 
     System.out.print("Enter key value: "); 
     temp = in.nextLine(); 
     temp = "'" + temp + "'"; 
     delPst.setString(2, temp); 

     delPst.executeUpdate(); 
     System.out.println("Deleted!"); 

     break; 
+0

您不能參數化這樣的條件。一個參數只能是**值**,而不是對象名稱,當然不是謂詞。包含連接創建等也可能是一個好主意。例如,您是否禁用了autoCommit? – 2014-11-15 07:24:48

+0

那麼有沒有辦法爲用戶指定的字段形成sql查詢來更新/刪除它們中的值? – sprkv5 2014-11-15 09:11:49

回答

0

你的意圖是好的。總是最好使用參數而不是動態(「粘在一起」)SQL語句。但是,參數只能用於表示SQL語句中的。所以,這是不行的

String tableName = "Clients"; 
String columnToUpdate = "LastName"; 
String newValue = "Thompson"; 
String columnToSearch = "ID"; 
int idToMatch = 1; 
String sql = String.format(
     "UPDATE \"%s\" SET ? = ? WHERE ? = ?", 
     tableName.replaceAll("\"", "\"\"")); 
try (PreparedStatement ps = conn.prepareStatement(sql)) { 
    ps.setString(1, columnToUpdate); 
    ps.setString(2, newValue); 
    ps.setString(3, columnToSearch); 
    ps.setInt(4, idToMatch); 
    ps.executeUpdate(); 
} 

就像我們做表名,我們必須把列PreparedStatement文本中,只是提供作爲參數:

String tableName = "Clients"; 
String columnToUpdate = "LastName"; 
String newValue = "Thompson"; 
String columnToSearch = "ID"; 
int idToMatch = 1; 
String sql = String.format(
     "UPDATE \"%s\" SET \"%s\" = ? WHERE \"%s\" = ?", 
     tableName.replaceAll("\"", "\"\""), 
     columnToUpdate.replaceAll("\"", "\"\""), 
     columnToSearch.replaceAll("\"", "\"\"")); 
try (PreparedStatement ps = conn.prepareStatement(sql)) { 
    ps.setString(1, newValue); 
    ps.setInt(2, idToMatch); 
    ps.executeUpdate(); 
} 
+0

用戶指定字段的預先添加現在可用。我明白,在你的版本中,我們也添加了圍繞字段的引號(通過轉義)但我不明白正則表達式替換在string.replaceAll()中是如何工作的 - 特別是兩個參數。 – sprkv5 2014-11-15 23:28:30

+1

@ sprkv5沒有涉及到正則表達式,只是在''''''''''''''''''''''''''''''字符中轉義字符,所以''''替換爲''''以幫助保護自己免受[Little Bobby Tables](http://xkcd.com/327 /)。 – 2014-11-15 23:46:43