Java 我已经更新了我的代码,使用PreparedStatement的代码。我也在谷歌上搜索准备好的报表。好吧,我的问题是对的。它在mysql编译器中运行良好。因此,我仍然无法找出代码的错误所在。你们说的我都做了。等等,我有一个文本字段。我正在输入一些东西,我将该

Java 我已经更新了我的代码,使用PreparedStatement的代码。我也在谷歌上搜索准备好的报表。好吧,我的问题是对的。它在mysql编译器中运行良好。因此,我仍然无法找出代码的错误所在。你们说的我都做了。等等,我有一个文本字段。我正在输入一些东西,我将该,java,mysql,swing,search,jdbc,Java,Mysql,Swing,Search,Jdbc,我已经更新了我的代码,使用PreparedStatement的代码。我也在谷歌上搜索准备好的报表。好吧,我的问题是对的。它在mysql编译器中运行良好。因此,我仍然无法找出代码的错误所在。你们说的我都做了。等等,我有一个文本字段。我正在输入一些东西,我将该值存储在“s”变量中。然后我在查询中使用它。但根据事先准备好的声明,Stefan要求我单独使用“?”标记。不过,我两个都试过了。同样的,表有字段名,但没有值。好的,当我直接将查询设置为“SELECT*FROM delicious where n


我已经更新了我的代码,使用PreparedStatement的代码。我也在谷歌上搜索准备好的报表。好吧,我的问题是对的。它在mysql编译器中运行良好。因此,我仍然无法找出代码的错误所在。你们说的我都做了。等等,我有一个文本字段。我正在输入一些东西,我将该值存储在“s”变量中。然后我在查询中使用它。但根据事先准备好的声明,Stefan要求我单独使用“?”标记。不过,我两个都试过了。同样的,表有字段名,但没有值。好的,当我直接将查询设置为“SELECT*FROM delicious where name='Tandoori Roti'”时,它给出了正确的输出。因此,问题在于s=field.getText();我应该如何放置它以及放置在何处?好的,我将它放在try语句中,得到了输出。非常感谢:)等等,我有一个文本字段。我正在输入一些东西,我将该值存储在“s”变量中。然后我在查询中使用它。但根据事先准备好的声明,Stefan要求我单独使用“?”标记。不过,我两个都试过了。同样的,表有字段名,但没有值。好的,当我直接将查询设置为“SELECT*FROM delicious where name='Tandoori Roti'”时,它给出了正确的输出。因此,问题在于s=field.getText();我应该如何放置它以及放置在何处?好的,我将它放在try语句中,得到了输出。非常感谢:)
    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);
PreparedStatement statement = con.preparedStatement("SELECT * FROM delicious WHERE name = ?");
statement.setString(1,s);
ResultSet rs= (ResultSet) statement.executeQuery();
s = field.getText();