【问题标题】:Unable to spot an SQL syntax error in the following functions : Causing an exception [closed]无法在以下函数中发现 SQL 语法错误:导致异常 [关闭]
【发布时间】:2013-03-13 01:49:19
【问题描述】:

我不断收到以下错误。

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

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

【问题讨论】:

    标签: java mysql sql


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!这解决了我花了整整两个小时的问题:(
    • 不客气:D 很高兴它有帮助。
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2014-12-31
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多