我有以下代码:

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;
}

最佳答案

您可以简单地使用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();

关于java - 如何在Java中的嵌套sql语句中串联动态值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13409353/

10-10 19:31