设计一个学生类,创建相应的数据库表,利用JDBC技术访问数据库,实现学生信息的增删查改。
时间: 2024-02-27 18:53:16 浏览: 75
JDBC技术使用标准的 SQL 语言对数据库表中的数据进行访问,以及增删查改
好的,我可以帮你解答这个问题。
首先,我们需要设计一个学生类,包括学号、姓名、性别、年龄等基本属性。代码如下:
```java
public class Student {
private int id;
private String name;
private String gender;
private int age;
public Student(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
// getter and setter methods
// ...
}
```
接下来,我们需要创建相应的数据库表。可以使用以下的 SQL 语句创建一个名为 student 的表:
```sql
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT NOT NULL
);
```
然后,我们可以使用 JDBC 技术访问数据库,并实现学生信息的增删查改。以下是一个示例代码:
```java
import java.sql.*;
public class StudentDAO {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public void add(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "INSERT INTO student (id, name, gender, age) VALUES (?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getAge());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void delete(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "DELETE FROM student WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void update(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "UPDATE student SET name = ?, gender = ?, age = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setInt(4, student.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public Student getById(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "SELECT * FROM student WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
String gender = rs.getString("gender");
int age = rs.getInt("age");
return new Student(id, name, gender, age);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
```
上面的代码中,我们定义了一个 StudentDAO 类,其中包含了增加、删除、更新和查询学生信息的方法。我们可以在程序中实例化该类,并调用其方法来操作数据库表。
注意,这里我们使用了PreparedStatement来预编译 SQL 语句,可以有效地防止 SQL 注入攻击。同时,我们在使用完数据库连接、PreparedStatement和ResultSet对象后,需要手动关闭它们,以释放资源。
阅读全文