SQLServerException:该语句未返回结果集。我可以删除结果吗?

时间:2016-09-25 19:21:11

标签: java

如何在不获取throw消息SQLServerException:The statement did not return a result set的情况下运行查询。我可以在没有结果的情况下运行它吗?我想要的原因只是运行查询并从一个表复制我的值,并通过一个查询将它们添加到另一个表

 public void makesales(){

         Connection con =getConnection();
         Statement st;
         PreparedStatement pst = null;
         ResultSet rs;
     try{
         String query= "INSERT INTO Sales (Pro_Id ,Pro_Name,Sales_Quantity,Pro_Price ) SELECT Pro_Id,Pro_Name,Sales_Quantity ,Pro_Price FROM Receipt";
         pst=con.prepareStatement(query);

                        rs=pst.executeQuery();
                      if (rs.rowInserted()){
                         JOptionPane.showMessageDialog(null, "copy success");
                      }else{
                        JOptionPane.showMessageDialog(null,"error");                      }
                        // if (rs.next()){

                            //JOptionPane.showMessageDialog(null,"done");
                       //  }else{
           // JOptionPane.showMessageDialog(null, "wrong");
     //}

     }catch (Exception e) { JOptionPane.showMessageDialog(null, e); }



     }

以下是想要使用它的人的正确代码。

     public void makesales(String query,String message) {


        Connection con =getConnection();
        Statement st;
        try{
            st =con.createStatement();
            if((st.executeUpdate(query))==1)
            {            
      JOptionPane.showMessageDialog(null,"Data "+message+" Succefully");
            }else{
                JOptionPane.showMessageDialog(null,"Data Not "+message+ "Error");
            }

                }catch (Exception ex){
                    ex.printStackTrace();
                }
}


private void ReceiptActionPerformed(java.awt.event.ActionEvent evt) {                                        

        String query= "INSERT INTO Sales (Pro_Id ,Pro_Name,Sales_Quantity,Pro_Price ) SELECT Pro_Id,Pro_Name,Sales_Quantity ,Pro_Price FROM Receipt";
    makesales(query,"Inserted");
}

0 个答案:

没有答案