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 perfectl开发者_如何转开发y 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 the EDT
. The utility class SwingUtilities
allows you to do such.
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
comboBox_2.addItem(new(dane.getString("year")));
}
});
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
.
You do have an error in your SQL at WHERE carID = CarID_"
You are missing the actual ID of the Car. Hope this helps!
It looks like its a problem with your SQL... does that statement execute correctly outside of Java?
SELECT * FROM owner JOIN cars USING (carID) ORDER BY OwnerName;
May work out better for you?
精彩评论