在运行我的java代码时,我遇到了这个错误 - >

时间:2013-05-07 07:33:21

标签: 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

我的代码:

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 个答案:

答案 0 :(得分:5)

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

String wherestring = "SELECT * FROM bf_loanmaster WHERE "; 

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

答案 1 :(得分:1)

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

另外需要注意的事项:

所有这些追加都非常低效,请使用StringBuilder或StringBuffer。您还可以使用PreparedStatements,这将使您的代码表现更好,甚至可以使其更容易阅读。

答案 2 :(得分:1)

在其后添加空格..您必须分隔像where ..

这样的关键字

答案 3 :(得分:0)

在查询中添加空格

String wherestring = "SELECT * FROM bf_loanmaster WHERE";

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