使用struts2、数据库、JSP实现增、删、改、查源码

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Action 包下面的class文件:
package action;
import erDao;
import erDaoImp;
import er;
public class UserAction {
private User user;
public String add(){
UserDao userDao = new UserDaoImp();
try {
userDao.save(user);
return"success";
} catch (Exception e) {
e.printStackTrace();
return"error";
}
}
public String update(){
UserDao userDao = new UserDaoImp();
try {
userDao.updata(user);
System.out.print("----");
return"success";
} catch (Exception e) {
e.printStackTrace();
return"error";
}
}
public String delete(){
UserDao userDao = new UserDaoImp();
try {
userDao.deleteById(user.getId());
return"success";
} catch (Exception e) {
e.printStackTrace();
return"error";
}
}
public String show(){
UserDao userDao = new UserDaoImp();
try {
user = userDao.findById(user.getId());
return"success";
} catch (Exception e) {
e.printStackTrace();
return"error";
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
er = user;
}
}
package action;
import java.util.List;
import erDao;
import erDaoImp;
import er;
public class UserListAction {
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) { ers = users;
}
public String execute(){
UserDao userDao = new UserDaoImp();
try {
users = userDao.findAll();
return"success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return"error";
}
}
}
Dao 下面的class文件
package dao;
import java.util.List;
import er;
public interface UserDao {
public void save(User user) throws Exception;
public void updata(User user) throws Exception;
public void deleteById(int id) throws Exception;
public User findById(int id) throws Exception;
public List<User> findAll() throws Exception;
}
Daoimpl包下面的class文件
package daoimp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.DBUtil;
import erDao;
import er;
public class UserDaoImp implements UserDao {
static final String insert = "insert into t_user(username,pwd,age) values(?,?,?)";
static final String findAll = "select * from t_user";
static final String delete = "delete from t_user where id=?";
static final String findById = "select * from t_user where id=?";
static final String update = "update t_user set username=?,pwd=?,age=? where id=?";
public List<User> findAll() throws Exception {
Connection con = DBUtil.getConnection();
PreparedStatement pst = con.prepareStatement(findAll);
ResultSet rs = pst.executeQuery();
List<User> users = new ArrayList<User>();
while(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("pwd"));
user.setAge(rs.getInt("age"));
users.add(user);
}
con.close();
return users;
}
public User findById(int id) throws Exception {
Connection con = DBUtil.getConnection();
PreparedStatement pst = con.prepareStatement(findById);
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("pwd"));
user.setAge(rs.getInt("age"));
return user;
}
DBUtil.close(con);
return null;
}
@Override
public void deleteById(int id) throws Exception {
// TODO Auto-generated method stub
Connection con = DBUtil.getConnection();
PreparedStatement pstmt = con.prepareStatement(delete);
pstmt.setInt(1, id);
pstmt.executeUpdate();
con.close();
}
@Override
public void save(User user) throws Exception {
// TODO Auto-generated method stub
Connection con = DBUtil.getConnection();
PreparedStatement pstmt = con.prepareStatement(insert);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getAge());
pstmt.executeUpdate();
con.close();
}
@Override
public void updata(User user) throws Exception {
// TODO Auto-generated method stub
Connection con = DBUtil.getConnection();
PreparedStatement pstmt = con.prepareStatement(update);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getAge());
pstmt.setInt(4, user.getId());
pstmt.executeUpdate();
con.close();
}
}
实体层entity包下面的class文件:
package entity;
public class User {
private int id;
private String username;
private String password;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
ername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
JSP文件:
Add.jsp
<body>
<form action="add.action"method="post">
<table border="1"align="center">
<tr>
<td>UserName:</td>
<td><input type="text"name="ername"></td> </tr>
<tr>
<td>PassWord:</td>
<td><input type="password"name="user.password"></td> </tr>
<tr>
<td>Age:</td>
<td><input type="text"name="user.age"></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit"value="添加">
<a href="list.action">查看用户列表</a>
</td>
</tr>
</table>
</form>
<!--
<h1>添加</h1>
<form action="add.action" method="post">
用户名:<input type="text" name="ername"><br/>
密&nbsp;&nbsp;码:<input type="password" name="user.pwd"><br/> name="user.pwd"><br/>
年&nbsp;&nbsp;龄:<input type="text" name="user.age"><br/>
<input type="submit" value="添加">
<a href="list.action">查看用户列表</a>
</form>
-->
</body>
Error.jsp
<body>
<h1>系统错误!请稍候重试....!</h1><br>
</body>
List.jsp
<body>
<table border="1"align="center">
<tr>
<th>ID:</th>
<th>UserName:</th>
<th>Age:</th>
<th>相关操作</th>
</tr>
<s:iterator value="users"><!-- 遍历 value是集合的对象 --> <tr>
<td><s:property value="id"/></td>
<td><s:property value="username"/></td>
<td><s:property value="age"/></td>
<td>
<a href='delete.action?user.id=<s:property value="id"/>'>删除</a>
<a href='show.action?user.id=<s:property value="id"/>'>查看</a>
<a href='update_init.action?user.id=<s:property value="id"/>'>修改</a>
</td>
</tr>
</s:iterator>
<tr align="center">
<td colspan="4"><a href="add.jsp">添加</a></td> </tr>
</table>
<br/>
</body>
Show.jsp
<body>
用户名:<s:property value="ername"/><br>
密&nbsp;&nbsp;码:<s:property value="user.password"/><br>
年&nbsp;&nbsp;龄:<s:property value="user.age"/><br>
<a href="list.action">返回</a>
</body>
Update.jsp
<body>
<h1>用户更新</h1>
<s:form action="update"theme="simple"method="post">
<s:textfield name="user.id"/>
用户名:<s:textfield name="ername"/><br/>
密&nbsp;&nbsp;码:<s:password name="user.password" showPassword="true"/><br/>
年&nbsp;&nbsp;龄:<s:textfield name="user.age"/><br/>
<input type="submit"value="更新">
<a href="list.action">查看用户列表</a>
</s:form>
</body>。

相关文档
最新文档