增(id自增) 删(根据id删除) 改(根据id修改) Java做查(根据学号查询、姓名模糊查询、查询全部)
时间: 2023-08-09 11:10:00 浏览: 82
基于java swing开发的桌面程序-学生信息管理系统,实现了增删改查
好的,以下是Java代码实现数据库的增删改查操作:
1. 增加数据(id自增)
```java
public void add(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 获取数据库连接
conn = getConnection();
// SQL语句,id自增长,不需要插入id
String sql = "INSERT INTO students (name, age, gender) VALUES (?, ?, ?)";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
// 设置参数
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
// 执行SQL语句
pstmt.executeUpdate();
// 获取自增长的id
ResultSet rs = pstmt.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
student.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和PreparedStatement对象
close(conn, pstmt, null);
}
}
```
2. 删除数据(根据id删除)
```java
public void delete(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 获取数据库连接
conn = getConnection();
// SQL语句,根据id删除
String sql = "DELETE FROM students WHERE id = ?";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设置参数
pstmt.setInt(1, id);
// 执行SQL语句
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和PreparedStatement对象
close(conn, pstmt, null);
}
}
```
3. 修改数据(根据id修改)
```java
public void update(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 获取数据库连接
conn = getConnection();
// SQL语句,根据id修改
String sql = "UPDATE students SET name = ?, age = ?, gender = ? WHERE id = ?";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设置参数
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getId());
// 执行SQL语句
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和PreparedStatement对象
close(conn, pstmt, null);
}
}
```
4. 查询数据(根据学号查询、姓名模糊查询、查询全部)
```java
public List<Student> query(String sno, String name) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Student> list = new ArrayList<Student>();
try {
// 获取数据库连接
conn = getConnection();
// SQL语句,根据学号查询、姓名模糊查询、查询全部
String sql = "SELECT * FROM students WHERE 1 = 1 ";
if (!StringUtils.isEmpty(sno)) {
sql += "AND sno = '" + sno + "' ";
}
if (!StringUtils.isEmpty(name)) {
sql += "AND name LIKE '%" + name + "%' ";
}
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 执行SQL语句
rs = pstmt.executeQuery();
// 处理结果集
while (rs.next()) {
int id = rs.getInt("id");
String sNo = rs.getString("sno");
String sName = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
Student student = new Student();
student.setId(id);
student.setSno(sNo);
student.setName(sName);
student.setAge(age);
student.setGender(gender);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接、PreparedStatement对象和ResultSet对象
close(conn, pstmt, rs);
}
return list;
}
```
希望这些代码对您有所帮助。
阅读全文