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());
}
3
  • 3
    "How to Use Lists" would be a good point to start Commented Aug 27, 2012 at 0:40
  • 2
    Possible duplicate of Data from database to JList Please edit the original question, rather than start a new one! Commented Aug 27, 2012 at 2:58
  • @AndrewThompson: Duplicate :/...didn't notice that part! Commented Aug 27, 2012 at 3:03

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.