java课程设计实验报告_第1页
java课程设计实验报告_第2页
java课程设计实验报告_第3页
java课程设计实验报告_第4页
java课程设计实验报告_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java语言程序设计综合实验报告系 别: 计算机科学与技术系 学科专业: 信息管理与信息系统 课题题目: qq聊天系统 人员及分工:白 娟()张 丹() 张富利()指导老师: 卢照 2014年6月13日 目录一、课程设计目的3二、课程内容3三、实现代码(部分重要代码,不需要全粘)31.服务器:42.客户端:43.功能实现:74.图形化界面的转换:75.连接数据库代码:8四、运行结果(有必要的截图)111. 服务器启动:112. 客户端启动:123. 登入界面:124. 好友列表:125. 聊天界面:136. 数据库信息:13五、课程总结14六、小组分工141、 课程设计目的运用本学期java课

2、程所学知识制作一个小型的比较完整的小系统,旨在使学生对所学知识有一个整体的运用,了解java功能的强大,对学习java产生兴趣。2、 课程内容基本页面:登入页面,注册页面,好友列表页面,聊天页面,查询好友界面基本功能:注册信息,添加好友,查找好友,聊天三、实现代码(部分重要代码,不需要全粘)1.服务器代码:package com.sram.server;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.ObjectInputStream

3、;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.io.PrintStream;import .ServerSocket;import .Socket;import java.util.HashMap;import java.util.List;import java.util.Map;import com.sram.bean.User;import com.sram.dao.UserDao;public class Server public static Ma

4、p socketMap = new HashMap();public static void main(String args) try ServerSocket ss = new ServerSocket(20001);System.out.println(服务器已启动,准备接收数据);while(true)Socket soc = ss.accept();ServerSocketThread sst = new ServerSocketThread(soc);Thread thr = new Thread(sst);thr.start(); catch (IOException e) /

5、TODO Auto-generated catch blocke.printStackTrace();class ServerSocketThread implements Runnableprivate Socket soc;public ServerSocketThread(Socket soc)this.soc = soc;public void run() System.out.println(接受到数据);UserDao ud = new UserDao();try BufferedReader br = new BufferedReader(new InputStreamReade

6、r(soc.getInputStream();String str = ;while(str = br.readLine()!= null)String strs = str.split(,);/1代表if(strs0.equals(1)String qq = strs1;String pass = strs2;User u = ud.findUser(qq, pass);OutputStream os = soc.getOutputStream();PrintStream ps = new PrintStream(os);if(u!=null)System.out.println(登陆成功)

7、;Server.socketMap.put(u.getQqNum(),soc);ps.println(1,yes,+u.getNickName();ps.flush();List li = ud.FriendList(u);li.add(u);ObjectOutputStream oos = new ObjectOutputStream(soc.getOutputStream();oos.writeObject(li);oos.flush();elseSystem.out.println(登陆失败);ps.println(1,no);if(strs0.equals(2)ObjectInputS

8、tream ois = new ObjectInputStream(soc.getInputStream();try User u = (User) ois.readObject();String qqnum = ud.insertUser(u);OutputStream os = soc.getOutputStream();PrintStream ps = new PrintStream(os);ps.println(2,ok,+qqnum); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printS

9、tackTrace();if(strs0.equals(3)String friendNum = strs1;String userNum = strs2;String message = strs3;System.out.println(好友号码:+friendNum+,用户号码:+userNum+,信息:+message);Socket friendSoc = (Socket) Server.socketMap.get(friendNum);PrintStream ps = new PrintStream(friendSoc.getOutputStream();ps.println(str

10、);ps.flush();if(strs0.equals(4)String qqnum = strs1;User nu = ud.findUserfromQQ(qqnum);OutputStream os = soc.getOutputStream();PrintStream ps = new PrintStream(os);if(nu!=null)ps.println(4,ok);ps.flush();ObjectOutputStream oos = new ObjectOutputStream(soc.getOutputStream();oos.writeObject(nu);oos.fl

11、ush();elseps.println(4,no);ps.flush();if(strs0.equals(5)String qqnum = strs1;ObjectInputStream ois = new ObjectInputStream(soc.getInputStream();User u = (User) ois.readObject();User nu = ud.findUserfromQQ(qqnum);ud.addFriend(u.getId(), nu.getId();OutputStream os = soc.getOutputStream();PrintStream p

12、s = new PrintStream(os);ps.println(5,ok,+u.getQqNum()+,+u.getPass();ps.flush(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();2.客户端:package com.sram.client;import java.io.BufferedReade

13、r;import java.io.IOException;import java.io.InputStreamReader;import .Socket;import java.util.List;import com.sram.bean.User;import com.sram.util.MessageIO;import com.sram.view.ChatUI;import com.sram.view.FriendListUI;import com.sram.view.LoginUI;import com.sram.view.RegisterSuccessUI;import

14、 com.sram.view.SearchFriendUI;import com.sram.view.UIList;public class Client public static Socket soc;public static void main(String args) LoginUI lu = new LoginUI();UIList.UIlist.put(login, lu);ClientThread ct = new ClientThread();new Thread(ct).start();class ClientThread implements Runnablepublic

15、 void run()try BufferedReader br = new BufferedReader(new InputStreamReader(Client.soc.getInputStream();String str = ;while(str = br.readLine()!=null)String strs = str.split(,);if(strs0.equals(1)if(strs1.equals(yes)List li = (List) MessageIO.getObject();FriendListUI fui = new FriendListUI(li);UIList

16、.UIlist.put(friend, fui);UIList.UIlist.get(login).setVisible(false);if(strs0.equals(2)if(strs1.equals(ok)UIList.UIlist.get(register).setVisible(false);/UIList.UIlist.get(login).setVisible(true);new RegisterSuccessUI(strs2);if(strs0.equals(3)ChatUI cu = (ChatUI) UIList.UIlist.get(chatui);cu.jta1.setT

17、ext(strs3);if(strs0.equals(4)SearchFriendUI sfui = (SearchFriendUI) UIList.UIlist.get(search);if(strs1.equals(ok)User u = (User) MessageIO.getObject();sfui.jl2.setText(u.getNickName();elsesfui.jl2.setText(QQ号码错误);if(strs0.equals(5)if(strs1.equals(ok)UIList.UIlist.get(friend).setVisible(false);String

18、 qq = strs2;String pass = strs3;MessageIO.printlnString(1,+qq+,+pass); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();3数据库的连接以及实现功能package com.sram.util;public class MessageIO private static Socket soc;public MessageIO(Socket soc)this.soc = soc;public static void printlnS

19、tring(String messege)try PrintStream ps = new PrintStream(soc.getOutputStream();ps.println(messege);ps.flush(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();public static void printObject(Object obj)OutputStream os;try os = soc.getOutputStream();ObjectOutputStream oos =

20、 new ObjectOutputStream(os);oos.writeObject(obj);oos.flush(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();package com.sram.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;

21、import java.util.ArrayList;import java.util.List;import com.sram.bean.User;import com.sram.util.GetConnection;public class UserDao public User findUser(String name,String pass)Connection conn = null;Statement stmt = null;ResultSet rs = null;try conn = GetConnection.getConnection();stmt = conn.create

22、Statement();rs = stmt.executeQuery(select * from t_user where qqnum=+name+ and password=+pass+);rs.next();User u = new User();u.setId(rs.getInt(id);u.setNickName(rs.getString(nickname);u.setPass(rs.getString(password);u.setQqNum(rs.getString(qqnum);return u; catch (SQLException e) / TODO Auto-genera

23、ted catch blocke.printStackTrace();finallytry if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return null;public String insertUser(User u)Connection conn = null;PreparedStatement ptmt = null;Res

24、ultSet rs = null;String qqnum = null;try conn = GetConnection.getConnection();ptmt = conn.prepareStatement(insert into t_user (qqnum,nickname,password) values(?,?,?);int i = findLastId();qqnum = (20000+i+1)+;ptmt.setString(1,qqnum);ptmt.setString(2, u.getNickName();ptmt.setString(3, u.getPass();bool

25、ean b = ptmt.execute();System.out.println(b);return qqnum; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry if(rs!=null)rs.close();if(ptmt!=null)ptmt.close();if(conn!=null)conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();

26、return qqnum;public int findLastId()Connection conn = null;Statement stmt = null;ResultSet rs = null;try conn = GetConnection.getConnection();stmt = conn.createStatement();rs = stmt.executeQuery(select id from t_user order by id desc limit 1);if(rs.next()return rs.getInt(id);elsereturn 0; catch (SQL

27、Exception e) / TODO Auto-generated catch blocke.printStackTrace();finallytry if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return 0;public List FriendList(User u)Connection conn = null;Stateme

28、nt stmt = null;ResultSet rs = null;try conn = GetConnection.getConnection();stmt = conn.createStatement();int id = u.getId();List li = new ArrayList();rs = stmt.executeQuery(select id,qqnum,nickname from t_user where id in(select friendnum from t_friend where mynum = +id+);while(rs.next()User nu = n

29、ew User();nu.setId(rs.getInt(id);nu.setQqNum(rs.getString(qqnum);nu.setNickName(rs.getString(nickname);li.add(nu);return li; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close(); catch (SQL

30、Exception e) / TODO Auto-generated catch blocke.printStackTrace();return null;public User findUserfromQQ(String qqnum)Connection conn = null;Statement stmt = null;ResultSet rs = null;try conn = GetConnection.getConnection();stmt = conn.createStatement();rs = stmt.executeQuery(select * from t_user wh

31、ere qqnum = +qqnum);User nu = new User();while(rs.next()nu.setId(rs.getInt(id);nu.setQqNum(rs.getString(qqnum);nu.setNickName(rs.getString(nickname);return nu; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close(); catch (SQLException e) / TODO Auto-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论