Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on a project in which I have a SQLite database with a table called Table1 and values title/location without an ID as column etc...

My form has a combobox which I've managed to get it display my DB each entry in one row . Now I want with a button "Delete" to delete the row that I have selected in the combobox and I am kind of lost.

Here is my code for the combobox (I think it's fine):

private void FillCombo(){
    try {
        String newLine = System.getProperty("line.separator");        
        Class.forName("org.sqlite.JDBC");
      connection = DriverManager.getConnection("jdbc:sqlite:C:\users\Kajou\momentsdb.sqlite");
        statement = connection.createStatement();
        resultSet = statement.executeQuery("SELECT * FROM Table1");

        while (resultSet.next()) {    
            comboBox1.addItem(resultSet.getString("Title") + " " + "Date:" + resultSet.getString("Day")+"/"+ resultSet.getString("Month") +"/" + resultSet.getString("Year") + " " + resultSet.getString("Location")+ " " + resultSet.getString("Mood"));
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And now about the button delete. I've tried few things but looks like not going well:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        Class.forName("org.sqlite.JDBC");                
        connection = DriverManager.getConnection("jdbc:sqlite:C:\users\Kajou\momentsdb.sqlite");
        statement = connection.createStatement();
        String sql = "DELETE FROM Table1 WHERE col_string=Title";
        int deleteCount = statement.executeUpdate(sql);
        sql = "DELETE FROM Table1 WHERE col_string=?";
        pst = connection.prepareStatement(sql);
        pst.setString(1,"a string");
        deleteCount=pst.executeUpdate();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(DeleteImportantMomentsForm.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(DeleteImportantMomentsForm.class.getName()).log(Level.SEVERE, null, ex);
    }            
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need something like:

Object item = comboBox.getSeletedItem();
pst.setString(1, item.toString());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...