简体   繁体   English

运行我的Java代码时,我遇到此错误->

[英]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.

相关问题 我在编译源代码时遇到错误,它抛出下面给出的错误 - I am facing a error when I compile the source code, It is throwing an error that is given below 当我在ubuntu的摇摆代码中使用DJ浏览器时遇到libwebkitgtk的问题 - When i am using DJ browser in my swing code in ubuntu facing issue with libwebkitgtk 我在以下 Java 代码中遇到错误,任何人都可以帮助我 - I am facing an error in the following java code can anyone help me out 我在 Pascal 的三角形代码中面临的逻辑错误是什么? - What is the logical error I am facing in Pascal's Triangle code? 为什么我的JAVA代码中出现此错误? - Why am I getting this error in my JAVA code? 运行以下Java代码时出现此错误 - I am getting this error when I run the below java code 我在运行 react-native run-android 时遇到了这个问题 - I am facing this issue when I am running react-native run-android 运行我的应用程序时,我在运行时收到以下错误:java.lang.NoSuchMethodError: No virtual method setTokenProvider - I am getting the following error during runtime when running my app: java.lang.NoSuchMethodError: No virtual method setTokenProvider 当我运行我的程序 Java.lang.ClassNotFoundException:con.mysql.jdbc.driver 错误来了 - When i am running my program Java.lang.ClassNotFoundException:con.mysql.jdbc.driver error coming 通过命令行运行Java代码时出现路径错误 - Path error when running Java code through my command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM