Results 1 to 2 of 2
- 03-22-2013, 04:36 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 1
- Rep Power
- 0
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:
My table has the following fields: id, name, category, origin, taste, type.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); } }); } }
Can anyone please check out the code on your comp, try it out and help me with it?
Thanks
- 03-22-2013, 06:17 PM #2
Re: Not getting the desired row displayed
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Smooth rotation to a desired direction
By lolmister in forum Java 2DReplies: 5Last Post: 08-22-2012, 09:17 AM -
Need help. I am not able to get the desired display of output in my program.
By pcmechanic in forum New To JavaReplies: 1Last Post: 02-13-2012, 02:51 PM -
Problem extracting the values in the desired format
By Whisperer in forum New To JavaReplies: 16Last Post: 08-15-2011, 10:11 AM -
How to scale an image to desired size?
By asifzbaig in forum AWT / SwingReplies: 4Last Post: 06-26-2011, 09:07 PM -
displaying a desired out put
By Cubswin in forum New To JavaReplies: 3Last Post: 03-13-2011, 06:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks