JDBC SQLite PreparedStatement更新和删除
我想通过JDBC为sqlite3中设置的给定表更新/删除用户指定的字段。为此,我使用了准备好的报表 现在,案例3:deletesql查询没有语法错误,但它不会删除行 案例2:UPDATESQL查询有语法错误-我希望它接受一个字段、更新值和条件 我做错了什么 [编辑]以下是代码段:JDBC SQLite PreparedStatement更新和删除,jdbc,sqlite,Jdbc,Sqlite,我想通过JDBC为sqlite3中设置的给定表更新/删除用户指定的字段。为此,我使用了准备好的报表 现在,案例3:deletesql查询没有语法错误,但它不会删除行 案例2:UPDATESQL查询有语法错误-我希望它接受一个字段、更新值和条件 我做错了什么 [编辑]以下是代码段: 你的意图是好的。最好使用参数,而不是动态粘合在一起的SQL语句。但是,参数只能用于表示SQL语句中的值。所以,这是行不通的 字符串tableName=Clients; String columnToUpdate=Las
你的意图是好的。最好使用参数,而不是动态粘合在一起的SQL语句。但是,参数只能用于表示SQL语句中的值。所以,这是行不通的 字符串tableName=Clients; String columnToUpdate=LastName; 字符串newValue=Thompson; 字符串columnToSearch=ID; int-idToMatch=1; 字符串sql=String.format 更新\%s\SET?=?在哪里?, tableName.replaceAll\,\; 请尝试PreparedStatement ps=conn.prepareStatementsql{ ps.setString1,柱状更新; ps.setString2,新值; ps.setString3,columnToSearch; ps.setInt4,idToMatch; 执行更新; } 就像我们对表名所做的那样,我们必须将列名放在PreparedStatement文本中,并且只提供值作为参数: 字符串tableName=Clients; String columnToUpdate=LastName; 字符串newValue=Thompson; 字符串columnToSearch=ID; int-idToMatch=1; 字符串sql=String.format 更新\%s\设置\%s\=?其中\%s\=?, tableName.replaceAll\,\, ColumntToUpdate.replaceAll\,\, ColumntSearch.replaceAll\,\; 请尝试PreparedStatement ps=conn.prepareStatementsql{ ps.setString1,newValue; ps.setInt2,idToMatch; 执行更新; }
你不能把这样的条件参数化。参数只能是值,不能是对象名,当然也不能是谓词。包括连接创建等也可能是一个好主意。例如,您是否禁用了自动提交?那么有没有一种方法可以对用户指定的字段形成sql查询,以更新/删除其中的值?用户指定字段的预添加现在起作用了。我知道在你的版本中,我们也通过转义字段周围的引号来添加引号;但是我不明白正则表达式替换在string.replaceAll中是如何工作的,特别是在两个参数中。@sprkv5不涉及正则表达式,只是在一个分隔字符串中转义字符,因此被替换为,以帮助保护我们免受攻击。
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;