我正在使用以下代码来更新我的数据库表。数据库连接已建立,没有显示异常,但我的数据库表没有更新。
private void setupSaveButton(){
saveButton.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
try {
String updateQuery = "UPDATE " + MySqlConnectionManager.getDatabaseTableName()
+ " SET BUGID='" + bugIdTextField.getValue()
+ "', USERID='" + userIdTextField.getValue()
+ "', SUBJECT='" + subjectTextField.getValue()
+ "', COMMENT='" + commentTextArea.getValue()
+ "', STATUS='" + statusComboBox.getValue()
+ "', OWNER='" +ownerTextField.getValue()
+ "', PRIORITY='" + priorityComboBox.getValue()
+ "' WHERE DATE='"+dateTextField.getValue()+"'; ";
Connection connection = MySqlConnectionManager.getInstance().getConnection();
if(connection!=null){
Statement stmt = connection.createStatement();
System.out.println("Query " + updateQuery);
stmt.executeUpdate(updateQuery);
}
} catch (SQLException ex) {
Logger.getLogger(BugDetailDisplay.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
最佳答案
我猜您正在使用 DateField
来表示日期。
MySql 的默认日期格式是 YYYY-MM-DD
而您的 dateTextField.getValue()
将返回 Date
对象和默认
表示形式将在您的查询中连接起来。因此,两种格式不同,您的查询成功执行,但无法检测到从 Date
的 >toStringdateTextField< 获取的日期行
。您可以使用 SimpleDateFormat
格式化 dateTextField.getValue()
的结果,以允许查询找到匹配的行。
如果您使用简单的textField
,请确保您的日期格式必须与 MySql 日期匹配。
关于java - vaadin 中的数据库表更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27780025/