我的查询在workbanch中运行完美,但没有使用Java发送MySQL语法错误的异常:
Set @total=0;
SET @amountDue=0;
SELECT (l.paid_date),c.customer_name,IF(@total=0,(SELECT total_amount FROM
payment_loan WHERE loan_id=99)-(SELECT (installment_amount_month) FROM
payment_loan WHERE loan_id=99)*(SELECT (total_installments)
FROM payment_loan
WHERE loan_id=99),
(SELECT (installment_amount_month) FROM payment_loan WHERE
loan_id=99))AS AmountDue,
@amountDue:=@amountDue+(SELECT IF(@total=0,(SELECT (total_amount)
FROM payment_loan WHERE loan_id=99)-(SELECT (installment_amount_month) FROM payment_loan
WHERE loan_id=99)*(SELECT (total_installments) FROM payment_loan
WHERE loan_id=99),(SELECT (installment_amount_month) FROM
payment_loan WHERE loan_id=99))) as ComulativeDue,
l.amount AS AmountPaid, @total := @total +l.amount AS comulativePaid,
(@total/@amountDue ) as percentage FROM payments_details l Join
customer c on (c.customer_id=l.customer_id) WHERE l.customer_id=115
GROUP BY l.paid_date ORDER BY l.paid_date DESC LIMIT 1 ;
我的java代码是:
PreparedStatement stmt = (PreparedStatement) connection.Connect.con.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
cName = rs.getString(2);
amountDue = rs.getInt(3);
}
错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个 SQL语法错误;查看与您的手册相对应的手册 MySQL服务器版本,用于在'SET附近使用正确的语法 @ amountDue = 0; SELECT(l.paid_date),c.customer_name,IF(@ total = 0,(SELECT 总数'在第2行
答案 0 :(得分:0)
默认情况下,不允许在单个调用中进行多个查询。这在Multiple queries executed in java in single statement中很好地描述了:
发送连接请求时,您需要附加连接 属性
allowMultiQueries=true
到数据库url。这是 额外的连接属性,如果已经存在一些,如autoReConnect=true
等。allowMultiQueries
的可接受值 属性为true
,false
,yes
和no
。任何其他价值 在运行时被SQLException
拒绝。String dbUrl = "jdbc:mysql:///test?allowMultiQueries=true";
...
答案 1 :(得分:-1)
修改您的查询:
SELECT (l.paid_date),c.customer_name,IF(@total=0,(SELECT total_amount FROM
payment_loan WHERE loan_id=99)-(SELECT (installment_amount_month) FROM
payment_loan WHERE loan_id=99)*(SELECT (total_installments)
FROM payment_loan, (SELECT @total:=0, @amountDue:=0) setup
WHERE loan_id=99),
(SELECT (installment_amount_month) FROM payment_loan WHERE
loan_id=99))AS AmountDue,
@amountDue:=@amountDue+(SELECT IF(@total=0,(SELECT (total_amount)
FROM payment_loan WHERE loan_id=99)-(SELECT (installment_amount_month) FROM payment_loan
WHERE loan_id=99)*(SELECT (total_installments) FROM payment_loan
WHERE loan_id=99),(SELECT (installment_amount_month) FROM
payment_loan WHERE loan_id=99))) as ComulativeDue,
l.amount AS AmountPaid, @total := @total +l.amount AS comulativePaid,
(@total/@amountDue ) as percentage FROM payments_details l Join
customer c on (c.customer_id=l.customer_id) WHERE l.customer_id=115
GROUP BY l.paid_date ORDER BY l.paid_date DESC LIMIT 1 ;