Java 无法在以下函数中发现SQL语法错误:导致异常

Java 无法在以下函数中发现SQL语法错误:导致异常,java,mysql,sql,Java,Mysql,Sql,我一直得到以下错误 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '= 3' at line 1 我通读了我的全部代码,目前只有2个SQL函数被调用,能够

我一直得到以下错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
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 '= 3' at line 1
我通读了我的全部代码,目前只有2个SQL函数被调用,能够导致此错误:

  • 阅读

    public void Read (String userName) /*have checked validity*/
    try {
        con = getConnection();
    
        PreparedStatement pstmt = con.prepareStatement("SELECT * FROM " + 
                                  tableName + 
                                 " WHERE userName=?");
    
            pstmt.setString(1,userName);
    
        ResultSet rs = pstmt.executeQuery();
    
        User user;
        PrintQuery(rs);
        rs.close();
        pstmt.close();
        releaseConnection(con);
        return user;
    
    }
    
  • GetItem

     public void getItem(int userid) /*have checked validity*/
     try {
        con = getConnection();
    
        Statement stmt = con.createStatement();
    
        PreparedStatement pstmt = con.prepareStatement("SELECT * FROM " 
                                  + tableName +"WHERE UserId=?");
        pstmt.setInt(1,userid);
        rs = pstmt.executeQuery();
        PrintQuery(rs);
        stmt.close();
        releaseConnection(con);
    
    }
    

  • 我已经被这些问题困扰了好几个小时,我无法解决它。我仔细检查了这些参数是否为空,等等,它们不是。事实上,它们都很好地存在于数据库中。我不知道我错在哪里。任何帮助都将不胜感激。

    错误出现在
    GetName
    上,因为在连接tableName和
    WHERE
    子句的过程中缺少额外的空间

    + tableName +" WHERE UserId=?");
               // ^ add extra space here
    

    非常感谢你!这解决了我的问题,为此我花了整整两个小时:(不客气
    :D
    很高兴它能帮上忙。