java编写学生管理演示系统的程序初稿,要连接mysql数据库,并且包括学生id、姓名、性别、年龄、电话号码、家庭住址等。代码中要实现学生信息添加、删除、修改、查询等功能。代码中实现在键盘上输入1、2、3等数字,实现学生信息的增加、删除、修改、查询等操作。并将代码整合到一起。
时间: 2023-10-20 18:22:40 浏览: 109
基于java+控件台+mysql的学生信息管理系统(含演示视频)
5星 · 资源好评率100%
以下是一个简单的Java代码,可以连接MySQL数据库并实现学生信息的添加、删除、修改和查询功能。请注意,这只是一个初稿,可能需要根据具体需求进行更改。
```java
import java.sql.*;
public class StudentManagementSystem {
public static void main(String[] args) {
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 创建表
Statement stmt = conn.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS students (id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, gender VARCHAR(10) NOT NULL, age INT(11) NOT NULL, phone VARCHAR(20) NOT NULL, address VARCHAR(100) NOT NULL, PRIMARY KEY (id))";
stmt.executeUpdate(sql);
// 读取用户输入
System.out.println("请选择操作:1.添加学生信息 2.删除学生信息 3.修改学生信息 4.查询学生信息");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
// 添加学生信息
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生性别:");
String gender = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生电话号码:");
String phone = scanner.next();
System.out.println("请输入学生家庭住址:");
String address = scanner.next();
sql = "INSERT INTO students (name, gender, age, phone, address) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, gender);
pstmt.setInt(3, age);
pstmt.setString(4, phone);
pstmt.setString(5, address);
pstmt.executeUpdate();
System.out.println("添加成功!");
break;
case 2:
// 删除学生信息
System.out.println("请输入要删除的学生id:");
int id = scanner.nextInt();
sql = "DELETE FROM students WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
System.out.println("删除成功!");
break;
case 3:
// 修改学生信息
System.out.println("请输入要修改的学生id:");
id = scanner.nextInt();
System.out.println("请输入要修改的学生姓名:");
name = scanner.next();
System.out.println("请输入要修改的学生性别:");
gender = scanner.next();
System.out.println("请输入要修改的学生年龄:");
age = scanner.nextInt();
System.out.println("请输入要修改的学生电话号码:");
phone = scanner.next();
System.out.println("请输入要修改的学生家庭住址:");
address = scanner.next();
sql = "UPDATE students SET name = ?, gender = ?, age = ?, phone = ?, address = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, gender);
pstmt.setInt(3, age);
pstmt.setString(4, phone);
pstmt.setString(5, address);
pstmt.setInt(6, id);
pstmt.executeUpdate();
System.out.println("修改成功!");
break;
case 4:
// 查询学生信息
System.out.println("请输入要查询的学生id:");
id = scanner.nextInt();
sql = "SELECT * FROM students WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("学生id:" + rs.getInt("id"));
System.out.println("学生姓名:" + rs.getString("name"));
System.out.println("学生性别:" + rs.getString("gender"));
System.out.println("学生年龄:" + rs.getInt("age"));
System.out.println("学生电话号码:" + rs.getString("phone"));
System.out.println("学生家庭住址:" + rs.getString("address"));
}
break;
default:
System.out.println("输入错误!");
break;
}
// 关闭连接
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
请注意,这只是一个简单的例子,实际应用中可能需要更多的错误处理和安全措施。例如,应该对用户输入进行验证,以避免SQL注入攻击。同时,应该使用try-with-resources块或手动关闭连接,以避免资源泄漏问题。
阅读全文