简体   繁体   English

JDBC + Swing:无法获取表内容

[英]JDBC + Swing: Not able to get table contents

I am learning JDBC, completely new to it. 我正在学习JDBC,这是一个全新的知识。 I know MySQL very well. 我非常了解MySQL。 I have a search form in my applet (tab 2), where I search for food. 我的小程序中有一个搜索表单(选项卡2),在其中搜索食物。 And when I click search, I want it to search for the exact food from the Database and return the table contents of that particular row alone. 当我单击搜索时,我希望它从数据库中搜索确切的食物,并仅返回该特定行的表内容。 But I am not getting any result. 但是我没有任何结果。 Only an empty frame opens. 仅打开一个空框架。 But when I put the query as "SELECT * FROM table", I am getting the complete table. 但是,当我将查询输入为“ SELECT * FROM table”时,我将获得完整的表。 But I want only one row, the one that is searched for. 但是我只需要搜索的那一行。 Can anyone please tell me where I am going wrong? 谁能告诉我我要去哪里错了? Here's my code: 这是我的代码:

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.DriverManager;
    import java.util.Vector;
    import javax.swing.*;
    import com.mysql.jdbc.*;

    public class Tryout extends JFrame  implements ActionListener {

            private static final long serialVersionUID = 1L;

            final Vector columnNames = new Vector();
            final Vector data = new Vector();

            private JTabbedPane tabbedPane = new JTabbedPane();
            private JPanel inputpanel;
            private JPanel searchpanel;
            public String s;

            public Tryout()  {          
                    inputpanel = createPage1();
                    searchpanel = createPage2();
                    tabbedPane.addTab("Input Form", inputpanel);
                    tabbedPane.addTab("Search Form", searchpanel);
                    this.add(tabbedPane, BorderLayout.CENTER);           
            }

            public JPanel createPage1()  {          
                JPanel panel = new JPanel();
                panel.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                //Some code
                return panel;            
            }

            public JPanel createPage2() {      

                    JPanel panel = new JPanel();
                    panel.setLayout(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();
                    c.anchor = GridBagConstraints.LINE_START;
                    c.weightx = 0.5;
                    c.weighty = 0.5;
                    JLabel region = new JLabel("Enter Region"); 
                    c.anchor = GridBagConstraints.FIRST_LINE_START;
                    c.gridx = 0;
                    c.gridy = 0;
                    panel.add(region, c);
                    JTextField field = new JTextField(20);
                    c.anchor = GridBagConstraints.FIRST_LINE_START;
                    c.gridx = 1;
                    c.gridy = 0;
                    panel.add(field, c);
                    JButton search = new JButton("SEARCH");               
                    c.anchor = GridBagConstraints.FIRST_LINE_START;
                    c.gridx = 2;
                    c.gridy = 0;
                    panel.add(search, c);
                    s = field.getText();
                    search.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent ae){

                        try{
                        Connection con = null;
                        Class.forName("com.mysql.jdbc.Driver");
                        con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/delikat", "root", "");
                        PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT * FROM delicious WHERE name = ?");
statement.setString(1,s);
ResultSet rs= (ResultSet) statement.executeQuery();
                        ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData();
                        int columns = md.getColumnCount();
                        for (int i = 1; i <= columns; i++) {
                        columnNames.addElement( md.getColumnName(i) );
                        }
                        while (rs.next()) {
                        Vector row = new Vector(columns);
                        for (int i = 1; i <= columns; i++) {
                        row.addElement( rs.getObject(i) );
                        }
                        data.addElement( row );
                        }
                        rs.close();
                        statement.close();
                        }
                        catch(Exception e) {}
                        JFrame tab=new JFrame();
                        JTable table = new JTable(data, columnNames);
                        JScrollPane scrollPane = new JScrollPane( table );
                        tab.add( scrollPane );
                        tab.setVisible(true);
                        tab.setSize(300,100);
                        }
                        });
                    return panel;
            }

            public static void main(String args[]) {
                    SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                    Tryout ex = new Tryout();
                                    ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                    ex.setSize(500,500);
                                    ex.setVisible(true);
                            }
                    });
            }       
    }

Here is my table: 这是我的桌子:

表结构

Also, I am trying to display the table in the panel itself below the search form but it gives error when I add 另外,我正在尝试在搜索表单下方的面板中显示表格,但添加时却显示错误

panel.add(scrollpane);

How do I rectify this problem too? 我也该如何解决这个问题?

Thanks 谢谢

What Sudhanshu means is: Sudhanshu的意思是:

PreparedStatement statement = con.preparedStatement("SELECT * FROM delicious WHERE name = ?");
statement.setString(1,s);
ResultSet rs= (ResultSet) statement.executeQuery();

Enclose name is quotes if its of type string (varchar). 如果其名称为字符串(varchar),则用引号引起来。 Better use PreparedStatement in such cases. 在这种情况下,最好使用PreparedStatement。

Basic debugging. 基本调试。 Did you display the value of "s" that is being used in the PreparedStatement? 您是否显示了PreparedStatement中使用的“ s”值?

s = field.getText();

From what I can see, this statement is executed when you build the GUI which means nothing because the user hasn't even entered any text into this text field. 据我所知,该语句是在构建GUI时执行的,这没有任何意义,因为用户甚至没有在此文本字段中输入任何文本。

That statement needs to be executed when you actually invoke the SQL. 实际调用SQL时需要执行该语句。

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

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