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

时间:2013-03-13 01:49:19

标签: 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函数被调用,能够导致此错误:

  1. 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;
    
    }
    
  2. 的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);
    
    }
    
  3. 我现在被困在这几个小时,我无法解决它。我已仔细检查以查看这些参数是否为空等等。它们不是。事实上,它们都很好地存在于数据库中。我无法弄清楚我哪里出错了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:6)

错误在GetName上,因为在tableName和WHERE子句的连接期间缺少额外的空间

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