How can I add items to combobox from Access database?
My code:
String sql = "SELECT * FROM Owner,Cars "
+ "WHERE carID=CarID_ "
+ "ORDER BY OwnerName;";
ResultSet dane = zadanie.executeQuery(sql);
while (dane.next())
{
comboBox_2.addItem(new(dane.getString("year")));
}
zadanie.close();
} catch(SQLException sqe) {
System.out.println("SQl error");
}
but it throws “Sql error”
btw this code:
String sql="SELECT * FROM Cars ;";
ResultSet dane = zadanie.executeQuery(sql);
while (dane.next())
{
System.out.println(
dane.getString("CarName")+"\t" +
dane.getString("year"));
}
zadanie.close();
} catch(SQLException sqe) {
System.out.println("SQl error");
}
works perfectly but if write in that way it shows error
comboBox_2.addItem(new String(dane.getString("CarName")));
System.out.println(
dane.getString("CarName")+"\t" +
dane.getString("year"));
If you’re in a thread other than the
EDT, it is essential to ensure that an event that modifies a Swing component is posted on theEDT. The utility classSwingUtilitiesallows you to do such.But this is not your current problem. Instead of printing out that useless message (i.e. SQL error), why not print a stack trace? Also, I suggest you read up on
SQLException.