JavaWeb实现增删改查
时间: 2023-07-07 08:38:23 浏览: 476
JavaWeb实现增删改查一般需要以下步骤:
1. 编写数据库表结构
2. 编写JavaBean类,对应数据库表结构
3. 编写DAO层,实现增删改查操作
4. 编写Service层,调用DAO层实现业务逻辑
5. 编写Controller层,接收请求,调用Service层处理请求,返回响应
下面是一个简单的示例:
1. 编写数据库表结构
```
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
2. 编写JavaBean类
```
public class User {
private int id;
private String name;
private int age;
private String gender;
// 省略getter和setter方法
}
```
3. 编写DAO层
```
public class UserDao {
private Connection conn;
public UserDao(Connection conn) {
this.conn = conn;
}
public void add(User user) {
PreparedStatement pstmt = null;
try {
String sql = "INSERT INTO user(name, age, gender) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getGender());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
}
}
public void delete(int id) {
PreparedStatement pstmt = null;
try {
String sql = "DELETE FROM user WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
}
}
public void update(User user) {
PreparedStatement pstmt = null;
try {
String sql = "UPDATE user SET name=?, age=?, gender=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getGender());
pstmt.setInt(4, user.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
}
}
public User getById(int id) {
PreparedStatement pstmt = null;
ResultSet rs = null;
User user = null;
try {
String sql = "SELECT * FROM user WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setGender(rs.getString("gender"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
}
return user;
}
public List<User> getAll() {
PreparedStatement pstmt = null;
ResultSet rs = null;
List<User> userList = new ArrayList<>();
try {
String sql = "SELECT * FROM user";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setGender(rs.getString("gender"));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
}
return userList;
}
}
```
4. 编写Service层
```
public class UserService {
private UserDao userDao;
public UserService(UserDao userDao) {
this.userDao = userDao;
}
public void add(User user) {
userDao.add(user);
}
public void delete(int id) {
userDao.delete(id);
}
public void update(User user) {
userDao.update(user);
}
public User getById(int id) {
return userDao.getById(id);
}
public List<User> getAll() {
return userDao.getAll();
}
}
```
5. 编写Controller层
```
public class UserController {
private UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
public void add(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
User user = new User();
user.setName(name);
user.setAge(age);
user.setGender(gender);
userService.add(user);
// 返回响应
}
public void delete(HttpServletRequest request, HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("id"));
userService.delete(id);
// 返回响应
}
public void update(HttpServletRequest request, HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
user.setGender(gender);
userService.update(user);
// 返回响应
}
public void getById(HttpServletRequest request, HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("id"));
User user = userService.getById(id);
// 返回响应
}
public void getAll(HttpServletRequest request, HttpServletResponse response) {
List<User> userList = userService.getAll();
// 返回响应
}
}
```
阅读全文