Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Delete MSSQL delete queries in Java Program
    primarykey
    data
    text
    <p>I am writing a program where I am changing roles. The Change Role process involves deleting from two tables(to clear the current role/group), inserting into two tables(to set the role/group). </p> <p>I have <code>allowMultipleQueries = true</code> in my connection string, but it looks like only the first query is running. </p> <p>The database is an MSSQL db. </p> <p>Is there a way to run both queries? Can I delete from both tables?</p> <p>The code I have is below:</p> <pre><code>JButton changeRoleBtn = new JButton("Change Role"); changeRoleBtn.setBounds(50, 375, 150, 30); changeRoleBtn.setToolTipText("Changes the role of the User"); changeRoleBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (requesterRole.isSelected()) { StringBuffer getRolesQuery3 = new StringBuffer("delete from hib.personrole where personid = '"); getRolesQuery3.append(userID).append("'"); StringBuffer getRolesQuery4 = new StringBuffer("delete from hib.persongroup where personid = '"); getRolesQuery4.append(userID).append("'"); try { ResultSet rs = stmt.executeQuery(getRolesQuery3.toString()); ResultSet rs1 = stmt.executeQuery(getRolesQuery4.toString()); boolean empty = true; if(empty) { userRoleLbl.setText("The User is a Requester"); System.out.println(rs); System.out.println(rs1); } } catch(Exception e2) { System.out.println(e2); } } } }); </code></pre> <p>I have changed it to have the prepared statement I get the following error though when I run it. <code>java.sql.SQLException: Invalid parameter index 2.</code></p> <pre><code> changeRoleBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (requesterRole.isSelected()) { try { PreparedStatement ps1, ps2; ps1 = con.prepareStatement("delete from hib.personrole where personid = ?"); ps2 = con.prepareStatement("delete from hib.persongroup where personid = ?"); ps1.setInt(1, userID); ps2.setInt(2, userID); ps1.executeQuery(); ps2.executeQuery(); con.commit(); userRoleLbl.setText("The user is a requester"); } catch(Exception e3) { e3.printStackTrace(); } } } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload