编写一个java普通项目,实现单表(表自己创建,字段不要太多)的列表查询、详情查询、增、删、改 }
时间: 2024-06-12 07:10:13 浏览: 5
好的,这是一个比较基础的Java项目,可以按照以下步骤进行实现:
1. 创建数据库表,例如创建一个名为"student"的表,包含"id"、"name"、"age"三个字段。
2. 创建Java项目,导入数据库驱动包。
3. 编写数据库连接代码,连接到数据库。
4. 编写查询代码,实现列表查询和详情查询功能。
5. 编写增、删、改代码,实现对表中数据的增加、删除和修改操作。
以下是示例代码:
```java
import java.sql.*;
public class StudentDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// 连接数据库
public void getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
}
// 关闭连接
public void closeConnection() throws SQLException {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
}
// 列表查询
public void list() throws SQLException {
String sql = "SELECT * FROM student";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getInt("age"));
}
}
// 详情查询
public void detail(int id) throws SQLException {
String sql = "SELECT * FROM student WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getInt("age"));
}
}
// 增加数据
public void add(String name, int age) throws SQLException {
String sql = "INSERT INTO student(name, age) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.executeUpdate();
}
// 删除数据
public void delete(int id) throws SQLException {
String sql = "DELETE FROM student WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
}
// 修改数据
public void update(int id, String name, int age) throws SQLException {
String sql = "UPDATE student SET name=?, age=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setInt(3, id);
pstmt.executeUpdate();
}
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)