0

我有这个代码:

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+")";

我想在 Where 子句中使用 userID 作为查询的一部分,但我似乎没有正确连接 userID(我希望它被读取为动态值,而不是字符串)。有人可以提供一些指导吗它?

编辑:
这是完整的源代码:

public ArrayList<Post> retrieveFollowerDB(String userName){
    ArrayList<Post> postArray = new ArrayList<Post>();
    ArrayList<Integer> intArray = new ArrayList<Integer>();
    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt = null;

    Twitter twitter = new TwitterFactory().getInstance();
    try {
        User user = twitter.showUser(userName);
        long userID = user.getId();
    } catch (IllegalStateException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (TwitterException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    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 = ?)";
    System.out.println(query);

    String userNames = null;
    String query1 = null;
    try{
        JDBC db = new JDBC();
        conn = db.getConnection();
        pstmt = conn.prepareStatement(query);
        pstmt.setLong(1, userID);
        rs = pstmt.executeQuery();

        Properties config = new Properties();
        config.load(new FileInputStream("C:/Users/L31207/Desktop/FYPJ/OpenCalais/MarcusFYPJ/categories.properties"));
        String[][] categories = fetchArrayFromPropFile("content", config);
        HashMap<String,Integer> hashmap = new HashMap<String,Integer>();
        for (String[] mapping : categories)
        {
            hashmap.put(mapping[0], 0);

        }

        while(rs.next()){
            int id = rs.getInt("followerid");
            userNames = rs.getString("username");
            intArray.add(id);
        }   

        ResultSet rs1 = null;
        Connection conn1 = null;
        PreparedStatement pstmt1 = null;

            for(int i=0;i<intArray.size();i++){
                query1 = "Select post.content from twitter_content.post where post.userid = "+intArray.get(i)+"";
                JDBC db1 = new JDBC();
                conn1 = db1.getConnection();
                pstmt1 = conn1.prepareStatement(query1);
                rs1 = pstmt1.executeQuery();
            }
                while(rs1.next()){
                    Post post = new Post(rs.getString(userNames), rs.getString("content"), hashmap);
                    System.out.println(rs.getString("content"));
                    postArray.add(post);
                    System.out.println(postArray.size());
                }
    }
    catch (Exception e) {
        System.err.println(e);
    }
    return postArray;
}
4

1 回答 1

3

您可以简单地使用 Prepared Statement 来注入变量的值。

con = DriverManager.getConnection("...");
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 = ?)";
PreparedStatement prest = con.prepareStatement(sql);
prest.setLong(1,<user_id>);
ResultSet rs1 = prest.executeQuery();
于 2012-11-16T02:11:57.743 回答