如何在java中使用jdbc使用多个查询

时间:2013-05-20 08:12:59

标签: jdbc

如何在java中使用jdbc

使用多个查询

1.如何在方法中使用以下查询而不删除
中已存在的查询   方法

  1. 插入item_details(stock_name,temple,quantity)  选择a.stock_name,a.temple,SUM(Type ='purchase'时的情况,然后是数量。)   (数量* -1)结束)AS数量  来自purchase_details a  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;
    
        } 
    

1 个答案:

答案 0 :(得分:0)

您可以使用类似于以下代码的内容使两个SQL成为原子。这保证了全部或全部规则。

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;

}