在运行我的java代码时,我面临着这个错误 - >

joa*_*oan -1 java mysql sql

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
Run Code Online (Sandbox Code Playgroud)

我的代码:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢您的帮助.

Gia*_*ian 5

我的猜测:你WHERE在构造的字符串之后错过了一个空格.试试这个:

String wherestring = "SELECT * FROM bf_loanmaster WHERE "; 
Run Code Online (Sandbox Code Playgroud)

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