Note that there are some explanatory texts on larger screens.

plurals
  1. POJDBC + Swing: Not able to get table contents
    text
    copied!<p>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: </p> <pre><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", ""); 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 &lt;= columns; i++) { columnNames.addElement( md.getColumnName(i) ); } while (rs.next()) { Vector row = new Vector(columns); for (int i = 1; i &lt;= 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); } }); } } </code></pre> <p>Here is my table: </p> <p><img src="https://i.stack.imgur.com/2M8DV.png" alt="Table Structure"></p> <p>Also, I am trying to display the table in the panel itself below the search form but it gives error when I add </p> <pre><code>panel.add(scrollpane); </code></pre> <p>How do I rectify this problem too?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload