2014-11-14 79 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