Search code examples
javasqlprepared-statementjlabelresultset

multiple Jlabel with Query JAVA


My output is

  1. 192.123.123

  2. 123.344.344

  3. 152.254.123

And i Need create 3 Jlabel, Here is my code, but only make 3 Jlabel with the first output and i need the three

        String val= null;
        String sql = "";
        sql = "SELECT ipswitch,x,y FROM switch where Act='A'";

        try {
            com.mysql.jdbc.Statement sta = (com.mysql.jdbc.Statement) cn.createStatement();
            ResultSet r = sta.executeQuery(sql);
            int contador1=3;
            if (r.next() == true) {
                 for (int i = 0; i < contador1; i++) {
                lipe = new javax.swing.JLabel();
                lipe.setText(r.getString(1));
                paneldesiwtch.add(lipe);
                lipe.setBounds(r.getInt(2), r.getInt(3), 100, 40);
                green.setLocation(r.getInt(2), r.getInt(3) + 40);
                yellow.setLocation(r.getInt(2) + 30, r.getInt(3) + 40);
                red.setLocation(r.getInt(2) + 60, r.getInt(3) + 40);
                 }  
            } else {
                JOptionPane.showMessageDialog(null, "No Found");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.toString());
        }

Solution

  • You need to call the next method each time you pass through the for loop so that the cursor moves to the next line of the ResultSet, and so you will get the new lines. Now you are creating all the Jlabels with the first result of the Resultset.