0

I'm using the following method to run a prepared sql statement in java, but it give me an syntax error message:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; 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

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);
    }
}
1
  • With JDBC you can only execute a single statement (unless you specify the (MySQL specific) connection property that enables support for executing multiple statements in one execute (which is technically not allowed by JDBC). Commented Jun 30, 2014 at 14:53

2 Answers 2

3

Statement interface (and its subinterface PreparedStatement) allows the execution of a single query per statement. 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(),
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can´t issue two separate sql statements separated by ';' in the same call to executeUpdate.

In this case an insert and then an update.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.