0

I want to display values from database in many lines using jLabel. I tried using the html trick but I don't know how to apply it here in my code. I just get errors. Maybe I'm doing it the wrong way. Haha.

     try{
            //display doctor's name by selected classification
            String url = "jdbc:mysql://localhost/sched";
            Connection conn = DriverManager.getConnection(url,"root","");
            Statement stmt = conn.createStatement();
            String classification = comboClass.getSelectedItem().toString();
            String sqlSelect= "select * from doctorsched where class = '"+classification+"'";
            ResultSet rs = stmt.executeQuery(sqlSelect); 
            String finalText ="";
        while(rs.next()){
             String docsName= rs.getString("docName");
             String room = rs.getString("room");
             String days = rs.getString("day");
             String from = rs.getString("timefrom");
             String to = rs.getString("timeto");
             
             finalText += docsName+" (room "+room+", "+days+", "+from+"-"+to+") \\n";
             // i want to display values from database in many lines but using jLabel
             
        }
        jLabel10.setText(finalText);
      
} catch (Exception ex) {
  
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}

Something like this should be my output

doc riza (room 104, Every Thursday, 10:30AM-10:00AM)
doc j (room 101, null, 10:30AM-11:30AM)
doc lea (room 102, Every Saturday, 10:30AM-4:30PM)
frida (room 101, null, 8:00AM-9:30AM)

Please help me out :( The '\n' works in jTextArea but not in jLabel. I also tried '\n' in label, but doesn't work, too.

Okay so I tried this one

finalText += "<html>"+docsName+" (room "+room+", "+days+", "+from+"-"+to+")<br></html>";

But this code only display one line. The first row in my database. I need to display all of the rows.

Now, this next code shows it all, but the next line still doesn't work.

while(rs.next()){
             String docsName= rs.getString("docName");
             String room = rs.getString("room");
             String days = rs.getString("day");
             String from = rs.getString("timefrom");
             String to = rs.getString("timeto");
             
             finalText += docsName+" (room "+room+", "+days+", "+from+"-"+to+")\n";
        }
        jLabel10.setText("<html>"+finalText+"<br></html>");
} 

Whyyy

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

0

use a code like this

String finalText = "";
for (int i = 0; i < 10; i++) {
    finalText += "line:" + i;
    finalText += "<br>";
}
label.setText("<html>" + finalText + "</html>");
JavaMan
  • 1,142
  • 12
  • 22
  • Thank you! I've done it without using for loop tho. But I followed your code inside the loop. <3 – Rica Jacutina Nov 14 '20 at 10:42
  • No problem, please consider accepting my answer so that others will not unnecessarily take time to answer your already answered question. – JavaMan Nov 14 '20 at 10:51
0
 try{
            //display doctor's name by selected classification
            String url = "jdbc:mysql://localhost/sched";
            Connection conn = DriverManager.getConnection(url,"root","");
            Statement stmt = conn.createStatement();
            String classification = comboClass.getSelectedItem().toString();
            String sqlSelect= "select * from doctorsched where class = '"+classification+"'";
            ResultSet rs = stmt.executeQuery(sqlSelect); 
            String finalText ="";

        while(rs.next()){
             String docsName= rs.getString("docName");
             String room = rs.getString("room");
             String days = rs.getString("day");
             String from = rs.getString("timefrom");
             String to = rs.getString("timeto");

             finalText += (docsName+" (room "+room+", "+days+", "+from+"-"+to+")\n") ;
             finalText += "<br>";
               
        }
         jLabel10.setText("<html>" + finalText + "</html>");

} catch (Exception ex) {
  
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}