我创建了此查询以从多个表中获取数据
DROP TABLE IF EXISTS tmp_acc;
DROP TABLE IF EXISTS tmp_loan;
CREATE
TEMPORARY TABLE
IF NOT EXISTS
tmp_acc AS
(SELECT
ac.date,
COALESCE((SELECT SUM(cash) FROM accounts WHERE MONTH(date) = MONTH(ac.date - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(ac.date - INTERVAL 1 YEAR)), 0)AS op_cash,
COALESCE((SELECT SUM(bank) FROM accounts WHERE MONTH(date) = MONTH(ac.date - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(ac.date - INTERVAL 1 YEAR)),0)AS op_bank,
COALESCE((SELECT SUM(ho_funds) FROM accounts WHERE date = ac.date), 0)AS ho_funds,
COALESCE((SELECT SUM(recieved) FROM funds WHERE date = ac.date AND branch_to = 'kandy' AND method = 'Cash'),0)AS funds_cash,
COALESCE((SELECT SUM(recieved) FROM funds WHERE date = ac.date AND branch_to = 'kandy' AND method = 'Bank'),0)AS funds_bank,
COALESCE((SELECT SUM(diposit) FROM bank WHERE date = ac.date),0)AS diposits,
COALESCE((SELECT SUM(withdraw) FROM bank WHERE date = ac.date ),0)AS withdrawals
FROM accounts ac);
CREATE
TEMPORARY TABLE
IF NOT EXISTS
tmp_loan AS
SELECT ls.date, COUNT(l.idloan)AS count, l.interest,l.admission_fee,l.document_fee,l.insuarance,
(SELECT SUM(d1.overdue) FROM daily_collections d1 WHERE d1.loan_idloan = l.idloan)AS overdue_collection,
ls.completed_date
FROM loan l
LEFT OUTER JOIN daily_collections d ON l.idloan = d.loan_idloan
JOIN loan_session ls ON ls.loan_idloan = l.idloan
WHERE ls.completed = 'Yes'
GROUP BY ls.completed_date,l.interest;
SELECT
a.date,COALESCE(a.op_cash,0)AS op_cash,COALESCE(a.op_bank,0)AS op_bank, COALESCE(a.ho_funds,0)AS ho_funds,
COALESCE(a.funds_cash,0)AS funds_cash, COALESCE(a.funds_bank,0)AS funds_bank,COALESCE( a.diposits,0)AS diposits, COALESCE(a.withdrawals,0)AS withdrawals,
COALESCE(l.count,0)AS count, COALESCE(l.interest,0)AS interest, COALESCE(l.admission_fee,0)AS admision_fee,
COALESCE(l.document_fee,0)AS document_fee, COALESCE(l.insuarance,0)AS insrance, COALESCE(l.overdue_collection,0)AS overdue_collection
FROM tmp_acc a
LEFT OUTER JOIN tmp_loan l ON a.date = l.completed_date
WHERE MONTH(a.date) = MONTH(current_date())
我使用此查询生成Jasper报告并在java中查看数据。此查询在工作台中正常工作,并返回所有结果。
但它不适用于java。我使用Statement.executeQuery()
并分配给ResultSet
。我打印了ResultSet
,但没有打印。
如何在java中执行此查询?
Java代码
public ArrayList<cashObject> getCashbookData(String date) {
ArrayList<cashObject> results = new ArrayList<>();
try {
conn c = new conn();
String query = "DROP TABLE IF EXISTS tmp_acc;\n"
+ "DROP TABLE IF EXISTS tmp_loan;\n"
+ " \n"
+ " CREATE \n"
+ " TEMPORARY TABLE \n"
+ " IF NOT EXISTS \n"
+ " tmp_acc AS \n"
+ " (SELECT\n"
+ " ac.date,\n"
+ " COALESCE((SELECT SUM(cash) FROM accounts WHERE MONTH(date) = MONTH(ac.date - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(ac.date - INTERVAL 1 YEAR)), 0)AS op_cash,\n"
+ " COALESCE((SELECT SUM(bank) FROM accounts WHERE MONTH(date) = MONTH(ac.date - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(ac.date - INTERVAL 1 YEAR)),0)AS op_bank,\n"
+ " COALESCE((SELECT SUM(ho_funds) FROM accounts WHERE date = ac.date), 0)AS ho_funds,\n"
+ " COALESCE((SELECT SUM(recieved) FROM funds WHERE date = ac.date AND branch_to = 'kandy' AND method = 'Cash'),0)AS funds_cash,\n"
+ " COALESCE((SELECT SUM(recieved) FROM funds WHERE date = ac.date AND branch_to = 'kandy' AND method = 'Bank'),0)AS funds_bank,\n"
+ " COALESCE((SELECT SUM(diposit) FROM bank WHERE date = ac.date),0)AS diposits,\n"
+ " COALESCE((SELECT SUM(withdraw) FROM bank WHERE date = ac.date ),0)AS withdrawals\n"
+ " FROM accounts ac);\n"
+ " \n"
+ " CREATE \n"
+ " TEMPORARY TABLE \n"
+ " IF NOT EXISTS \n"
+ " tmp_loan AS \n"
+ " SELECT ls.date, COUNT(l.idloan)AS count, l.interest,l.admission_fee,l.document_fee,l.insuarance,\n"
+ " \n"
+ " (SELECT SUM(d1.overdue) FROM daily_collections d1 WHERE d1.loan_idloan = l.idloan)AS overdue_collection,\n"
+ " ls.completed_date\n"
+ " FROM loan l \n"
+ " LEFT OUTER JOIN daily_collections d ON l.idloan = d.loan_idloan \n"
+ " JOIN loan_session ls ON ls.loan_idloan = l.idloan\n"
+ " WHERE ls.completed = 'Yes'\n"
+ " GROUP BY ls.completed_date,l.interest;\n"
+ " \n"
+ " SELECT\n"
+ " a.date,COALESCE(a.op_cash,0)AS op_cash,COALESCE(a.op_bank,0)AS op_bank, COALESCE(a.ho_funds,0)AS ho_funds,\n"
+ " COALESCE(a.funds_cash,0)AS funds_cash, COALESCE(a.funds_bank,0)AS funds_bank,COALESCE( a.diposits,0)AS diposits, COALESCE(a.withdrawals,0)AS withdrawals,\n"
+ " COALESCE(l.count,0)AS count, COALESCE(l.interest,0)AS interest, COALESCE(l.admission_fee,0)AS admision_fee,\n"
+ " COALESCE(l.document_fee,0)AS document_fee, COALESCE(l.insuarance,0)AS insrance, COALESCE(l.overdue_collection,0)AS overdue_collection\n"
+ " FROM tmp_acc a \n"
+ " LEFT OUTER JOIN tmp_loan l ON a.date = l.completed_date\n"
+ " WHERE MONTH(a.date) = '" + date + "' ";
Statement st = c.getConnection().createStatement();
ResultSet rs =st.executeQuery(query);
System.out.println("RS :: " + rs);
while (rs.next()) {
cashObject o = new cashObject();
o.setDate(rs.getString(0));
o.setOp_cash(parseDouble(rs.getString(1)));
o.setOp_bank(parseDouble(rs.getString(2)));
o.setHo_funds(parseDouble(rs.getString(3)));
o.setFunds_cash(parseDouble(rs.getString(4)));
o.setFunds_bank(parseDouble(rs.getString(5)));
o.setDiposits(parseDouble(rs.getString(6)));
o.setWithdrawals(parseDouble(rs.getString(7)));
o.setCount(parseInt(rs.getString(8)));
o.setInterest(parseDouble(rs.getString(9)));
o.setAdmission_fee(parseDouble(rs.getString(10)));
o.setDocument_fee(parseDouble(rs.getString(11)));
o.setInsuarance(parseDouble(rs.getString(12)));
o.setOverdueCollection(parseDouble(rs.getString(13)));
results.add(o);
}
} catch (SQLException e) {
logger.error(e.getMessage());
}
// print size of arraylist
System.out.println("CLASS : " + results.size());
return results;
}
答案 0 :(得分:1)
使用默认配置,您不能执行这样的多个语句,因为语句用于执行单个语句,而不是脚本。
您需要分别在脚本中执行五个语句,所以
statement.execute("drop table ...");
statement.execute("drop table ...");
statement.execute("create temporary table ...");
statement.execute("create temporary table ...");
ResultSet rs = statement.executeQuery("select ...");
或者,您可以尝试使用MySQL特定的连接属性allowMultiQueries=true
;这将允许您执行多个语句。它的行为在技术上是不允许的JDBC标准,我不知道它是否适用于这个特定的例子。