JDBC + Swing:无法获取表内容

时间:2013-03-22 14:24:52

标签: java mysql swing search jdbc

我正在学习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);
                            }
                    });
            }       
    }

这是我的表:

Table Structure

另外,我试图在搜索表单下面的面板中显示该表,但是当我添加时它会出错

panel.add(scrollpane);

我如何纠正这个问题?

由于

3 个答案:

答案 0 :(得分:3)

Sudhanshu的意思是:

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

答案 1 :(得分:2)

如果类型为string(varchar),则封闭名称为引号。在这种情况下更好地使用PreparedStatement。

答案 2 :(得分:1)

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

s = field.getText();

从我所看到的,当您构建GUI时执行此语句,这意味着什么,因为用户甚至没有在此文本字段中输入任何文本。

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