在MySQL工作台中正确运行的sql syntaxt将Java中的语法错误返回为prepared语句
我使用以下方法在java中运行准备好的sql语句,但它会给我一条语法错误消息: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在“更新临时测试”附近使用的正确语法 内部合并条例草案 在第6行的临时测试中,跟踪号=ups\U账单.tra 有人能指出可能的错误吗?非常感谢 下面是该方法的代码:在MySQL工作台中正确运行的sql syntaxt将Java中的语法错误返回为prepared语句,java,mysql,sql,Java,Mysql,Sql,我使用以下方法在java中运行准备好的sql语句,但它会给我一条语法错误消息: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在“更新临时测试”附近使用的正确语法 内部合并条例草案 在第6行的临时测试中,跟踪号=ups\U账单.tra 有人能指出可能的错误吗?非常感谢 下面是该方法的代码: private void updateTemp(){ try{
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);
}
}
不能发出以“;”分隔的两条单独的sql语句在对executeUpdate的相同调用中
在这种情况下,先插入,然后更新。您不能发出两个用“;”分隔的单独sql语句在对executeUpdate的相同调用中
在这种情况下,先插入,然后更新。
语句
接口(及其子接口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(),
}
语句
接口(及其子接口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(),
}
使用JDBC,您只能执行一条语句(除非指定(特定于MySQL的)连接属性,该属性支持在一次执行中执行多条语句(这在技术上是JDBC不允许的)。使用JDBC,您只能执行一条语句(除非指定(特定于MySQL的)连接属性)连接属性,支持在一次执行中执行多个语句(JDBC在技术上不允许)。非常感谢!我将尝试此路由!非常感谢!我将尝试此路由!