【发布时间】:2018-02-05 09:22:14
【问题描述】:
我的Jtable 已连接到我创建的数据库,以便它可以在我的 GUI 中显示所有数据。但我试图从我的 JTable 获取数据到 JTextField。就像当您单击表格的行时,表格内的数据库中的数据将转到 TextField。但是当我点击表格时,它会显示如下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你有一个 SQL 语法错误;检查与您对应的手册 MariaDB 服务器版本,用于在 'NO.='1 附近使用正确的语法 拉舍尔”在第 1 行
我一直在寻找答案,但找不到。请帮助我,自星期五以来我一直坚持这个错误。
table = new JTable();
scrollPane.setViewportView(table);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
int row = table.getSelectedRow();
String table_click = (table.getModel().getValueAt(row, 0).toString());
try {
String query = "SELECT * FROM `raschel` where MACHINE NO.='" + table_click + "'";
Connection con;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String machine = rs.getString("MACHINE NO.");
String type = rs.getString("TYPE");
String product = rs.getString("PRODUCT");
txtMachine.setText(machine);
txtType.setText(type);
txtProd.setText(product);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
});
【问题讨论】: