2013-05-20 54 views
0

瞭如何使用在Java中使用多個查詢JDBC如何在Java中使用多個查詢使用JDBC

1.how在方法
方法使用下面的查詢,而不刪除現有的查詢

  1. 插入item_details(stock_name,寺廟,數量) SELECT a.stock_name,a.temple,SUM(CASE WHEN類型= '購買',那麼其他數量
    (數量* -1)完)AS數量 FROM purchase_details一個 GROUP BY a.stock_name,a.temple


    public boolean insertIntimationDetails(StockForm ofform) { 
        boolean status=false; 
        PreparedStatement pst=null; 
        Connection conn=null; 
    
        try { 
        System.out.println("Inside insertIntimationDetails "); 
        String query=" update purchase_details set intimation_quantity = ? where     
    temple=? and Stock_name=? ";  
        System.out.println(query); 
        conn=getConnection(); 
        System.out.println(query); 
        pst=conn.prepareStatement(query); 
        System.out.println(ofform.getIntimationQuantity()); 
        pst.setString(2, ofform.getForTemple()); 
        pst.setString(3, ofform.getStockName()); 
        pst.setLong(1, ofform.getIntimationQuantity()); 
    
           int rows= pst.executeUpdate(); 
        if(rows>0){ 
         status=true; 
        } 
    
    
    
        } catch (Exception e) { 
        e.printStackTrace(); 
        } finally{ 
        try { 
         if(pst!=null) 
          pst.close(); 
         if(conn!=null) 
          conn.close(); 
        } catch (Exception e2) { 
         e2.printStackTrace(); 
        } 
    
    } 
    
    return status; 
    
        } 
    
+0

看起來像你需要有事務屬性。 – shazin

回答

0

可以使兩個SQLS原子通過使用類似於下面的代碼的東西。這保證了全部或沒有規則。

public boolean insertIntimationDetails(StockForm ofform) { 
    boolean status = false; 
    PreparedStatement pst = null; 
    Connection conn = null; 
      Statement stat = null; 

    try { 
     System.out.println("Inside insertIntimationDetails "); 
     String query = " update purchase_details set intimation_quantity = ? where temple=? and Stock_name=? "; 
     System.out.println(query); 
     conn = getConnection(); 
     conn.setAutoCommit(false); // Disable Auto Commit 
     System.out.println(query); 
     pst = conn.prepareStatement(query); 
     System.out.println(ofform.getIntimationQuantity()); 
     pst.setString(2, ofform.getForTemple()); 
     pst.setString(3, ofform.getStockName()); 
     pst.setLong(1, ofform.getIntimationQuantity()); 

     int rows = pst.executeUpdate(); 
     if (rows > 0) { 
      status = true; 
     } 

     stat = conn.createStatement(); 
     boolean status2 = stat 
       .execute("Insert into item_details(stock_name,temple,quantity) SELECT a.stock_name, a.temple, SUM(Case when Type='purchase' then quantity else (quantity*-1) End) AS quantity FROM purchase_details a GROUP BY a.stock_name, a.temple"); 

     if (status && status2) { 
      conn.commit(); 
     } else { 
      conn.rollback(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
     conn.rollback(); 
    } finally { 
     try { 
      if (pst != null) 
       pst.close(); 
      if (stat != null) 
       stat.close(); 
      if (conn != null) 
       conn.close(); 
     } catch (Exception e2) { 
      e2.printStackTrace(); 
     } 

    } 

    return status; 

} 
相關問題