Note that there are some explanatory texts on larger screens.

plurals
  1. POJDBC show records in JLabel when button clicked
    primarykey
    data
    text
    <pre><code>public class d4 extends JFrame implements ActionListener { Connection con; String dbName = "mydb"; String bdUser = "root"; String dbPassword = "2323"; String dbUrl = "jdbc:mysql://localhost/mydb"; JButton showButton; static JLabel[] lbl; JPanel panel; public d4() { try { con = DriverManager.getConnection(dbUrl, bdUser, dbPassword); System.out.println("Connected to database successfully!"); } catch (SQLException ex) { System.out.println("Could not connect to database"); } add(mypanel(), BorderLayout.PAGE_START); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 500); setLocation(300, 30); setVisible(true); } public JPanel mypanel() { panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); showButton = new JButton("Show"); showButton.addActionListener(this); // lbl = recordsLabel(); // for (JLabel jlabel : lbl) { // panel.add(jlabel); // Make no sense , Why? // } panel.add(showButton); return panel; } public static void main(String[] args) { new d4(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == showButton) { // } } public JLabel[] recordsLabel() { try { Statement st1 = con.createStatement(); ResultSet result1 = st1.executeQuery("select * from mytable"); ArrayList&lt;String&gt; lableList = new ArrayList&lt;&gt;(); while (result1.next()) { String resultRow = result1.getString(1) + " " + result1.getString(2); System.out.println(resultRow); lableList.add(resultRow); } Object[] arrayResultRow = lableList.toArray(); int rows = result1.last() ? result1.getRow() : 0; System.out.println("Number of rows is: " + rows); lbl = new JLabel[rows]; for (int i = 0; i &lt; rows; i++) { lbl[i] = new JLabel(arrayResultRow[i].toString()); } } catch (SQLException sqle) { System.out.println("Can not excute sql statement"); sqle.printStackTrace(); } return lbl; } } </code></pre> <p>Output:</p> <pre><code>Connected to database successfully! 10 sajjad 11 hamed 12 mehdi 13 hasan 555 fcvc 5858 cccc 1200 world 10 sajjad 1200 world 1200 world 1200 world 555 yes 333 ttt 1200 world Number of rows is: 14 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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