按以下要求设计程序,完成对数据表users的查询操作,已知表记录如下。 mysql> select * from users; +----+--------+----------+-----------------+------------+ | id | name | password | email | birthday | +----+--------+----------+-----------------+------------+ | 1 | zhangs | 123456 | zs@sina.com | 1980-12-04 | | 2 | lisi | 123456 | lisi@sina.com | 1981-12-04 | | 3 | wangwu | 123456 | wangwu@sina.com | 1979-12-04 | +----+--------+----------+-----------------+------------+ (1)使用DBCP数据库连接池的BasicDataSource类直接创建数据源对象,类名为DBCPUtils。 (2)创建QueryState类,完成查询操作id=1,并返回对象。
时间: 2023-08-15 15:48:12 浏览: 123
以下是使用BasicDataSource类创建数据源对象和查询数据的Java代码示例:
```java
import java.sql.*;
import org.apache.commons.dbcp2.BasicDataSource;
public class DBCPUtils {
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
private static BasicDataSource dataSource = null;
static {
dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(URL);
dataSource.setUsername(USERNAME);
dataSource.setPassword(PASSWORD);
dataSource.setInitialSize(5);
dataSource.setMaxTotal(10);
}
public static QueryState queryUserById(int userId) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
QueryState state = null;
try {
conn = dataSource.getConnection();
String sql = "SELECT * FROM users WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, userId);
rs = stmt.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");
String email = rs.getString("email");
Date birthday = rs.getDate("birthday");
state = new QueryState(id, name, password, email, birthday);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return state;
}
public static void main(String[] args) {
QueryState state = queryUserById(1);
if (state != null) {
System.out.println(state.toString());
} else {
System.out.println("No user found.");
}
}
}
class QueryState {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
public QueryState(int id, String name, String password, String email, Date birthday) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.birthday = birthday;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public Date getBirthday() {
return birthday;
}
public String toString() {
return "id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", birthday=" + birthday.toString();
}
}
```
该示例中,我们使用了Apache Commons DBCP数据库连接池库的BasicDataSource类创建了一个数据源对象,然后在queryUserById方法中使用该数据源对象获取数据库连接,执行SQL查询语句,并返回查询结果封装成的QueryState对象。最后在main方法中调用该方法进行查询,并输出查询结果。
需要注意的是,我们在代码中硬编码了数据库连接信息,实际应用中应该将其配置在properties文件中等更安全的方式进行管理。
阅读全文