我有一个jcombobox,用于填充mysql表中的数据。我向jcombobox添加了一个新项目,并希望将该项添加回mysql表。我可以成功地将项添加到jcombobox列表中,但是当我尝试将所选项添加到mysql表时,它会添加一个空值。
我做错了什么?这是我的代码......
checkitems = con.createStatement();
ResultSet rs = checkitems.executeQuery("SELECT * from artist WHERE artist = '" +(String)art_combo.getSelectedItem()+ "'");
System.out.println((String)art_combo.getSelectedItem());
while (rs.next()){
String artist = rs.getString("artist");
System.out.println("The value of variable artist is "+artist);
String art = (String)art_combo.getSelectedItem();
System.out.println("The value at art variable is "+art+" and the value at artist variable is "+artist+" .");
System.out.println("The value of variable art is "+art);
if (artist.equals(art)){
System.out.println("Artist already exist. Nothing added!!!");
}else{
stm1 = con.prepareStatement("INSERT into artist"+"(artist)"+"VALUES ('?')");
try{
stm1.setString(1, art);
System.out.println(art);
stm1.executeUpdate();
}
catch (SQLException e){
System.out.print(e);
}
}
}