The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

answer

Posted by Chin Loong on September 25, 2001 at 10:11 AM

> Hi,
> Can anyone show me how to retrieve values from a database field (e.g. student name) and display it in a JComboBox?
> Thanks!


err.. use java.sql to connect to database, retrieve the data into ResultSet objects, and then assign data into the JComboBox object.

i don't really understand your question.. do u mean => how do connect and retrieve data from the database? or => how to assign values to JComboBox?

u can find both in books or online tutorials. not that hard to find.

anyway, just to help :
it will look something like this

try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception e) {
System.err.println("Unable to load driver.");
e.printStackTrace();
}

try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/survey_data?user=survey&password=survey");
}
catch(SQLException e) {
System.out.println("SQLException caught: " + e.getMessage());
}

Statement stat=con.createStatement();
ResultSet rs=stat.executeQuery("select * from member");

JComboBox combo=new JComboBox();
while(rs.next())
{
combo.addItem(rs.getString(1));
}
con.close();


[blah blah blah] hope i didn't get any commands wrong :p




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us