我正在用Java创建一个方法。那是从数据库中检索消息,但我不断收到语法错误。有人能帮助我吗?
public static String retrieveMessages()
{
PreparedStatement pstmt;
try
{
pstmt = connection.prepareStatement("SELECT * from tblEmail"
+ "INNERJOIN tblUser "
+ "ON tblUser.username = tblEmail.`from`"
+ "WHERE tblUser.username = ?");
pstmt.setString(1, user.Getuser());
results = pstmt.executeQuery();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return (results.toString());
}
答案 0 :(得分:2)
在tblEmail
和inner
之间以及from
和where
之间添加空格
pstmt = connection.prepareStatement("SELECT * from tblEmail "
+ "INNER JOIN tblUser "
+ "ON tblUser.username = tblEmail.`from` "
+ "WHERE tblUser.username = ?");
答案 1 :(得分:1)
您需要添加空格(INNER
之前和WHERE
之前)。将查询拆分为多行并不会将一行的结尾与下一行的开头分开。
pstmt = connection.prepareStatement("SELECT * from tblEmail "
+ "INNER JOIN tblUser "
+ "ON tblUser.username = tblEmail.`from` "
+ "WHERE tblUser.username = ?");
当你写:
"SELECT * from tblEmail"
+ "INNER JOIN tblUser "
与以下内容相同:
"SELECT * from tblEmailINNER JOIN tblUser "