import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class SocialNetwork {
private Connection connection; // 假设已建立数据库连接
public SocialNetwork(Connection connection) {
this.connection = connection;
}
public ArrayList listMutualFollowers(int id_user) {
ArrayList
代码解释
SQL查询语句: 使用了包含子查询的SQL语句,从 followers 表中查找共同关注者。"SELECT id_follower FROM followers a WHERE id_follower IN (SELECT id_follower FROM followers b WHERE a.id_user = b.id_user) and id_user = ?"
外层查询 SELECT id_follower FROM followers a WHERE ... and id_user = ? 用于筛选出指定用户 (id_user) 的关注者。
内层查询 SELECT id_follower FROM followers b WHERE a.id_user = b.id_user 用于找出所有用户,这些用户也关注了和外层查询相同的人。