我正在尝试使用 NetBeans-8.0.2 和 JavaFX (FXMLApplication) 制作一个待办事项列表,将内存存储到 MySQL 数据库中。
我知道,SQL 查询需要引号 -> ( ' ) 和 双引号 -> ( ") 与理解字符串相同。
现在,我正在尝试的是,(今天是我姐姐的生日)我正在尝试在我的列表中添加一个任务,上面写着:这是 Rahi 的生日!
但是,由于sql查询,它失败了。
这是因为在代码内部,输入的引号使整个 sql 查询变得复杂。
@FXML
private void handleAddTaskAction(ActionEvent event) {
String date = addTaskDatePicker.getValue().toString();
System.out.println(date);
String hour = hourComboBox.getValue() + "";
String minute = minuteComboBox.getValue() + "";
String where = whereField.getText();
String header = headerField.getText();
String description = descriptionArea.getText();
if(hour.length()==0)
hour= "12 AM";
if(minute.length()==0)
minute= "00";
if(header.length()==0)
header= "(No header available)";
if(description.length()==0)
description= "(No description available)";
if(header.length()==0 && description.length()==0){
header= "(Empty task)";
description= "(Empty description)";
}
String query = "insert into task values('" + date + "','" + hour + " " + minute + " minutes', '"
+ header + "', '" + description + "', 'at " + where + "');";
if (date.length() >= 1) {
try {
statement.execute(query);
} catch (SQLException ex) {
//Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("Error occured!");
alert.showAndWait();
}
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setHeaderText("You must select a date.");
alert.showAndWait();
}
}
我想在键入消息时存储该消息。有什么解决办法吗?
我的数据库表描述和 GUI 附为图片。 问我是否还需要什么。 谢谢。
图片:GUI和 图片:Table description
最佳答案
只需使用 PreparedStatement注入(inject)你的值(value)观,它会为你转义它们。
关于java - MySQL:插入查询中出现引号 -> ( ' ) and double quotation -> ( ") 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34071683/