0

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());
}
NullUserException
  • 83,810
  • 28
  • 209
  • 234
Rohan Singh Dhaka
  • 173
  • 2
  • 8
  • 33
  • 3
    ["How to Use Lists"](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html) would be a good point to start – Sujay Aug 27 '12 at 00:40
  • 2
    Possible duplicate of [Data from database to JList](http://stackoverflow.com/questions/12133621/data-from-database-to-jlist) Please edit the original question, rather than start a new one! – Andrew Thompson Aug 27 '12 at 02:58
  • @AndrewThompson: Duplicate :/...didn't notice that part! – Sujay Aug 27 '12 at 03:03

1 Answers1

6

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!

olagjo
  • 2,125
  • 2
  • 14
  • 16