java课程设计实验报告Word下载.docx
- 文档编号:20427910
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:20
- 大小:74.15KB
java课程设计实验报告Word下载.docx
《java课程设计实验报告Word下载.docx》由会员分享,可在线阅读,更多相关《java课程设计实验报告Word下载.docx(20页珍藏版)》请在冰豆网上搜索。
1.服务器启动:
11
2.客户端启动:
12
3.登入界面:
4.好友列表:
5.聊天界面:
13
6.数据库信息:
五、课程总结14
六、小组分工14
1、课程设计目的
运用本学期java课程所学知识制作一个小型的比较完整的小系统,旨在使学生对所学知识有一个整体的运用,了解java功能的强大,对学习java产生兴趣。
2、课程内容
基本页面:
登入页面,注册页面,好友列表页面,聊天页面,查询好友界面
基本功能:
注册信息,添加好友,查找好友,聊天
三、实现代码(部分重要代码,不需要全粘)
1.服务器代码:
packagecom.sram.server;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.OutputStream;
importjava.io.PrintStream;
import.ServerSocket;
import.Socket;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importcom.sram.bean.User;
importcom.sram.dao.UserDao;
publicclassServer{
publicstaticMapsocketMap=newHashMap();
publicstaticvoidmain(String[]args){
try{
ServerSocketss=newServerSocket(20001);
System.out.println("
服务器已启动,准备接收数据"
);
while(true){
Socketsoc=ss.accept();
ServerSocketThreadsst=newServerSocketThread(soc);
Threadthr=newThread(sst);
thr.start();
}
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
classServerSocketThreadimplementsRunnable{
privateSocketsoc;
publicServerSocketThread(Socketsoc){
this.soc=soc;
publicvoidrun(){
System.out.println("
接受到数据"
UserDaoud=newUserDao();
BufferedReaderbr=newBufferedReader(newInputStreamReader(soc.getInputStream()));
Stringstr="
"
;
while((str=br.readLine())!
=null){
String[]strs=str.split("
"
//1代表
if(strs[0].equals("
1"
)){
Stringqq=strs[1];
Stringpass=strs[2];
Useru=ud.findUser(qq,pass);
OutputStreamos=soc.getOutputStream();
PrintStreamps=newPrintStream(os);
if(u!
=null){
System.out.println("
登陆成功"
Server.socketMap.put(u.getQqNum(),soc);
ps.println("
1,yes,"
+u.getNickName());
ps.flush();
List<
User>
li=ud.FriendList(u);
li.add(u);
ObjectOutputStreamoos=newObjectOutputStream(soc.getOutputStream());
oos.writeObject(li);
oos.flush();
}else{
登陆失败"
1,no"
}
}
2"
ObjectInputStreamois=newObjectInputStream(soc.getInputStream());
try{
Useru=(User)ois.readObject();
Stringqqnum=ud.insertUser(u);
OutputStreamos=soc.getOutputStream();
PrintStreamps=newPrintStream(os);
2,ok,"
+qqnum);
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
3"
StringfriendNum=strs[1];
StringuserNum=strs[2];
Stringmessage=strs[3];
System.out.println("
好友号码:
+friendNum+"
用户号码:
+userNum+"
信息:
+message);
SocketfriendSoc=(Socket)Server.socketMap.get(friendNum);
PrintStreamps=newPrintStream(friendSoc.getOutputStream());
ps.println(str);
ps.flush();
4"
Stringqqnum=strs[1];
Usernu=ud.findUserfromQQ(qqnum);
if(nu!
4,ok"
oos.writeObject(nu);
4,no"
5"
Useru=(User)ois.readObject();
ud.addFriend(u.getId(),nu.getId());
ps.println("
5,ok,"
+u.getQqNum()+"
+u.getPass());
}catch(ClassNotFoundExceptione){
packagecom.sram.client;
importcom.sram.util.MessageIO;
importcom.sram.view.ChatUI;
importcom.sram.view.FriendListUI;
importcom.sram.view.LoginUI;
importcom.sram.view.RegisterSuccessUI;
importcom.sram.view.SearchFriendUI;
importcom.sram.view.UIList;
publicclassClient{
publicstaticSocketsoc;
LoginUIlu=newLoginUI();
UIList.UIlist.put("
login"
lu);
ClientThreadct=newClientThread();
newThread(ct).start();
classClientThreadimplementsRunnable{
publicvoidrun(){
BufferedReaderbr=newBufferedReader(newInputStreamReader(Client.soc.getInputStream()));
if(strs[1].equals("
yes"
li=(List<
)MessageIO.getObject();
FriendListUIfui=newFriendListUI(li);
UIList.UIlist.put("
friend"
fui);
UIList.UIlist.get("
).setVisible(false);
ok"
register"
//UIList.UIlist.get("
).setVisible(true);
newRegisterSuccessUI(strs[2]);
ChatUIcu=(ChatUI)UIList.UIlist.get("
chatui"
cu.jta1.setText(strs[3]);
SearchFriendUIsfui=(SearchFriendUI)UIList.UIlist.get("
search"
Useru=(User)MessageIO.getObject();
sfui.jl2.setText(u.getNickName());
sfui.jl2.setText("
QQ号码错误"
Stringqq=strs[2];
Stringpass=strs[3];
MessageIO.printlnString("
1,"
+qq+"
+pass);
3.数据库的连接以及实现功能
packagecom.sram.util;
publicclassMessageIO{
privatestaticSocketsoc;
publicMessageIO(Socketsoc){
publicstaticvoidprintlnString(Stringmessege){
PrintStreamps=newPrintStream(soc.getOutputStream());
ps.println(messege);
ps.flush();
publicstaticvoidprintObject(Objectobj){
OutputStreamos;
os=soc.getOutputStream();
ObjectOutputStreamoos=newObjectOutputStream(os);
oos.writeObject(obj);
oos.flush();
}
packagecom.sram.dao;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;
importcom.sram.util.GetConnection;
publicclassUserDao{
publicUserfindUser(Stringname,Stringpass){
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
conn=GetConnection.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery("
select*fromt_userwhereqqnum='
+name+"
'
andpassword='
+pass+"
rs.next();
Useru=newUser();
u.setId(rs.getInt("
id"
));
u.setNickName(rs.getString("
nickname"
u.setPass(rs.getString("
password"
u.setQqNum(rs.getString("
qqnum"
returnu;
}catch(SQLExceptione){
}finally{
try{
if(rs!
rs.close();
if(stmt!
stmt.close();
if(conn!
conn.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnnull;
publicStringinsertUser(Useru){
PreparedStatementptmt=null;
Stringqqnum=null;
ptmt=conn.prepareStatement("
insertintot_user(qqnum,nickname,password)values(?
?
)"
inti=findLastId();
qqnum=(20000+i+1)+"
ptmt.setString(1,qqnum);
ptmt.setString(2,u.getNickName());
ptmt.setString(3,u.getPass());
booleanb=ptmt.execute();
System.out.println(b);
returnqqnum;
try{
if(rs!
rs.close();
if(ptmt!
ptmt.close();
if(conn!
conn.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnqqnum;
publicintfindLastId(){
selectidfromt_userorderby
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 课程设计 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)