更新好友列表
- 在ClientLogin中,创建好友列表对象
String friendString=mess.getContent();
FriendList friendList=new FriendList(userName,friendString);
注:在FriendList中,改单参量类型为双参量类型 - 从数据库relation表中读取好友信息来更新好友列表2.利用服务器发送来的好友名字(friendString)更新好友列表 String[] friendName=friendString.split(" “);
int count=friendName.length;
myFriendListJPanel=new JPanel(new GridLayout(count,1));//网格布局
for(int i=1;i<count;i++){
myFriendJLabel[i]=new JLabel(friendName[i]+”",new ImageIcon(“images/duck.gif”),JLabel.LEFT);更改成字符串形式//myFriendJLabel[i].setEnabled(false);注释掉为了激活好友图标
//if(Integer.parseInt(userName)==i) myFriendJLabel[i].setEnabled(true);
myFriendJLabel[i].addMouseListener(this);
myFriendListJPanel.add(myFriendJLabel[i]);
数据库的封装把和数据库相关的处理放在一个单独的类(YychatDbUtil),使得我们的程序逻辑更清晰。
将StartServer中的使用数据库来验证用户名何密码的五个步骤//使用数据库来验证用户名和密码
///1.加载驱动程序
Class.forName(“com.mysql.jdbc.Driver”);
//2.建立连接,默认GBK
String url=“jdbc:mysql://127.0.0.1:3306/yychat?useUnicode=true&charaterEncoding=UTF-8”;
String dbuser=“root”; String dbpass="";
Connection conn=DriverManager.getConnection(url,dbuser,dbpass);
//3.建立一个prepareStatement S
tring user_Login_Sql=“select * from user where username=? and password=?”;
PreparedStatement ptmt=conn.prepareStatement(user_Login_Sql);
ptmt.setString(1, userName); ptmt.setString(2, passWord);
//4.执行 ResultSet rs=ptmt.executeQuery();
//5.判断结果集 boolean loginSuccess=rs.next();
/放到新建的类(YychatDbUtil)中,
用boolean loginSuccess=YychatDbUtil.looginValidate(userName,passWord)替换
将StartServer中的//从数据库 relation表中读取好友信息来更新好友列表1.服务器读好友数据出来。 /String friend_Relation_Sql=“select slaveuser from relation where majoruser=? and relationtype=‘1’”; ptmt=conn.prepareStatement(friend_Relation_Sql);
ptmt.setString(1 ,userName); rs=ptmt.executeQuery(); String friendString=" “; while(rs.next()){ friendString=friendString+rs.getString(“slaveuser”)+” "; }/用String friendString=YychatDbUtil.getFriendString(userName);替换。