我正在编写一个我正在改变角色的程序。更改角色过程涉及从两个表中删除(以清除当前角色/组),插入到两个表中(以设置角色/组)。
我allowMultipleQueries = true
的连接字符串中有,但看起来只有第一个查询正在运行。
该数据库是一个 MSSQL 数据库。
有没有办法同时运行这两个查询?我可以从两个表中删除吗?
我的代码如下:
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);
}
}
}
});
我已将其更改为准备好的语句,但是当我运行它时出现以下错误。java.sql.SQLException: Invalid parameter index 2.
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();
}
}
}
});