JDBC SQLite PreparedStatement更新和删除

时间:2014-11-14 22:49:17

标签: jdbc sqlite

我想通过JDBC更新/删除sqlite3中设置的给定表的用户指定字段。我已经为此目的使用了PreparedStatements。

现在案例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;

1 个答案:

答案 0 :(得分: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文本中,并提供作为参数:< / p>

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();
}