java连接mysql数据库实现增删改查数据库的学生数据要创建一个学生类和增删改查的接口
时间: 2023-12-06 21:40:03 浏览: 81
基于mysql数据库进行java控制台增删改查学生信息
5星 · 资源好评率100%
好的,下面我给您提供一个简单的示例代码:
首先,我们需要创建一个 `Student` 类,用于存储学生的信息,代码如下:
```java
public class Student {
private int id; // 学生编号
private String name; // 学生姓名
private int age; // 学生年龄
private String gender; // 学生性别
// 构造方法
public Student(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
// getter 和 setter 方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
接着,我们需要创建一个 `StudentDao` 接口,并在其中定义增删改查的方法,代码如下:
```java
public interface StudentDao {
// 添加学生
public boolean add(Student student);
// 删除学生
public boolean delete(int id);
// 修改学生信息
public boolean update(Student student);
// 根据学生编号查询学生信息
public Student findById(int id);
// 查询所有学生信息
public List<Student> findAll();
}
```
然后,我们需要创建一个 `StudentDaoImpl` 类,实现 `StudentDao` 接口,并实现其中的方法,代码如下:
```java
public class StudentDaoImpl implements StudentDao {
// 数据库连接相关信息,根据实际情况进行修改
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/studentdb";
private static final String USER = "root";
private static final String PASS = "root";
// 数据库连接
private Connection conn = null;
private PreparedStatement pstmt = null;
// 构造方法中进行数据库连接
public StudentDaoImpl() {
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加学生
public boolean add(Student student) {
try {
String sql = "insert into student(id, name, age, gender) values (?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getGender());
pstmt.executeUpdate();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
closeConnection();
}
}
// 删除学生
public boolean delete(int id) {
try {
String sql = "delete from student where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
closeConnection();
}
}
// 修改学生信息
public boolean update(Student student) {
try {
String sql = "update student set name=?, age=?, gender=? where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getId());
pstmt.executeUpdate();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
closeConnection();
}
}
// 根据学生编号查询学生信息
public Student findById(int id) {
try {
String sql = "select * from student where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
Student student = new Student(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("gender"));
return student;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
closeConnection();
}
}
// 查询所有学生信息
public List<Student> findAll() {
List<Student> students = new ArrayList<Student>();
try {
String sql = "select * from student";
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("gender"));
students.add(student);
}
return students;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
closeConnection();
}
}
// 关闭数据库连接
private void closeConnection() {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
最后,我们可以在 `main` 方法中进行测试,代码如下:
```java
public class Test {
public static void main(String[] args) {
// 创建一个 StudentDaoImpl 对象
StudentDaoImpl dao = new StudentDaoImpl();
// 添加学生
Student student1 = new Student(1, "张三", 18, "男");
boolean result1 = dao.add(student1);
System.out.println("添加学生结果:" + result1);
// 修改学生信息
Student student2 = new Student(1, "李四", 20, "女");
boolean result2 = dao.update(student2);
System.out.println("修改学生信息结果:" + result2);
// 查询所有学生信息
List<Student> students = dao.findAll();
System.out.println("所有学生信息:");
for (Student student : students) {
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getGender());
}
// 根据学生编号查询学生信息
Student student3 = dao.findById(1);
System.out.println("编号为 1 的学生信息:" + student3);
// 删除学生
boolean result3 = dao.delete(1);
System.out.println("删除学生结果:" + result3);
}
}
```
以上就是一个简单的 Java 连接 MySQL 数据库并实现增删改查的示例代码,您可以根据实际情况进行修改和扩展。
阅读全文