数据库(二)

更新好友列表

  1. 在ClientLogin中,创建好友列表对象
    String friendString=mess.getContent();
    FriendList friendList=new FriendList(userName,friendString);
    注:在FriendList中,改单参量类型为双参量类型
  2. 从数据库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);替换。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值