在MySQL工作台中正确运行的sql语法在Java中返回语法错误作为预处理语句

时间:2014-06-30 14:48:07

标签: java mysql sql

我使用以下方法在java中运行准备好的sql语句,但它给我一个语法错误消息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在更新temp_test附近使用 内联合ups_bill on temp_test.tracking_number = ups_bill.tra'在第6行

有人可以指出可能出现的问题吗?非常感谢!

这里是方法的代码:

private void updateTemp(){

try{

String sql="insert into temp_test (tracking_number, account_number, service, to_name, to_address, to_city, to_state, to_zip, to_zone, invoice_number, invoice_date, ship_date, account_code,entry_type_1,entry_type_2) \n" +
                    "select distinct trackingnumber1, AccountNumber, chargetypedescription, receiverorganization, receiveraddressline1, receivercity, receiverstate, receiverzipcode, zone, invoicenumber, invoicedate, pickupdate, mid(ref4,1,3),entrytype, entrytype2 \n" +
                    "from ups_bill \n" +
                    "where trackingnumber1!= ' ' and ChargeType = 'FRT' and entrytype = 'SHP' and entrycategorycode!='DFC' and entrycategorycode!='DTP' \n" +
                    "and not exists (select * from temp_test where temp_test.tracking_number=trackingnumber1 and temp_test.invoice_date=invoicedate);\n" +
                    "update temp_test \n" +
                    "inner join ups_bill \n" +
                    "on temp_test.tracking_number=ups_bill.trackingnumber1 \n" +
                    "set temp_test.account_code=mid(ups_bill.ref2,1,3) \n" +
                    "where mid(temp_test.account_code,1,1) between 'a' and 'z';\n" +
                    "update temp_test \n" +
                    "inner join ups_bill \n" +
                    "on temp_test.tracking_number=ups_bill.trackingnumber1 \n" +
                    "set temp_test.account_code=mid(ups_bill.ref1,1,3) \n" +
                    "where mid(temp_test.account_code,1,1) \n" +
                    "between 'a' and 'z';\n" +
                    "update temp_test \n" +
                    "inner join customer_account \n" +
                    "on temp_test.account_code=customer_account.customer_code \n" +
                    "set temp_test.customer_name=\"Dyson Inc\";\n" +
                    "update temp_test \n" +
                    "inner join customer_info \n" +
                    "on temp_test.customer_name=customer_info.customer_name \n" +
                    "set temp_test.address=customer_info.address_line_1;\n" +
                    "update temp_test \n" +
                    "inner join customer_info \n" +
                    "on temp_test.customer_name=customer_info.customer_name \n" +
                    "set temp_test.addressline2=customer_info.address_line_2;\n" +
                    "update temp_test \n" +
                    "inner join customer_info \n" +
                    "on temp_test.customer_name=customer_info.customer_name \n" +
                    "set temp_test.city=customer_info.city;\n" +
                    "update temp_test \n" +
                    "inner join customer_info \n" +
                    "on temp_test.customer_name=customer_info.customer_name \n" +
                    "set temp_test.state=customer_info.state;\n" +
                    "update temp_test \n" +
                    "inner join customer_info \n" +
                    "on temp_test.customer_name=customer_info.customer_name \n" +
                    "set temp_test.zip_code=customer_info.zip_code;\n" +
                    "update temp_test \n" +
                    "inner join ups_bill \n" +
                    "on temp_test.tracking_number=ups_bill.trackingnumber1 \n" +
                    "set temp_test.ref_two=if(ups_bill.ref2!='',ups_bill.ref2,null);\n" +
                    "update temp_test \n" +
                    "inner join ups_bill \n" +
                    "on temp_test.tracking_number=ups_bill.trackingnumber1 \n" +
                    "set temp_test.ref_one=if(ups_bill.ref1!='',ups_bill.ref1,'');\n" +
                    "update temp_test \n" +
                    "inner join ups_bill \n" +
                    "on temp_test.tracking_number=ups_bill.trackingnumber1 \n" +
                    "and ups_bill.chargetype=\"FRT\" \n" +
                    "set temp_test.ups_charge=ups_bill.incentivecredit+ups_bill.billedcharge;\n" +
                    "update temp_test \n" +
                    "inner join ups_bill \n" +
                    "on temp_test.tracking_number=ups_bill.trackingnumber1 and ups_bill.chargetype=\"FSC\" \n" +
                    "set temp_test.acc_charges=ups_bill.chargeamount-temp_test.ups_charge;\n" +
                    "update temp_test \n" +
                    "inner join ups_bill \n" +
                    "on temp_test.tracking_number=ups_bill.trackingnumber1 and ups_bill.chargetype=\"FSC\" \n" +
                    "set temp_test.fsc=ups_bill.incentivecredit+ups_bill.billedcharge;\n" +
                    "update temp_test \n" +
                    "inner join customer_discount \n" +
                    "on temp_test.customer_name=customer_discount.customer_name \n" +
                    "and temp_test.service=customer_discount.service \n" +
                    "set temp_test.discount=customer_discount.discount;\n" +
                    "update temp_test \n" +
                    "inner join customer_discount \n" +
                    "on temp_test.customer_name=customer_discount.customer_name and temp_test.service=customer_discount.service \n" +
                    "set temp_test.min_charge=customer_discount.min_charge;\n" +
                    "update temp_test \n" +
                    "set temp_test.incentives=if(temp_test.ups_charge*(1-temp_test.discount)>=temp_test.min_charge,temp_test.ups_charge*temp_test.discount,temp_test.ups_charge-temp_test.min_charge);\n" +
                    "update temp_test \n" +
                    "set temp_test.billed_charges=temp_test.ups_charge-temp_test.incentives+temp_test.acc_charges+fsc;";
        pst=conn.prepareStatement(sql);

pst.executeUpdate();

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

2 个答案:

答案 0 :(得分:3)

Statement接口(及其子接口PreparedStatement)允许每个语句执行单个查询。您的查询包含多个语句,因此无法执行。您必须准备单独的语句并执行它们。

如果你想要一个全有或全无的行为,你可以用连接启动一个事务,并在执行所有这些事务时执行commit,并在有异常时回滚。

以下是代码的外观框架:

try {
    //you state that the transaction needs a commit statement
    conn.setAutoCommit(false);
    //perform your DML statements
    //...
    //explicitly state you're committing the transaction
    conn.commit();
} catch (Exception e) {
    //rollback the transaction
    conn.rollback();
    //handle the exception...
    //Note: always retrieve the stacktrace
    //it would be better to use a log or another way to archive it
    //this is a pretty basic example
    e.printStacktrace(),
}

答案 1 :(得分:2)

你不能发出两个单独的sql语句,用';'分隔在对executeUpdate的同一次调用中。

在这种情况下,插入然后更新。