2013-03-13 27 views
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函数,能够引起这种错误的:

  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); 
    
    } 
    

我被困在这些小时现在,我无法解决它。我有双重检查,看看这些参数是否为空等,他们不是。事实上,它们都很好地存在于数据库中。我无法弄清楚我哪里出错了。任何帮助都感激不尽。

回答

6

错误是GetName因为你的表名和WHERE条款

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

谢谢你这么多的拼接过程中缺少一个额外的空间!这解决了我的问题,因为我花了2个小时的时间:(欢迎您提供: – 2013-03-13 01:52:38

+0

':D'很有帮助。 – 2013-03-13 01:53:54

相关问题