JavaWEB经典完整实例之JSP+Servlet+DAO代码Word版Word文档下载推荐.docx
- 文档编号:22198179
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:18
- 大小:18.92KB
JavaWEB经典完整实例之JSP+Servlet+DAO代码Word版Word文档下载推荐.docx
《JavaWEB经典完整实例之JSP+Servlet+DAO代码Word版Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《JavaWEB经典完整实例之JSP+Servlet+DAO代码Word版Word文档下载推荐.docx(18页珍藏版)》请在冰豆网上搜索。
publicvoidsetPassword(Stringpassword){
this.password=password;
publicUser(Stringname,Stringpassword){
super();
publicUser(){
@Override
publicStringtoString(){
return"
UserDAO[name="
+name+"
password="
+password+"
]"
;
}
UserDao.java
packagecom.zuijiajiao.servlab.DatabaseWEB.DAO;
importjava.io.Serializable;
importjava.util.List;
importcom.zuijiajiao.servlab.DatabaseWEB.DTO.User;
publicinterfaceUserDAO{
publicvoiddelete(Useruser)throwsClassNotFoundException;
publicvoidupdate(Useruser)throwsClassNotFoundException;
publicList<
User>
findAll(Stringname,Stringpassword)throwsClassNotFoundException;
findAll()throwsClassNotFoundException;
publicUserfindByNameAndPassword(Stringname,Stringpaaword)throwsClassNotFoundException;
publicSerializablesave(Useruser)throwsClassNotFoundException;
publicUserget(Serializableid)throwsClassNotFoundException;
UserDaoImpl.java
packagecom.zuijiajiao.servlab.DatabaseWEB.DAOImpl;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.ArrayList;
importcom.zuijiajiao.servlab.DatabaseWEB.DAO.UserDAO;
importcom.zuijiajiao.servlab.DatabaseWEB.JDBC.ConnectionFacroty;
publicclassUserDAOImplimplementsUserDAO{
Connectionconnection=null;
PreparedStatementpreparedStatemen=null;
ResultSetresultSet=null;
publicvoiddelete(Useruser)throwsClassNotFoundException{
try{
StringBuffersql=newStringBuffer();
sql.append("
DELETE*FROMtb_user"
);
WHERE?
=?
"
connection=ConnectionFacroty.getConnection();
preparedStatemen=connection.prepareStatement(sql.toString());
preparedStatemen.setString(1,"
id"
preparedStatemen.setInt(2,3);
intresult=preparedStatemen.executeUpdate();
if(result!
=0){
System.out.println("
成功修改"
+result+"
条记录"
}else{
0条记录被修改,修改失败"
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
ConnectionFacroty.close(connection,preparedStatemen,resultSet);
}
publicvoidupdate(Useruser)throwsClassNotFoundException{
UPDATEtb_user"
SETname=?
SETpassword=?
WHEREid=?
preparedStatemen.setString(1,user.getName());
preparedStatemen.setString(2,user.getPassword());
preparedStatemen.setInt(3,user.getId());
findAll(Stringname,Stringpassword)throwsClassNotFoundException{
SELECTid,name,password"
FROMtb_user"
WHEREname=?
ANDpassword=?
preparedStatemen.setString(1,name);
preparedStatemen.setString(2,password);
resultSet=preparedStatemen.executeQuery();
List<
users=newArrayList<
();
while(resultSet.next()){
Useruser=newUser();
user.setName(resultSet.getString(name));
user.setPassword(resultSet.getString(password));
user.setId(resultSet.getInt("
));
users.add(user);
returnusers;
returnnull;
publicUserfindByNameAndPassword(Stringname,Stringpassword)throwsClassNotFoundException{
Useruser=newUser();
while(resultSet.next()){//if(resultSet.next()){
returnuser;
publicSerializablesave(Useruser)throwsClassNotFoundException{
INSERTINTOtb_user(name,password)"
VALUES(?
?
)"
preparedStatemen.executeUpdate();
resultSet=preparedStatemen.getGeneratedKeys();
while(resultSet.next()){
intid=resultSet.getInt
(1);
idis:
"
+id);
returnid;
publicUserget(Serializableid)throwsClassNotFoundException{
preparedStatemen.setInt(1,(int)id);
user.setName(resultSet.getString("
name"
user.setPassword(resultSet.getString("
password"
user.setId(resultSet.getInt((int)id));
findAll()throwsClassNotFoundException{
SELECT*FROMtb_user"
ConnectionFactory.java
packagecom.zuijiajiao.servlab.DatabaseWEB.JDBC;
importjava.sql.DriverManager;
importjava.sql.Statement;
/**
*ConnectionFactory--链接工厂
*
*从面向对象的角度来说,凡是和链接相关的基本操作都交给这个对象来处理即可
*将基本代码封装成一个对象,从而就可以省去不少事
*基本操作包括
*1.获取连接
*2.关闭资源
**/
publicclassConnectionFacroty{
//定义数据库参数
privatestaticfinalStringURL="
jdbc:
mysql:
//localhost:
3306/databaseWeb"
privatestaticfinalStringDRIVER="
com.mysql.jdbc.Driver"
privatestaticfinalStringUSER="
root"
privatestaticfinalStringpassword="
19950412"
//获取链接
publicstaticConnectiongetConnection()throwsClassNotFoundException{
Connectionconnection;
Class.forName(DRIVER);
//注册驱动
connection=DriverManager.getConnection(URL,USER,password);
//获取链接
System.out.println("
################################数据库连接错误#############################"
returnnull;
returnconnection;
//关闭数据库资源
publicstaticvoidclose(Connectionconnection){
if(connection!
=null)
connection.close();
##################################数据库连接错误############################"
publicstaticvoidclose(Statementstatement,ResultSetresultSet){
if(statement!
statement.close();
if(resultSet!
=null)
resultSet.close();
publicstaticvoidclose(Connectionconnec
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JavaWEB 经典 完整 实例 JSP Servlet DAO 代码 Word
链接地址:https://www.bdocx.com/doc/22198179.html