如何将新添加的项从jcombo框传递到mysql数据库

时间:2017-11-15 07:36:28

标签: java mysql jcombobox

我有一个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);
                }
            }
        }

0 个答案:

没有答案