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 に答える