Hi developers,
I'm not sure if this is possible, but I've tried to add database elements to a list box.
There are no errors shown, but it simply does not work. Here is the code below:

if(criteria1 == 1){
getDBConnection();
try 
{

    stmt = conn.createStatement();

    if(stmt.execute("SELECT Clients.Firstname FROM Clients WHERE Clients.Firstname= '"+ searchCriteria1 + "' "))
    {

	rs = stmt.getResultSet();
	System.out.print("STEP 2");

    }

	while(rs.next()) {
	//Add to record List box

	searchListModel.addElement(rs.getString(1).trim());
	System.out.print("STEP 3");

	}

}

catch (SQLException ex) {

    System.out.println("SQLException: " + ex.getMessage());

    return;

}

statements, listbox, listmodel and criteria and all been previously initialised.
Thanks in advance,
Cleo

I've tested the statement edited below in mySql and it does return the record I expect. Althout when I try to print out to the terminal, it only gets up to the print out 'STEP 2' :

if(event.getSource() == search) { //if search button is clicked
   System.out.print("STEP 1");
  System.out.print("criteria" + criteria1);

if(chosenCriteria1.equals(search1)) { 
//search 1 = firstname //search string
//chosenCriteria1 = firstname // (from combobox)
	criteria1 = 1;
	System.out.println("criteria 1: hellooooooo:" +criteria1);
}

if(criteria1 == 1){
getDBConnection();
try {
stmt = conn.createStatement();
if(stmt.execute("SELECT Firstname FROM Clients WHERE Firstname = '"+ searchCriteria1 + "' "))
{
  rs = stmt.getResultSet();
  System.out.print("STEP 2");
}
while(rs.next()) {
	String name = rs.getString("Firstname");
                System.out.println("name :" + name);

	//Add to record List box
	searchListModel.addElement(rs.getString(1).trim());
						System.out.print("STEP 3");
}
}
catch (SQLException ex) {
	System.out.println("SQLException: " + ex.getMessage());
	return;
}

And I have added the model to the list :

searchListModel = new DefaultListModel();
searchListModel.addElement("TEST");
searchList = new JList(searchListModel);

"Test" is currently seen in the list box...

This article has been dead for over six months. Start a new discussion instead.