用java和sql sever写一个学生信息管理系统
时间: 2023-06-19 20:08:45 浏览: 128
以下是一个简单的学生信息管理系统的Java代码和SQL Server数据库:
Java代码:
```java
import java.sql.*;
public class StudentManagementSystem {
// Establishing a database connection
private static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=student_db";
String username = "sa";
String password = "password";
con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
// Adding a new student to the database
public static void addStudent(String name, int age, String gender, String address, String phone) {
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("insert into students(name, age, gender, address, phone) values(?,?,?,?,?)");
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, gender);
ps.setString(4, address);
ps.setString(5, phone);
ps.executeUpdate();
System.out.println("Student added successfully!");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Updating an existing student in the database
public static void updateStudent(int id, String name, int age, String gender, String address, String phone) {
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("update students set name=?, age=?, gender=?, address=?, phone=? where id=?");
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, gender);
ps.setString(4, address);
ps.setString(5, phone);
ps.setInt(6, id);
ps.executeUpdate();
System.out.println("Student updated successfully!");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Deleting a student from the database
public static void deleteStudent(int id) {
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("delete from students where id=?");
ps.setInt(1, id);
ps.executeUpdate();
System.out.println("Student deleted successfully!");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Displaying all students in the database
public static void viewStudents() {
try {
Connection con = getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from students");
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id"));
System.out.println("Name: " + rs.getString("name"));
System.out.println("Age: " + rs.getInt("age"));
System.out.println("Gender: " + rs.getString("gender"));
System.out.println("Address: " + rs.getString("address"));
System.out.println("Phone: " + rs.getString("phone"));
System.out.println();
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Main method to test the program
public static void main(String[] args) {
addStudent("John Doe", 20, "Male", "123 Main St", "555-1234");
addStudent("Jane Smith", 21, "Female", "456 Oak Ave", "555-5678");
viewStudents();
updateStudent(1, "Johnny Doe", 21, "Male", "123 Main St", "555-1234");
deleteStudent(2);
viewStudents();
}
}
```
SQL Server数据库:
```sql
create database student_db;
use student_db;
create table students(
id int primary key identity(1,1),
name varchar(50) not null,
age int not null,
gender varchar(10) not null,
address varchar(100) not null,
phone varchar(20) not null
);
```
注意:这只是一个简单的示例程序,实际的学生信息管理系统需要更多的功能和完善的错误处理。同时,本程序的数据库连接信息(url、username、password)应该根据你的实际情况进行修改。
阅读全文