小编Boo*_*tic的帖子

Preparedstatement中的Mysql语法错误

我有一个简单的准备好的语句来更新 MySQL 数据库中的记录。表、列、目标属性和信息取自文本字段。首先我没有准备好的语句,但是如果用户选择插入引号或 %%$&^* 其他符号,程序很容易被破坏。

现在我得到这个:

更新客户端 SET 'full_name'=Martinajh WHERE id='5'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“selectedTable2”SET 'namec' = 'cellValue' WHERE id== 5' 附近使用正确的语法

String cellValue3 = String.valueOf( table.getValueAt(row, 0) );
int id = Integer.parseInt(cellValue3);
String selectedTable2 = (String) listOfTablesNames.getSelectedValue();
String cellValue = String.valueOf( table.getValueAt(row, column) );
String namec = table.getColumnName(column);
String query ="UPDATE ? SET ? = ? WHERE id== ?";
PreparedStatement preparedStmt = (PreparedStatement) conn.prepareStatement(query);
preparedStmt.setString   (1, "selectedTable2");
preparedStmt.setString(2, "namec");
preparedStmt.setString(3, "cellValue");
preparedStmt.setInt(4, id);
preparedStmt.executeUpdate();
Run Code Online (Sandbox Code Playgroud)

java mysql prepared-statement

1
推荐指数
1
解决办法
1029
查看次数

标签 统计

java ×1

mysql ×1

prepared-statement ×1