0

我正在学习 JDBC,对它完全陌生。我非常了解 MySQL。我的小程序(选项卡 2)中有一个搜索表单,用于搜索食物。当我单击搜索时,我希望它从数据库中搜索确切的食物并单独返回该特定行的表格内容。但我没有得到任何结果。只打开一个空框架。但是当我将查询作为“SELECT * FROM table”时,我得到了完整的表。但我只想要一行,即被搜索的那一行。谁能告诉我哪里出错了?这是我的代码:

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

这是我的桌子:

表结构

另外,我正在尝试在搜索表单下方的面板本身中显示表格,但是当我添加时它会出错

panel.add(scrollpane);

我该如何纠正这个问题?

谢谢

4

3 回答 3

3

Sudhanshu 的意思是:

PreparedStatement statement = con.preparedStatement("SELECT * FROM delicious WHERE name = ?");
statement.setString(1,s);
ResultSet rs= (ResultSet) statement.executeQuery();
于 2013-03-22T14:32:34.780 回答
2

如果其类型为字符串 (varchar),则将名称括起来。在这种情况下最好使用 PreparedStatement。

于 2013-03-22T14:26:29.840 回答
1

基本调试。您是否显示了 PreparedStatement 中使用的“s”值?

s = field.getText();

从我所见,当您构建 GUI 时会执行此语句,这没有任何意义,因为用户甚至没有在此文本字段中输入任何文本。

当您实际调用 SQL 时,需要执行该语句。

于 2013-03-22T17:03:36.283 回答