java - 我想写代码来更新Java中的mysql数据。这是我的代码。它给出了这样的错误:
问题描述
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultTableModel d1 = (DefaultTableModel)jTable2.getModel();
int selectIndex = jTable2.getSelectedRow();
int id = Integer.parseInt(d1.getValueAt(selectIndex, 0).toString());
String bname = txtname.getText();
CategoryItem citem = (CategoryItem) txtcategory.getSelectedItem();
AuthorItem aitem = (AuthorItem) txtauthor.getSelectedItem();
PublisherItem pitem = (PublisherItem) txtpub.getSelectedItem();
String contents = txtcontent.getText();
String pages = txtno.getText();
String edition = txtedition.getText();
try {
pst = con.prepareStatement("update book set bname= ? , category= ? , author= ? , publisher= ? , contents= ? , pages= ? , edition= ? , where id= ? ");
pst.setString(1, bname);
pst.setInt(2, citem.id);
pst.setInt(3, aitem.id);
pst.setInt(4, pitem.id);
pst.setString(5, contents);
pst.setString(6, pages);
pst.setString(7, edition);
pst.setInt(8,id);
int k=pst.executeUpdate();
if(k==1)
{
JOptionPane.showMessageDialog(this,"Book Added");
txtname.setText("");
txtcategory.setSelectedIndex(-1);
txtauthor.setSelectedIndex(-1);
txtpub.setSelectedIndex(-1);
txtcontent.setText("");
txtno.setText("");
txtedition.setText("");
}
else
{
JOptionPane.showMessageDialog(this,"Error");
}
// TODO add your handling code here:
} catch (SQLException ex) {
Logger.getLogger(Book.class.getName()).log(Level.SEVERE, null, ex);
}
}
// and it gives error like this
//com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check //the manual that corresponds to your MariaDB server version for the right syntax to use near 'where id= //1' at line 1
解决方案
此错误的可能原因是您编写了错误的查询。请看下面:
pst = con.prepareStatement("update book set bname= ? , category= ? , author= ? , publisher= ? , contents= ? , pages= ? , edition= ? , where id= ? ");
版本=?, 在哪里
在 where 之前删除 ,
未来的提示,这样就不会卡在未来
- 尝试自己逐行调试代码并识别行号。
- 仔细阅读异常。
- 如果问题出在 SQL 中(您可以通过此处的异常名称确定MySQLSyntaxErrorException),然后首先在数据库控制台手动执行查询。
推荐阅读
- powershell - 为什么 COM 对象方法在 powershell 中不可见/不可访问?
- objective-c - 确定应用程序是否因系统关闭而终止
- r - 如何在 R 中将两列合并为一列
- couchdb - 如何定义要在 Mango 查询中使用的索引
- android - RadAutoCompleteTextView 在离开页面时触发 @tokenRemoved
- android-studio - 重新安装 Windows 后构建和运行颤振项目时出错
- c# - 如何通过反序列化将类级属性值正确分配给嵌套对象
- html - Div 滚动使 padding-bottom 消失
- docker - 在 Windows 10 上安装 Docker
- javascript - 使用 NextJS 用 Class Components 和 getInitialProps 做 SSR,render 方法有未定义的数据