Javaee增删改查
时间: 2023-12-26 10:05:24 浏览: 178
java增删改查
以下是JavaEE中增删改查的示例代码:
1. 添加数据(增加):
```java
@Override
public void addUser(User user) {
String sql = "INSERT INTO USER" + "(name,gender,age,address,qq,email)" + "VALUES" + "(?,?,?,?,?,?)";
jdbcTemplate.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail());
}
```
2. 查询数据(查找):
```java
@Override
public User getUserById(int id) {
String sql = "SELECT * FROM USER WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
}
```
3. 更新数据(修改):
```java
@Override
public void updateUser(User user) {
String sql = "UPDATE USER SET NAME = ?,gender = ?,age = ?,address = ?,qq = ?,email = ? WHERE id = ?";
jdbcTemplate.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail(),user.getId());
}
```
4. 删除数据(删除):
```java
@Override
public void deleteUser(int id) {
String sql = "DELETE FROM USER WHERE id = ?";
jdbcTemplate.update(sql, id);
}
```
阅读全文