数据库访问技术.docx
- 文档编号:3166829
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:16
- 大小:2.71MB
数据库访问技术.docx
《数据库访问技术.docx》由会员分享,可在线阅读,更多相关《数据库访问技术.docx(16页珍藏版)》请在冰点文库上搜索。
数据库访问技术
实验项目名称:
数据库访问技术
(所属课程:
web系统与技术)
学院:
专业班级:
姓名:
学号:
实验日期:
实验地点:
合作者:
指导教师:
本实验项目成绩:
教师签字:
日期:
1、实验目的
(1)掌握JDBC的基本使用步骤。
(2)掌握数据源和规范的JDBC代码。
(3)掌握Spring的SimpleJdbcTemplate模板类常用方法。
二、实验条件
安装了Web开发环境的微机。
3、实验内容
(1)完成聊天室数据库设计以及三个实体类的数据访问代码。
(2)修改注册、登录、聊天三个功能模块的后台业务代码。
四、实验要求
(1)完善三个实体类的属性设计、构造方法等。
(2)设计聊天室数据库概念模型E-R图以及逻辑结构。
(3)完成JDBC的工具类和MD5加密工具类,配置DBCP数据源。
(4)设计三个实体类的数据访问DAO层接口。
(5)完成对三个接口的测试用例的开发。
(6)*使用Spring的SimpleJdbcTemplate模板类完成三个接口的实现。
(7)修改注册、登录、聊天三个功能模块的Servlet代码。
五、实验步骤
1、实体类的设计(用户、聊天室、消息)
首先建立三个实体类分别为ChatRoom,User和Message,注册窗口用户所填写的信息列表应该设计的和User类里边的成员变量一致,比如username,passWord等,Message类的设计应该包括信息发送者的Id,信息发送者,信息接受者,发送内容等以及相应的成员函数和StringtoString函数,ChatRoom类应该包括成员变量chatRoomId,chatRoomName等以及相应的成员函数。
(1)User类的重要实现代码如下:
privateStringuserName;//用户名
privateStringpassword;//密码
privatebooleansex;//性别
privateStringarea1;//国家
privateStringarea2;//城市
privateStringbirth1;//出生的年份
privateStringbirth2;//出生的月份
privateStringbirth3;//出生的日期
privateintchatRoomId;//聊天室的Id
publicUser(Stringusername,intchatroomId,StringpassWord,booleansex,Stringarea1,Stringarea2,Stringbirth1,Stringbirth2,Stringbirth3){
super();
this.userName=username;
this.chatRoomId=chatroomId;
this.password=passWord;
this.sex=sex;
this.area1=area1;
this.area2=area2;
this.birth1=birth1;
this.birth2=birth2;
this.birth3=birth3;}
(2)Message类的重要实现代码如下:
privateintmessageId;//信息id号
privateStringsender;//发送者
privateStringreceiver;//接收者
privateStringbody;//发送文本
privateDatetime;//发送时间
privateintchatRoomId;//聊天室的Id
privateinttype;//消息的类型
//普通消息
publicMessage(Stringsender,Stringreceiver,Stringbody,intchatRoomId){super();
this.sender=sender;
this.receiver=receiver;
this.body=body;
this.chatRoomId=chatRoomId;
this.type=0;
this.time=newDate();}
//系统消息
publicMessage(Stringsender,Stringbody,intchatRoomId){
super();
this.sender=sender;
this.body=body;
this.chatRoomId=chatRoomId;
this.type=1;
this.time=newDate();}
(3)ChatRoom类的重要实现代码如下:
privateintchatRoomId;//聊天室的Id
privateStringchatRoomName;//聊天室名字
privateLinkedList
privateLinkedList
publicChatRoom(){
this.users=newLinkedList
this.messages=newLinkedList
}//带参数的ChatRoom类构造函数
2、注册、登录、聊天页面的功能业务设计(servlet的构造)
分别建立三个servlet类,名字分别为Regist、Chat、Login。
Regist.java主要完成的功能为检测用户注册信息是否完整,用户密码是否一致,提示用户注册成功后跳转到登录界面,Chat.java主要是判断用户是否已经注册过和密码的是否正确,chat使用户能够聊天并显示登录列表。
(1)Regist.java的实现代码:
privateUserDaouserDao=newUserDaoSpringSimple();
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
StringuserName=request.getParameter("username");
StringpassWord=request.getParameter("password");
booleansex=request.getParameter("sex").equals("f")?
true:
false;
Stringarea1=request.getParameter("selectcounity");
Stringarea2=request.getParameter("selectcity");
Stringbirth1=request.getParameter("selectyear");
Stringbirth2=request.getParameter("selectmonth");
Stringbirth3=request.getParameter("selectday");
Useruser=newUser(userName,0,MD5Util.md5(passWord),sex,area1,area2,birth1,birth2,birth3);
out.println(user.getUserName());
if(userDao.getUser(user.getUserName())==null){
userDao.addUser(user);
out.println("注册成功!
}else
out.println("用户已经占用");
(2)Login.java的实现代码:
privateUserDaouserDao=newUserDaoSpringSimple();
privateMessageDaomessageDao=newMessageDaoSpringSimple();
privateChatRoomDaochatRoomDao=newchatRoomDaoSpringSimple();
privateintchatRoomId=1;
if(user==null)
out.println("用户没有注册
elseif(user.getChatRoomId()!
=0)out.println("你已在聊天室中!
"+"
elseif(!
user.getPassword().equals(MD5Util.md5(passWord)))
{
out.println("密码错误!
亲爱的“"+username+"”你输入的密码有误,
");
}
else{
//更新用户的状态
user.setChatRoomId(chatRoomId);
//持久化到数据库中
userDao.updateUser(user);
//生成系统消息
Messagemes=newMessage(user.getUserName(),"进入了聊天室",chatRoomId);
//保存到数据库
messageDao.addMessage(mes);
//将chatRoom信息保存到application中
ChatRoomchatRoom=chatRoomDao.getChatRoom(chatRoomId);
chatRoom.getUsers().add(user);
chatRoom.getMessages().add(mes);
getServletContext().setAttribute("chatRoom",chatRoom);
session.setAttribute("user",user);
response.sendRedirect("../chatroom.html");
}
(3)Chat.java的实现代码:
privateUserDaouserDao=newUserDaoSpringSimple();
privateMessageDaomessageDao=newMessageDaoSpringSimple();
privateChatRoomDaochatRoomDao=newchatRoomDaoSpringSimple();
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
StringinputWord=request.getParameter("inputWord");
Useruser=(User)request.getSession().getAttribute("user");
out.println(user.getUserName());
ChatRoomchatRoom=(ChatRoom)this.getServletContext().getAttribute("chatRoom");
if(inputWord!
=null||!
inputWord.equals("")){
Messagemessage=newMessage(user.getUserName(),"liuxianli",inputWord,user.getChatRoomId());
messageDao.addMessage(message);
//将chatRoom信息保存到application中
//chatRoom=chatRoomDao.getChatRoom(user.getChatRoomId());
chatRoom.getMessages().addFirst(message);
getServletContext().setAttribute("chatRoom",chatRoom);
}
response.sendRedirect("../CR2.jsp");
}
(4)Logout.java的实现代码:
privateUserDaouserDao=newUserDaoSpringSimple();
privateMessageDaomessageDao=newMessageDaoSpringSimple();
privateChatRoomDaochatRoomDao=newchatRoomDaoSpringSimple();
privateintchatRoomId=0;
if(user==null){
out.println("用户没有登录
}else{
//更新用户的状态
user.setChatRoomId(chatRoomId);
//持久化到数据库中
userDao.updateUser(user);
//生成系统消息
Messagemes=newMessage(user.getUserName(),"离开了聊天室",chatRoomId);
//保存到数据库
messageDao.addMessage(mes);
//将chatRoom信息保存到application中
ChatRoomchatRoom=(ChatRoom)this.getServletContext().getAttribute("chatRoom");
//ChatRoomchatRoom=chatRoomDao.getChatRoom(chatRoomId);
chatRoom.getUsers().remove(user);
chatRoom.getMessages().addFirst(mes);
//getServletContext().setAttribute("chatRoom",chatRoom);
session.removeAttribute("user");
response.sendRedirect("../chatroom.html");
}
3、连接数据库和密码加密
(1)连接数据库的主要实现代码如下(JDBC):
publicfinalclassjdbcUtil{
privatestaticDataSourcedataSource;
static{
try{//注册驱动,只需做一次
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Propertiesproperties=newProperties();
properties.load(jdbcUtil.class.getClassLoader()
.getResourceAsStream("dbcpconfig.properties"));
dataSource=BasicDataSourceFactory.createDataSource(properties);
}catch(Exceptione){
e.printStackTrace();
thrownewExceptionInInitializerError(e);}}
publicstaticDataSourcegetDataSource(){
returndataSource;
}*获取连接*/
publicstaticConnectiongetConnection()throwsSQLException{
returndataSource.getConnection();}
*释放资源
publicstaticvoidfree(ResultSetrs,Statementst,Connectioncon){
try{
if(rs!
=null)
rs.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
if(st!
=null)
st.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
if(con!
=null)
con.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();}}}}
(2)密码加密的主要实现代码如下
publicstaticStringmd5(Stringstr){
try{
MessageDigestmd=MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[]byteDigest=md.digest();
inti;
StringBufferbuf=newStringBuffer("");
for(intoffset=0;offset i=byteDigest[offset]; if(i<0) i+=256; if(i<16) buf.append("0"); buf.append(Integer.toHexString(i));} //32位加密 returnbuf.toString(); //16位的加密 //returnbuf.toString().substring(8,24); }catch(NoSuchAlgorithmExceptione){ e.printStackTrace(); returnnull;}} (3)创建数据库的SQL语句如下(weiliao.sql): createtablechatRoom( chatRoom_idintnotnull, chatRoom_namevarchar(10)null, constraintPK_CHATROOMprimarykeynonclustered(chatRoom_id) ); createtablemessage( message_idintidentity(1,1), sendervarchar(10)null, receivervarchar(10)null, chatRoom_idintnull, bodyvarchar(500)null, timedatetimenull, typeintnull, constraintPK_MESSAGEprimarykeynonclustered(message_id) ); createtableusers( user_namevarchar(10)notnull, chatRoom_idintnull, passwordvarchar(32)null, sexbitnull, area1varchar(30)null, area2varchar(30)null, birth1varchar(10)null, birth2varchar(10)null, birth3varchar(10)null, constraintPK_USERSprimarykeynonclustered(user_name) ); insertintochatRoomvalues(0,'离线'); insertintochatRoomvalues(1,'安工聊天室'); insertintochatRoomvalues(2,'计科聊天室'); select*fromusers; select*frommessage; select*fromchatRoom; (4)chatRoomDaoSpringSimple类主要代码: publicclasschatRoomDaoSpringSimpleimplementsChatRoomDao{ privateSimpleJdbcTemplatesimple=newSimpleJdbcTemplate(jdbcUtil.getDataSource()); publicChatRoomgetChatRoom(intchatRoomId){ Stringsql="selectchatRoom_idchatRoomId,chatRoom_namechatRoomNamefromchatRoomwherechatRoom_id=? "; ChatRoomchatRoom=simple.queryForObject(sql,newBeanPropertyRowMapper returnchatRoom; }//通过chatRoomId查找聊天室 } (5)MessageDaoSp
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 访问 技术