简体   繁体   中英

Displaying data from database in JList

I am using following code for retrieving data from database, but i don't know how to show it in JList

Class.forName("oracle.jdbc.driver.OracleDriver");
Statement stmt = null;
ResultSet rs;                                                                          
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/SPL","root","PWD");
stmt=(Statement) conn.createStatement();
rs=stmt.executeQuery(query);
while (rs.next()) 
{
    String stadium = rs.getString("Stadium");
    String city = rs.getString("City");
}

But i want to show column data in JList . Can you guys tell me how to do that?


i am using the following code but it is not displaying anything on my frame, can you please tell me where i am wrong? Thanks

 String query="SELECT * FROM Location";
 DefaultListModel model=new DefaultListModel();
 DefaultListModel model1=new DefaultListModel();
 try
 {                                              Class.forName("oracle.jdbc.driver.OracleDriver");
Statement stmt = null;
ResultSet rs;                                                                          
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/SPL","root","PWD");
stmt=(Statement) conn.createStatement();
rs=stmt.executeQuery(query);
while (rs.next()) 
{
String stadium = rs.getString("Stadium");
String city = rs.getString("City");
model.addElement(stadium);
model1.addElement(city);
}
JList list=new JList(model);
JList list1=new JList(model1);
f8.add(jpa1); //f8=frame name,jpa1=panel name
jpa1.add(list);                                 list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);                        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(1);
JScrollPane listScroller = new JScrollPane(list);                                          
}
catch(SQLException e)
{
System.out.println("Message   : " + e.getMessage());
}

A JList might be based on a ListModel , so you need to make a ListModel containing your data, and then use this to make your JList . I would make a class Stadium or something, with two String fields, name and city .

public class Stadium {
    private String name;
    private String city;

    public Stadium(String name, String city){...}
    //toString()-method to make it display in a meaningful way in the JList
    public String toString(){ return name + " - " + city; }
}

and then you can write something like

...
DefaultListModel listModel = new DefaultListModel();
while (rs.next()) {
    String name = rs.getString("Stadium");
    String city = rs.getString("City");
    Stadium stadium = new Stadium(name, city)
    listModel.addElement(stadium);
}
JList list = new JList(listModel);

Have not tried to compile and test this code, but hopefully the point is helpful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM