-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と入力パラメーターを使用すること?です。

使用例 (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 に答える