分别使用JDBC和C3P0数据库连接池完成以下数据库操作,具体要求如下: 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( )等方法,分别实现插入、更新、删除和查询所有记录的方法。 创建测试类Test2,对上述C3P0UserDao中定义的方法进行测试输出
时间: 2024-02-01 19:13:39 浏览: 117
由于篇幅较长,我只能提供代码示例,希望对你有所帮助。
使用JDBC实现:
User类:
```java
public class User {
private int id;
private String name;
private String password;
private String email;
public User(int id, String name, String password, String email) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
}
// getter and setter methods
}
```
JDBCUtils工具类:
```java
public class JDBCUtils {
private static final String URL = "jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
UserDao类:
```java
public class UserDao {
public void insert(User user) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "INSERT INTO t_user(name, password, email) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
int rows = pstmt.executeUpdate();
System.out.println("Inserted " + rows + " row(s).");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, pstmt, null);
}
}
public void update(User user) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "UPDATE t_user SET name=?, password=?, email=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
pstmt.setInt(4, user.getId());
int rows = pstmt.executeUpdate();
System.out.println("Updated " + rows + " row(s).");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, pstmt, null);
}
}
public void delete(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCUtils.getConnection();
String sql = "DELETE FROM t_user WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int rows = pstmt.executeUpdate();
System.out.println("Deleted " + rows + " row(s).");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, pstmt, null);
}
}
public List<User> queryAll() {
List<User> userList = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "SELECT id, name, password, email FROM t_user";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");
String email = rs.getString("email");
User user = new User(id, name, password, email);
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, stmt, rs);
}
return userList;
}
}
```
测试类Test1:
```java
public class Test1 {
public static void main(String[] args) {
UserDao userDao = new UserDao();
// insert
User user1 = new User(0, "Alice", "123456", "alice@example.com");
userDao.insert(user1);
User user2 = new User(0, "Bob", "abcdef", null);
userDao.insert(user2);
// update
User user3 = new User(1, "Alice2", "654321", "alice2@example.com");
userDao.update(user3);
// queryAll
List<User> userList = userDao.queryAll();
for (User user : userList) {
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getPassword() + "\t" + user.getEmail());
}
// delete
userDao.delete(2);
}
}
```
使用C3P0实现:
C3P0Utils工具类:
```java
public class C3P0Utils {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static DataSource getDataSource() {
return dataSource;
}
}
```
C3P0UserDao类:
```java
public class C3P0UserDao {
private QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
public void insert(User user) {
String sql = "INSERT INTO t_user(name, password, email) VALUES (?, ?, ?)";
try {
int rows = runner.update(sql, user.getName(), user.getPassword(), user.getEmail());
System.out.println("Inserted " + rows + " row(s).");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(User user) {
String sql = "UPDATE t_user SET name=?, password=?, email=? WHERE id=?";
try {
int rows = runner.update(sql, user.getName(), user.getPassword(), user.getEmail(), user.getId());
System.out.println("Updated " + rows + " row(s).");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
String sql = "DELETE FROM t_user WHERE id=?";
try {
int rows = runner.update(sql, id);
System.out.println("Deleted " + rows + " row(s).");
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<User> queryAll() {
String sql = "SELECT id, name, password, email FROM t_user";
try {
return runner.query(sql, new BeanListHandler<>(User.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
```
测试类Test2:
```java
public class Test2 {
public static void main(String[] args) {
C3P0UserDao userDao = new C3P0UserDao();
// insert
User user1 = new User(0, "Alice", "123456", "alice@example.com");
userDao.insert(user1);
User user2 = new User(0, "Bob", "abcdef", null);
userDao.insert(user2);
// update
User user3 = new User(1, "Alice2", "654321", "alice2@example.com");
userDao.update(user3);
// queryAll
List<User> userList = userDao.queryAll();
for (User user : userList) {
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getPassword() + "\t" + user.getEmail());
}
// delete
userDao.delete(2);
}
}
```
阅读全文