学习Java中的JDBC数据库连接方式,安装好MySQL后,完成下面的基于命令行操作的小型的学生信息管理系统: 要求: 1)在MySQL中创建数据库stu, 创建表格Student(id, name, age, address, birthday)。 2)通过JDBC建
时间: 2023-08-08 11:05:47 浏览: 77
学生信息管理系统Demo(GUI:Java-Swing、数据库:Mysql)
立Java程序,连接到MySQL数据库,并实现以下功能:
- 添加学生信息
- 删除学生信息
- 修改学生信息
- 查询学生信息
- 显示所有学生信息
以下是一个基本的示例代码,你可以根据自己的需要进行修改和完善:
```java
import java.sql.*;
import java.util.Scanner;
public class StudentManagementSystem {
private static final String DB_URL = "jdbc:mysql://localhost:3306/stu";
private static final String USER = "root";
private static final String PASSWORD = "123456";
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
System.out.println("数据库连接成功!");
while (true) {
System.out.println("请输入您要进行的操作:");
System.out.println("1.添加学生信息");
System.out.println("2.删除学生信息");
System.out.println("3.修改学生信息");
System.out.println("4.查询学生信息");
System.out.println("5.显示所有学生信息");
System.out.println("0.退出系统");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(conn);
break;
case 2:
deleteStudent(conn);
break;
case 3:
updateStudent(conn);
break;
case 4:
queryStudent(conn);
break;
case 5:
showAllStudents(conn);
break;
case 0:
System.out.println("谢谢使用,再见!");
System.exit(0);
default:
System.out.println("无效的选项,请重新输入!");
break;
}
}
} catch (SQLException e) {
System.out.println("数据库连接失败: " + e.getMessage());
}
}
private static void addStudent(Connection conn) throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生信息(id, name, age, address, birthday):");
int id = scanner.nextInt();
String name = scanner.next();
int age = scanner.nextInt();
String address = scanner.next();
String birthday = scanner.next();
String sql = "INSERT INTO Student(id, name, age, address, birthday) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setInt(3, age);
pstmt.setString(4, address);
pstmt.setString(5, birthday);
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("添加学生信息成功!");
} else {
System.out.println("添加学生信息失败!");
}
}
private static void deleteStudent(Connection conn) throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生id:");
int id = scanner.nextInt();
String sql = "DELETE FROM Student WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("删除学生信息成功!");
} else {
System.out.println("删除学生信息失败!");
}
}
private static void updateStudent(Connection conn) throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生id:");
int id = scanner.nextInt();
System.out.println("请输入修改后的学生信息(name, age, address, birthday):");
String name = scanner.next();
int age = scanner.nextInt();
String address = scanner.next();
String birthday = scanner.next();
String sql = "UPDATE Student SET name=?, age=?, address=?, birthday=? WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, address);
pstmt.setString(4, birthday);
pstmt.setInt(5, id);
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("修改学生信息成功!");
} else {
System.out.println("修改学生信息失败!");
}
}
private static void queryStudent(Connection conn) throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的学生id:");
int id = scanner.nextInt();
String sql = "SELECT * FROM Student WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println(String.format("%d\t%s\t%d\t%s\t%s",
rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("address"), rs.getString("birthday")));
} else {
System.out.println("没有找到该学生的信息!");
}
}
private static void showAllStudents(Connection conn) throws SQLException {
String sql = "SELECT * FROM Student";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("id\tname\tage\taddress\t\tbirthday");
while (rs.next()) {
System.out.println(String.format("%d\t%s\t%d\t%s\t%s",
rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("address"), rs.getString("birthday")));
}
}
}
```
在运行程序之前,需要先在MySQL中创建数据库和表格。可以使用以下命令行操作:
```sql
CREATE DATABASE stu;
USE stu;
CREATE TABLE Student (
id INT PRIMARY KEY,
name VARCHAR(20),
age INT,
address VARCHAR(100),
birthday VARCHAR(20)
);
```
这样就可以使用上面的Java程序来进行学生信息管理了。
阅读全文