Rakibul Hasan
Rakibul Hasan

Reputation: 61

unable to execute "delete" query in java

in a swing button I set a action that it execute delete query and execute another class. here's my code:

JButton btnScanMyPc = new JButton("SCAN MY PC");
    btnScanMyPc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try{
                String q="DELETE FROM `search` WHERE 1=1 ";
                PreparedStatement st=connection.prepareStatement(q);
                ResultSet rs=st.executeQuery();
                ReadDir rd = new ReadDir();
                ReadDir.main(null);
            }
            catch(Exception e)
            {JOptionPane.showMessageDialog(null, e);}


        }

when i execute this query in database it works perfectly. but in java it shows some error like:

java.sql.SQLException: Can not issue data manipulation Statement with executeQuery().

Upvotes: 0

Views: 462

Answers (2)

user4815100
user4815100

Reputation:

try this one

 "DELETE FROM search WHERE 1=1 "

 st.executeUpdate(q);

Upvotes: 1

Akash Thakare
Akash Thakare

Reputation: 22972

You have to use executeUpdate() instead of executeQuery() for data manipulation like Insert,Update or Delete.

Upvotes: 4

Related Questions