Unable to delete data from MS Access using GUI Java

115
June 30, 2019, at 11:30 PM

I am unable to delete a row from my database. I included a photo with the table & the code. Down u can see the method i used(psmts). I also stated the id as an int . I tried several things and it id not worked so far. Is there any way i can make a delete a row by inserting an ID?

    package fereastra1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
class Fereastra1 extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4,l5;
JTextField t1,t2,t3,t4,t5;
JButton b1,b2;
public Fereastra1()
{
    ResultSet rs;
    setLayout(new FlowLayout());
    l1=new JLabel("titlu_film");  
    l2=new JLabel("an_film");       
    l3=new JLabel("timp_film");        
    l4=new JLabel("film_limba");
    l5=new JLabel("ID");

    t1=new JTextField(20);  
    t2=new JTextField(10);
    t3=new JTextField(10);
    t4=new JTextField(10);
    t5=new JTextField(10);
    b1=new JButton("ADAUGA");
    b2=new JButton("STERGE");
    add(l1);add(t1);add(l2);add(t2);
    add(l3);add(t3);add(l4);add(t4);add(l5);add(t5);add(b1);add(b2);
    setSize(1000,400);   
    setTitle("Conectivitate");
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b1.addActionListener(this);
    b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
    try{
    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    Connection con=DriverManager.getConnection("jdbc:ucanaccess://D:\\Temp\\film.accdb");
PreparedStatement psmt=con.prepareStatement("INSERT INTO filme (titlu_film,an_film,timp_film,film_limba) VALUES (?,?,?,?)"); 

//HERE I TRIED THE METHOD !!!!!!
PreparedStatement psmts=con.prepareStatement("DELETE FROM filme WHERE film_id=?");

        String nume,data,timp,limba;
        int id;
        nume= t1.getText();
        data= t2.getText();
        timp=t3.getText();
        limba= t4.getText();
        id=Integer.parseInt(t5.getText());
        if(e.getSource()==b1)
    {   
        psmt.setString(1,nume);
        psmt.setString(2,data);
        psmt.setString(3,timp);
        psmt.setString(4,limba);
        psmt.executeUpdate();
    }
        if(e.getSource()==b2)
        {
        psmts.setInt(5,id);
        psmts.executeUpdate(); 
        }
        con.close();
    System.out.println("Success");
    }catch(Exception r){
    System.out.println(r);
            }
}

public static void main(String[] arr)
{
    new Fereastra1();
}
}
Answer 1

I believe the position reference is off for the delete?

Change:

psmts.setInt(5,id); // psmts delete parameter 5 ??

To:

psmts.setInt(1,id); // only 1 parameter in the delete prepared statement
READ ALSO
How to import .cer / .p12 into mobile application

How to import .cer / .p12 into mobile application

I have a SOAP request to make through HTTPS which requires a self-signed certificateI have it in the format of

192
Negative lookbehind: How can I stop matches when the suffix is repeated?

Negative lookbehind: How can I stop matches when the suffix is repeated?

I have some suffix that I want to match when some prefix is absentHowever, the suffix might be repeated

131
Installing AppDynamics with Java/Tomcat

Installing AppDynamics with Java/Tomcat

I am currently installing AppDynamics agent to monitor a Java App

146
Altering certain elements of a text datasbase post to display as a hyperlink in Spring

Altering certain elements of a text datasbase post to display as a hyperlink in Spring

I'm learning spring and attempting a Twitter clone to get a better grasp on the inner workingsI'm getting hung up on hashtag linking when a certain post is displayed

127