Query无法正确执行

时间:2013-10-03 04:31:01

标签: java sql sql-server-2008

我有从2个数据库调用查询的代码,但它必须同时编译。这是我的代码

public void SummTEkspor(){
        TEkspor ti = new TEkspor();
        TEksporDAOImpl impd = new TEksporDAOImpl (DBConnection.getInstance().getCon());
        String sql1 ="DELETE FROM a.dbo.t_export\n" +
                     "INSERT INTO a.dbo.t_export\n" +
                     "SELECT * FROM b.dbo.export x WHERE x.sk_batch IN \n" +
                     "(SELECT sk_batch from batch_hdr WHERE 

                          mth="+cmbBln.getSelectedItem()+
                         "AND year="+cmbThn.getSelectedItem();


        try {
            Statement st = kon.conn.createStatement();
            Statement st2 = kon.secCon.createStatement();
            ResultSet rs = st.executeQuery(sql1);
            while (rs.next()) {
                System.out.println("success");
            }
        } catch (Exception x) {
            System.out.println("error");
        }
    }

但是,当我运行它时,输出很好,但查询没有正确执行,因为我的表中没有数据,而有一些数据将在该表上。

有没有解决办法?感谢您的回复

3 个答案:

答案 0 :(得分:0)

尝试使用executeUpdate代替executeQuery进行数据更新。分离选择查询并使用executeQuery执行它并获取结果集。

答案 1 :(得分:0)

使用executeUpdate代替executeQuery并尝试将您的查询分成几个查询。

答案 2 :(得分:0)

更好的选择是不要在一行中使用所有语句,因为要检索你应该调用executequery并删除或插入你调用executeUpdate方法。 另请避免使用statement并使用PreparedStatement

相关问题