2

我不断收到以下错误。

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. 获取项目

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

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

4

1 回答 1

6

错误是因为在 tableName 和子句GetName之间的连接过程中缺少额外的空间WHERE

+ tableName +" WHERE UserId=?");
           // ^ add extra space here
于 2013-03-13T01:50:31.010 回答