[英]when running my java code i am facing this error ->
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 '' at line 1
my code: 我的代码:
public static void loanenquiry(String ApplicationID,String LoanNumber,String RIMNumber,String custname,String fromdate,String todate) {
String wherestring = "SELECT * FROM bf_loanmaster WHERE";
try {
if(ApplicationID != null) {
wherestring = wherestring + "ApplicationID ="+BillAction.StringtoInt(ApplicationID)+"";
}
if(LoanNumber != null ) {
if(ApplicationID != null) {
wherestring = wherestring + "AND LoanNumber = "+BillAction.StringtoInt(LoanNumber)+" ";
} else {
wherestring = wherestring + "LoanNumber = "+BillAction.StringtoInt(LoanNumber)+" ";
}
}
if(RIMNumber != null ) {
if(ApplicationID != null && LoanNumber != null) {
wherestring = wherestring + "AND AdvparyRIM = "+RIMNumber+" ";
} else {
wherestring = wherestring + "AdvparyRIM = "+RIMNumber+"";
}
}
if(custname != null ){
if(ApplicationID != null && LoanNumber != null && RIMNumber != null ) {
wherestring = wherestring + "AND custName = "+custname+"";
} else {
wherestring = wherestring + "custName = "+custname+"";
}
}
if(fromdate != null ) {
if(ApplicationID != null && LoanNumber != null && RIMNumber != null && custname != null ) {
wherestring = wherestring + "AND ApplicationDt >= "+BillAction.StringtoDate(fromdate)+" ";
} else {
wherestring = wherestring + "ApplicationDt = "+BillAction.StringtoDate(fromdate)+"";
}
}
if(todate != null ) {
if(ApplicationID != null && LoanNumber != null && RIMNumber != null && custname != null && fromdate != null) {
wherestring = wherestring + "AND ApplicationDt >= "+BillAction.StringtoDate(fromdate)+" AND ApplicationDt <= "+BillAction.StringtoDate(todate)+"";
} else {
wherestring = wherestring + "ApplicationDt >= "+BillAction.StringtoDate(todate)+"";
}
}
Connection conn = BillFinanceDB.getDBConnection();
PreparedStatement psloanenquiry= conn.prepareStatement(wherestring + ";");
ResultSet rs = psloanenquiry.executeQuery();
while(rs.next()) {
System.out.println("loan number"+rs.getInt("LoanNumber"));
}
} catch(SQLException e) {
e.printStackTrace();
}
}
Any ideas? 有任何想法吗?
thanks for the help. 谢谢您的帮助。
My guess: you're missing a space after WHERE
in your constructed string. 我的猜测:您在构造的字符串中的
WHERE
之后缺少空格。 Try this: 尝试这个:
String wherestring = "SELECT * FROM bf_loanmaster WHERE ";
The best way to debug these kinds of errors is to print out the SQL query you have constructed before it is executed so that you can manually inspect it for problems. 调试此类错误的最佳方法是在执行之前打印出已构造的SQL查询,以便您可以手动检查问题。
The WHERE
is most likely a problem. WHERE
最有可能是一个问题。 The second problem that you could have is not putting your strings in quotation marks. 您可能遇到的第二个问题是不将字符串放在引号中。 For example it probably should be
wherestring = wherestring + "custName = '"+custname+"' ";
例如,它可能应该是
wherestring = wherestring + "custName = '"+custname+"' ";
Also things to note: 另外要注意的事情:
All this appending is terribly inefficient, use a StringBuilder or StringBuffer instead. 所有这些附加操作效率极低,请改用StringBuilder或StringBuffer。 You could also use PreparedStatements which would make your code perform better and possibly even make it easier to read.
您还可以使用PreparedStatement,这将使您的代码性能更好,甚至可能更易于阅读。
在位置之后添加空格。
give a space in your query 在查询中留一个空格
String wherestring = "SELECT * FROM bf_loanmaster WHERE";
there is no space between WHERE statement and condition. WHERE语句和条件之间没有空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.