java增删改查经典案例
时间: 2023-10-14 20:11:06 浏览: 158
经典jdbc增删改查
Java中的经典增删改查案例包括以下几种:
1. 基于JDBC的增删改查
使用JDBC连接数据库,执行SQL语句进行增删改查操作。示例代码如下:
```java
import java.sql.*;
public class JdbcDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1. 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 获取连接
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
// 3. 执行SQL语句
// 插入数据
String sql = "insert into user(name, age) values(?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "张三");
ps.setInt(2, 20);
ps.executeUpdate();
// 查询数据
sql = "select * from user";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getInt("age"));
}
// 更新数据
sql = "update user set age=? where name=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, 25);
ps.setString(2, "张三");
ps.executeUpdate();
// 删除数据
sql = "delete from user where name=?";
ps = conn.prepareStatement(sql);
ps.setString(1, "张三");
ps.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 4. 释放资源
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
2. 基于MyBatis的增删改查
使用MyBatis ORM框架进行数据库操作。示例代码如下:
```java
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisDemo {
public static void main(String[] args) {
// 1. 加载配置文件
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(MybatisDemo.class.getClassLoader().getResourceAsStream("mybatis-config.xml"));
// 2. 获取SqlSession
SqlSession session = factory.openSession();
try {
// 3. 执行SQL语句
// 插入数据
User user = new User();
user.setName("张三");
user.setAge(20);
session.insert("UserMapper.insert", user);
// 查询数据
List<User> users = session.selectList("UserMapper.selectAll");
for (User u : users) {
System.out.println(u.getId() + "\t" + u.getName() + "\t" + u.getAge());
}
// 更新数据
User updateUser = new User();
updateUser.setName("张三");
updateUser.setAge(25);
session.update("UserMapper.update", updateUser);
// 删除数据
session.delete("UserMapper.delete", "张三");
// 提交事务
session.commit();
} catch (Exception e) {
// 回滚事务
session.rollback();
e.printStackTrace();
} finally {
// 4. 释放资源
session.close();
}
}
}
```
3. 基于Spring JDBC的增删改查
使用Spring JDBC模块进行数据库操作。示例代码如下:
```java
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class SpringJdbcDemo {
public static void main(String[] args) {
// 1. 创建数据源
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// 2. 创建JdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
try {
// 3. 执行SQL语句
// 插入数据
String sql = "insert into user(name, age) values(?, ?)";
jdbcTemplate.update(sql, "张三", 20);
// 查询数据
sql = "select * from user";
List<User> users = jdbcTemplate.query(sql, new UserRowMapper());
for (User u : users) {
System.out.println(u.getId() + "\t" + u.getName() + "\t" + u.getAge());
}
// 更新数据
sql = "update user set age=? where name=?";
jdbcTemplate.update(sql, 25, "张三");
// 删除数据
sql = "delete from user where name=?";
jdbcTemplate.update(sql, "张三");
} catch (Exception e) {
e.printStackTrace();
}
}
private static class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
return user;
}
}
}
```
阅读全文