1)在mysql数据库中创建mydb数据库,创建用户表t_user ,表字段如下: 序号 字段名 类型 是否主键、空 1 id int 主键,自增长 2 name Varchar(20) NOT NULL 3 password Varchar(20) NOT NULL 4 email Varchar(20) DEFAULT NULL 2)创建User类,用于封装表数据; 3)编写JDBCUtils工具类,类中要包含获取连接和释放资源的方法 4)创建一个UserDao类,在该类中编写对数据库进行增删改查的方法,包括insert(User user)、update(User user)、delete(int id)、queryAll( )等方法,分别实现插入、更新、删除和查询所有记录的方法。 5)创建测试类Test1,对上述UserDao中定义的方法进行测试输出。 6)尝试使用C3P0数据库连接池和DBUtils工具,完成上述一样的操作,对应dao类为C3P0UserDao,C3P0Utils工具类获取数据源。 7)在C3P0UserDao类中编写对数据库进行增删改查的方法,包括insert(User user)、update(User user)、delete(int id)、queryAll( )等方法,分别实现插入、更新、删除和查询所有记录的方法。 8)创建测试类Test2,对上述C3P0UserDao中定义的方法进行测试输出。
时间: 2024-02-01 20:13:43 浏览: 99
很好,这是一个完整的Java Web项目的实现。我将逐一回答你的问题。
1. 在mysql数据库中创建mydb数据库,创建用户表t_user ,表字段如下:
```sql
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE t_user (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
email VARCHAR(20) DEFAULT NULL
);
```
2. 创建User类,用于封装表数据;
```java
public class User {
private int id;
private String name;
private String password;
private String email;
// getter and setter methods
}
```
3. 编写JDBCUtils工具类,类中要包含获取连接和释放资源的方法
```java
public class JDBCUtils {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/mydb?useSSL=false";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName(DRIVER);
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se2) {
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
}
```
4. 创建一个UserDao类,在该类中编写对数据库进行增删改查的方法,包括insert(User user)、update(User user)、delete(int id)、queryAll( )等方法,分别实现插入、更新、删除和查询所有记录的方法。
```java
public class UserDao {
public void insert(User user) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "INSERT INTO t_user(name, password, email) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, user.getName());
stmt.setString(2, user.getPassword());
stmt.setString(3, user.getEmail());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, stmt, null);
}
}
public void update(User user) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "UPDATE t_user SET name=?, password=?, email=? WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, user.getName());
stmt.setString(2, user.getPassword());
stmt.setString(3, user.getEmail());
stmt.setInt(4, user.getId());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, stmt, null);
}
}
public void delete(int id) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "DELETE FROM t_user WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, stmt, null);
}
}
public List<User> queryAll() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<User> userList = new ArrayList<>();
try {
conn = JDBCUtils.getConnection();
String sql = "SELECT * FROM t_user";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
userList.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, stmt, rs);
}
return userList;
}
}
```
5. 创建测试类Test1,对上述UserDao中定义的方法进行测试输出。
```java
public class Test1 {
public static void main(String[] args) {
UserDao userDao = new UserDao();
// 测试插入
User user1 = new User();
user1.setName("user1");
user1.setPassword("123456");
user1.setEmail("user1@test.com");
userDao.insert(user1);
// 测试更新
User user2 = new User();
user2.setId(1);
user2.setName("user2");
user2.setPassword("654321");
user2.setEmail("user2@test.com");
userDao.update(user2);
// 测试查询
List<User> userList = userDao.queryAll();
for (User user : userList) {
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getPassword() + "\t" + user.getEmail());
}
// 测试删除
userDao.delete(1);
}
}
```
6. 尝试使用C3P0数据库连接池和DBUtils工具,完成上述一样的操作,对应dao类为C3P0UserDao,C3P0Utils工具类获取数据源。
```java
public class C3P0Utils {
private static ComboPooledDataSource dataSource;
static {
try {
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb?useSSL=false");
dataSource.setUser("root");
dataSource.setPassword("password");
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return dataSource;
}
}
```
```java
public class C3P0UserDao {
private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
public void insert(User user) {
String sql = "INSERT INTO t_user(name, password, email) VALUES (?, ?, ?)";
try {
queryRunner.update(sql, user.getName(), user.getPassword(), user.getEmail());
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(User user) {
String sql = "UPDATE t_user SET name=?, password=?, email=? WHERE id=?";
try {
queryRunner.update(sql, user.getName(), user.getPassword(), user.getEmail(), user.getId());
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
String sql = "DELETE FROM t_user WHERE id=?";
try {
queryRunner.update(sql, id);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<User> queryAll() {
String sql = "SELECT * FROM t_user";
try {
return queryRunner.query(sql, new BeanListHandler<>(User.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
```
7. 在C3P0UserDao类中编写对数据库进行增删改查的方法,包括insert(User user)、update(User user)、delete(int id)、queryAll( )等方法,分别实现插入、更新、删除和查询所有记录的方法。
```java
public class C3P0UserDao {
private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
public void insert(User user) {
String sql = "INSERT INTO t_user(name, password, email) VALUES (?, ?, ?)";
try {
queryRunner.update(sql, user.getName(), user.getPassword(), user.getEmail());
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(User user) {
String sql = "UPDATE t_user SET name=?, password=?, email=? WHERE id=?";
try {
queryRunner.update(sql, user.getName(), user.getPassword(), user.getEmail(), user.getId());
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
String sql = "DELETE FROM t_user WHERE id=?";
try {
queryRunner.update(sql, id);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<User> queryAll() {
String sql = "SELECT * FROM t_user";
try {
return queryRunner.query(sql, new BeanListHandler<>(User.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
```
8. 创建测试类Test2,对上述C3P0UserDao中定义的方法进行测试输出。
```java
public class Test2 {
public static void main(String[] args) {
C3P0UserDao userDao = new C3P0UserDao();
// 测试插入
User user1 = new User();
user1.setName("user1");
user1.setPassword("123456");
user1.setEmail("user1@test.com");
userDao.insert(user1);
// 测试更新
User user2 = new User();
user2.setId(1);
user2.setName("user2");
user2.setPassword("654321");
user2.setEmail("user2@test.com");
userDao.update(user2);
// 测试查询
List<User> userList = userDao.queryAll();
for (User user : userList) {
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getPassword() + "\t" + user.getEmail());
}
// 测试删除
userDao.delete(1);
}
}
```
阅读全文