我一直收到以下错误.
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
Run Code Online (Sandbox Code Playgroud)
我浏览了整个代码,此时只有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;
}
Run Code Online (Sandbox Code Playgroud)的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);
}
Run Code Online (Sandbox Code Playgroud)我现在坚持了几个小时,我无法解决它.我已仔细检查以查看这些参数是否为空等等.它们不是.事实上,它们都很好地存在于数据库中.我无法弄清楚我哪里出错了.任何帮助都感激不尽.
错误已启用,GetName因为在tableName和WHERE子句之间的连接期间缺少额外的空间
+ tableName +" WHERE UserId=?");
// ^ add extra space here
Run Code Online (Sandbox Code Playgroud)