java连接mysql数据库增删改查学生信息要创建学生类定义一个方法接口
时间: 2023-07-29 10:10:25 浏览: 60
首先需要创建一个学生类,包含学生的各种属性,如姓名、年龄、学号等等。然后我们可以创建一个接口,用于定义增删改查的方法。
以下是一个简单的示例代码:
```java
import java.sql.*;
// 学生类
class Student {
private String name;
private int age;
private int id;
public Student(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getId() {
return id;
}
}
// 接口
interface StudentDao {
// 增
boolean add(Student student);
// 删
boolean delete(int id);
// 改
boolean update(Student student);
// 查
Student find(int id);
}
// 实现接口
class StudentDaoImpl implements StudentDao {
private Connection conn;
public StudentDaoImpl(Connection conn) {
this.conn = conn;
}
@Override
public boolean add(Student student) {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO student(name,age,id) VALUES (?,?,?)");
ps.setString(1, student.getName());
ps.setInt(2, student.getAge());
ps.setInt(3, student.getId());
ps.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean delete(int id) {
try {
PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id=?");
ps.setInt(1, id);
ps.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean update(Student student) {
try {
PreparedStatement ps = conn.prepareStatement("UPDATE student SET name=?,age=? WHERE id=?");
ps.setString(1, student.getName());
ps.setInt(2, student.getAge());
ps.setInt(3, student.getId());
ps.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@Override
public Student find(int id) {
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM student WHERE id=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
int studentId = rs.getInt("id");
return new Student(name, age, studentId);
} else {
return null;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
// 测试类
public class Test {
public static void main(String[] args) {
// 连接数据库
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("连接数据库成功!");
// 创建学生Dao对象
StudentDao dao = new StudentDaoImpl(conn);
// 增加学生
Student student1 = new Student("小明", 18, 1);
dao.add(student1);
System.out.println("添加学生成功!");
// 修改学生
Student student2 = new Student("小红", 17, 1);
dao.update(student2);
System.out.println("修改学生成功!");
// 查找学生
Student student3 = dao.find(1);
System.out.println("查找到的学生信息:姓名=" + student3.getName() +
", 年龄=" + student3.getAge() + ", 学号=" + student3.getId());
// 删除学生
dao.delete(1);
System.out.println("删除学生成功!");
// 关闭连接
conn.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
```
在实现接口的类中,我们使用了 PreparedStatement 对象来执行 SQL 语句,这样可以避免 SQL 注入的风险。同时,在实现增删改查的方法时,我们需要处理 SQL 执行过程中可能出现的异常。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)