Results 1 to 2 of 2
  1. #1
    trollster is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default Not getting the desired row displayed

    I am learning JDBC, completely new to it. I know MySQL very well. I have a search form in my applet (tab 2), where I search for food. 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. 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:

    Java 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", "");
                        Statement st = (Statement) con.createStatement();
                        ResultSet rs= (ResultSet) st.executeQuery("SELECT * FROM delicious WHERE name = "+s+"");
                        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();
                        st.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);
                            }
                    });
            }       
    }
    My table has the following fields: id, name, category, origin, taste, type.

    Can anyone please check out the code on your comp, try it out and help me with it?

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Not getting the desired row displayed

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Smooth rotation to a desired direction
    By lolmister in forum Java 2D
    Replies: 5
    Last Post: 08-22-2012, 09:17 AM
  2. Replies: 1
    Last Post: 02-13-2012, 02:51 PM
  3. Problem extracting the values in the desired format
    By Whisperer in forum New To Java
    Replies: 16
    Last Post: 08-15-2011, 10:11 AM
  4. How to scale an image to desired size?
    By asifzbaig in forum AWT / Swing
    Replies: 4
    Last Post: 06-26-2011, 09:07 PM
  5. displaying a desired out put
    By Cubswin in forum New To Java
    Replies: 3
    Last Post: 03-13-2011, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •