简体   繁体   English

在MySQL工作台中正确运行的sql语法t作为准备好的语句在Java中返回语法错误

[英]sql syntaxt that runs correctly in MySQL workbench return syntax error in Java as prepared statement

I'm using the following method to run a prepared sql statement in java, but it give me an syntax error message: 我正在使用以下方法在Java中运行准备好的sql语句,但是它给了我一个语法错误消息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update temp_test inner join ups_bill on temp_test.tracking_number=ups_bill.tra' at line 6 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第6行“在temp_test.tracking_number = ups_bill.tra上更新temp_test内部联接ups_bill”附近使用

can some one point out what might be wrong please? 有人可以指出出什么问题吗? Very much appreciated! 非常感谢!

Here the code for the method: 下面是该方法的代码:

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);
    }
}

Statement interface (and its subinterface PreparedStatement ) allows the execution of a single query per statement. Statement接口(及其子接口PreparedStatement )允许对每个语句执行单个查询。 Your query contains several statements, so it cannot be executed. 您的查询包含多个语句,因此无法执行。 You must prepare separate statements and execute them. 您必须准备单独的语句并执行它们。

If you want an all-or-nothing behavior, you can start a transaction with the connection and execute commit when all these are executed, and rollback when you have an exception. 如果您希望采取全有或全无的行为,则可以通过连接启动事务,并在执行所有这些操作时执行提交,并在发生异常时回滚。

Here's a skeleton of how the code may look like: 这是代码外观的框架:

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(),
}

You can´t issue two separate sql statements separated by ';' 您不能发布两个以';'分隔的单独的sql语句 in the same call to executeUpdate. 在对executeUpdate的同一调用中。

In this case an insert and then an update. 在这种情况下,先插入然后是更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM