简体   繁体   English

无法将数据从JTable数据库提取到JTextField

[英]Cant fetch the data from JTable database to JTextField

My Jtable is connected to the database that I made so that it can show all the data right in my GUI. 我的Jtable连接到我Jtable的数据库,以便它可以在我的GUI中显示所有数据。 But Im trying to fetch the data from my JTable to the JTextField. 但我试图从我的JTable获取数据到JTextField。 Its like when you click the row of the table the data from the database thats inside the table will go to the TextField. 就像当您单击表中的行时,表中的数据库中的数据将转到TextField。 But when I clicked the table it shows an error like this: 但当我点击表格时,它显示如下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NO.='1 RASCHEL" at line 1 检查与您的MariaDB服务器版本对应的手册,以便在第1行的“NO。='1 RASCHEL”附近使用正确的语法

I've been searching for the answer but I was unable to find one. 我一直在寻找答案,但我找不到答案。 Please help me I've been stuck to this error since friday. 请帮帮我,自周五以来我一直坚持这个错误。

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);
        }
    }
});

The column you are using MACHINE NO. 您正在使用MACHINE NO.的列MACHINE NO. contains a space and a dot in the end to work with like names you have to put the name between two : 最后包含一个空格和一个点来处理类似名称,你必须将名称放在两个之间:

`MACHINE NO.`

So your query should look like this : 所以你的查询应该是这样的:

String query = "SELECT * FROM `raschel` where `MACHINE NO.`='" + table_click + "'";

But this still not secure agains syntax error or SQL Injection so instead you can use : 但这仍然不安全再次出现语法错误或SQL注入,所以你可以使用:

String query = "SELECT * FROM `raschel` where `MACHINE NO.` = ?";
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root","");
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, table_click);//<<-----------set the parameter like this
ResultSet rs = ps.executeQuery();

You sould not use blanks and dots in column names. 应该在列名中使用空格和点。 If you have to use blank and dot you have to escape the column name with a backtick: 如果必须使用空白和点,则必须使用反引号来转义列名:

String query = "SELECT * FROM `raschel` where `MACHINE NO.`='"+table_click+"'";

But i would strongly recommand to not use blanks and dots (or other special character) in column or table names. 但我强烈建议不要在列名或表名中使用空格和圆点(或其他特殊字符)。

String query = "SELECT * FROM `raschel` where MACHINENO='"+table_click+"'";

Also change to prepared statements to prevent SQL injection 还要更改为准备好的语句以防止SQL注入

试试吧

String query = "SELECT * FROM `raschel` where `MACHINE NO.`='"+table_click+"'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM