gpt4 book ai didi

mysql - netbeans mysql syntax Error at line 1 and no error in mysql 查询浏览器

转载 作者:行者123 更新时间:2023-11-30 21:50:17 27 4
gpt4 key购买 nike

您好,我有一个 netbeans 问题,我正在用几个查询更新我的数据库,当查询在 mysql 查询浏览器中完美运行时,只有一个运行第二个查询会出现语法问题。这是 netbeans 中的代码:

try{

String Query = "select nr,linkid,transdate,amount,type from astpay"
+ "where type = 'all' or 'cash';";
Statement ps = test.createStatement();
ResultSet rs = ps.executeQuery(Query);
//if there are payments that fit the criteria
if(rs.next()){
//while loop to generatepremuim for the cash payments
while(rs.next()){
System.out.println("Now to generate the premuim for the cash payments...");
Query = "insert into astpay" +
"(linkid,branchno,transdate,amount,refno,month,year,type)" +
"select a.linkid,a.branchno, a.transdate, a.amount, a.refno, a.month,a.year, p.paytype" +
"from astpay a, astpaytype p" +
"where(a.type ='all' or a.type ='cash' or a.type ='debit')" +
"and p.paytype = 'prem';";
ps = test.createStatement();
ps.execute(Query);


}
}else{
System.out.println("There was a an error generating the Premuims for Cash payments....");

}
}catch(SQLException exp){
System.err.println("Failed to execute the statement!");
System.err.println(exp.getMessage());

}

}

这是在 mysql 查询浏览器中运行的同一字符串的 mysql 代码:

`insert into astpay (linkid,branchno,transdate,amount,refno,month,year,type) select a.linkid,a.branchno, a.transdate, a.amount, a.refno, a.month,a.year, p.paytype from astpay a, astpaytype p where (a.type = 'all' or a.type ='cash' or a.type = 'debit') and p.paytype = 'prem';`

这是在 netbeans 中返回的错误:

Failed to execute the statement!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 '= 'all' or 'cash'' at line 1

这里

我已经尝试了很多方法来让它工作,但我不知道哪里出错了。我希望其他人可以提供帮助,我们将不胜感激。

最佳答案

使用 ps.executeUpdate 而不是 ps.execute。并在表 astpayastpaytype 之间添加一个连接(或者使用显式 cross join 如果你想要的话)

您的错误:

改变

where type = 'all' or 'cash'

where type = 'all' or type = 'cash'

where type in ('all' ,'cash')

您的第二个错误:在where 之前添加空格:

 Query = "insert into astpay" +
"(linkid,branchno,transdate,amount,refno,month,year,type)" +
"select a.linkid,a.branchno, a.transdate, a.amount, a.refno, a.month,a.year, p.paytype" +
"from astpay a, astpaytype p" +
" where(a.type ='all' or a.type ='cash' or a.type ='debit')" +
" and p.paytype = 'prem';";

您的代码生成“...from astpay a, astpaytype pwhere(a.type ='all' or ...”

关于mysql - netbeans mysql syntax Error at line 1 and no error in mysql 查询浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47628421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com