-1
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

我的代码:

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();
    }
}

有任何想法吗?

谢谢您的帮助。

4

4 回答 4

5

我的猜测:您WHERE在构造的字符串之后缺少一个空格。尝试这个:

String wherestring = "SELECT * FROM bf_loanmaster WHERE "; 

调试此类错误的最佳方法是在执行之前打印出您构建的 SQL 查询,以便您可以手动检查它是否存在问题。

于 2013-05-07T07:35:29.537 回答
1

在 where.. 之后添加一个空格。您必须将关键字分开,例如 where..

于 2013-05-07T08:43:48.557 回答
1

WHERE很可能是一个问题。您可能遇到的第二个问题是没有将字符串放在引号中。例如,它可能应该是wherestring = wherestring + "custName = '"+custname+"' ";

还有注意事项:

所有这些附加都非常低效,请改用 StringBuilder 或 StringBuffer 。您还可以使用 PreparedStatements,这将使您的代码性能更好,甚至可能使其更易于阅读。

于 2013-05-07T07:43:43.320 回答
0

在您的查询中留一个空格

String wherestring = "SELECT * FROM bf_loanmaster WHERE";

WHERE 语句和条件之间没有空格。

于 2013-05-07T07:43:31.347 回答