javajdbc实现学生管理系统增删改查
时间: 2023-07-08 21:16:04 浏览: 147
Java JDBC是Java语言中用于连接和操作关系型数据库的API,可以通过它实现学生管理系统的增删改查功能。以下是一个简单的示例代码,演示如何使用Java JDBC实现学生管理系统的增删改查功能:
1.导入JDBC的jar包,连接数据库
```
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
```
2.实现查询功能
```
public void query() {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
String sql = "select * from student";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("id=" + rs.getInt("id") + ", name=" + rs.getString("name") + ", age=" + rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3.实现添加功能
```
public void add(Student student) {
PreparedStatement pstmt = null;
try {
String sql = "insert into student(name, age) values(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
4.实现删除功能
```
public void delete(int id) {
PreparedStatement pstmt = null;
try {
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();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5.实现修改功能
```
public void update(Student student) {
PreparedStatement pstmt = null;
try {
String sql = "update student set name=?, age=? where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setInt(3, student.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
完整代码如下:
```
import java.sql.*;
public class StudentManager {
private Connection conn;
public StudentManager() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void query() {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
String sql = "select * from student";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("id=" + rs.getInt("id") + ", name=" + rs.getString("name") + ", age=" + rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void add(Student student) {
PreparedStatement pstmt = null;
try {
String sql = "insert into student(name, age) values(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void delete(int id) {
PreparedStatement pstmt = null;
try {
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();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void update(Student student) {
PreparedStatement pstmt = null;
try {
String sql = "update student set name=?, age=? where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setInt(3, student.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
StudentManager manager = new StudentManager();
manager.query();
manager.add(new Student("Tom", 18));
manager.query();
manager.update(new Student(1, "Jack", 20));
manager.query();
manager.delete(2);
manager.query();
}
}
class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
```
在本示例中,我们创建了一个StudentManager类,用于连接数据库和实现学生管理系统的增删改查功能。在main方法中,我们先查询了数据库中的学生信息,然后添加了一个新的学生,更新了一个学生的信息,删除了一个学生,最后再次查询了数据库中的学生信息,从而演示了增删改查功能的实现。
阅读全文