-1

我尝试查找有关它的信息,但似乎没有关于它的示例。
我有这条线:

long userID = user.getId();
String query = "select userclient.username from twitter_content.userclient where userclient.userid = " +
            "(select follower.followerid from twitter_content.follower where follower.followerid = userclient.userid and follower.userid = "+userID+")";

但 Eclipse 将“userID”读取为字符串,而不是变量。如何让 Eclipse 将“userID”读取为变量?

4

2 回答 2

2

最佳实践是使用PreparedStatement和 placeholder 输入参数?

示例用法(来自 JavaDocs):

PreparedStatement stmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?");
stmt.setBigDecimal(1, 153833.00)
stmt.setInt(2, 110592)

将该示例应用到您的案例中,您会得到如下信息:

// prepare connection "conn" earlier
long userID = user.getId();
PreparedStatement stmt = conn.prepareStatement("select userclient.username from twitter_content.userclient where userclient.userid = (select follower.followerid from twitter_content.follower where follower.followerid = userclient.userid and follower.userid = ?)");
stmt.setLong(1, userID);
于 2012-11-15T09:58:53.833 回答
0

尝试这个::

long userID = user.getId();
StringBuilder queryBuilder = new StringBuilder("select userclient.username from twitter_content.userclient where userclient.userid =");
queryBuilder.append("(select follower.followerid from twitter_content.follower where follower.followerid = userclient.userid and follower.userid = ?)");
PreparedStatement stmt = con.prepareStatement(queryBuilder.toString());
stmt.setInt(1, userId);
于 2012-11-15T10:03:22.200 回答