2013-05-21 41 views
-1

我正在編寫一個程序,我正在改變角色。 更改角色過程涉及從兩個表中刪除(清除當前角色/組),插入兩個表(設置角色/組)。在Java程序中多刪除MSSQL刪除查詢

我在我的連接字符串中有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(); 
       } 

      } 
     } 
    }); 
+2

SQL注入提前!更好地使用準備好的語句:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html – Barranka

+1

你的意思就像'從hib.personrole中刪除personid在([pid1],[pid2])''中? – Lucas

+0

Javadocs是你的朋友。 '默認情況下,每個Statement對象只能同時打開一個ResultSet對象。因此,如果一個ResultSet對象的讀取與另一個ResultSet對象的讀取交錯,則每個對象必須由不同的Statement對象生成。在Statement接口中的所有執行方法隱式關閉一個語句的當前ResultSet對象(如果存在一個打開的對象)。http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html –

回答

1

你必須獨立執行各delete指令,但沒有限制,做到這一點。

正如我在我的評論說,你的代碼是容易受到SQL注入,所以我建議你使用準備好的語句:

// ... 
PreparedStatement ps1, ps2; 
ps1 = con.prepareStatement("delete from hib.personrole where personid = ?"); 
ps2 = con.prepareStatement("delete from hib.persongroup where personid = ?"); 

ps1.setString(1, userID); 
ps2.setString(1, userID); 

ps1.execute(); 
ps2.execute(); 
// ... 

更多參考:

希望這會有所幫助

+0

謝謝,它看起來像這樣會工作。我遇到的問題是,查詢似乎沒有運行。 – DarthOpto

+0

@DarthOpto請確定你正在提交指令:'con.commit()' – Barranka

+0

@DOntOpto並檢查是否有異常彈出某處(確保打印所有可能的異常的堆棧跟蹤) – Barranka

1

我相信這將是更合適使用here.When你送幾個SQL語句到數據庫在一次批,可以減少通信開銷的量,從而提高性能。

JDBC驅動程序不需要支持此功能。您應該使用DatabaseMetaData.supportsBatchUpdates()方法來確定目標數據庫是否支持批量更新處理。如果您的JDBC驅動程序支持此功能,則該方法返回true。

  • 聲明的PreparedStatement的addBatch()方法,和 CallableStatement的用於個別語句添加到該批料中。 executeBatch()用於開始執行分組在一起的所有 語句。
  • executeBatch()返回一個整數數組,每個元素 該數組表示更新 語句的更新計數。
  • 就像您可以將語句添加到批處理中一樣,您可以使用clearBatch()方法刪除它們。此方法將刪除使用addBatch()方法添加的所有 語句。但是,您不能 有選擇地選擇要刪除的語句。

    示例代碼

` 

con.setAutoCommit(假); stmt.addBatch(getRolesQuery3);
stmt.addBatch(getRolesQuery4); ResultSet rs = stmt.executeBatch();

`