当前位置:   article > 正文

Java连接数据库(JDBC)实现学生信息管理系统(增删改查)_java写数据库管理系统

java写数据库管理系统

首先从MySQL官网上下载mysql-connector-j-8.0.33.jar包,链接:https://dev.mysql.com/downloads/connector/j/,jar包版本必须和你本身的mysql版本相匹配!

下载图片中我标记的那个,下载压缩包到随便文件夹中,自己能找到即可,然后解压到文件夹中

 打开该文件夹

 打开IDEA ,创建一个lib目录,将jar文件复制粘贴lib中

将lib目录设置为library

 这样数据库就配置好了!

下面开始用代码连接数据库

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. public class databaseConnection {
  5. public static void conn(){
  6. Connection connection;
  7. String url = "jdbc:mysql://localhost:3306/bookstore"; //数据库连接的url地址
  8. String user = "root"; //数据库用户名
  9. String password = "123456"; //数据库密码
  10. try {
  11. //加载数据库驱动
  12. Class.forName("com.mysql.cj.jdbc.Driver");
  13. System.out.println("数据库驱动加载成功!");
  14. }catch (ClassNotFoundException e) {
  15. e.printStackTrace();
  16. }
  17. try{
  18. //通过DriverManager获取数据库连接
  19. connection = DriverManager.getConnection(url,user,password);
  20. System.out.println("数据库连接成功!");
  21. }catch (SQLException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. public static void main(String[] args) {
  26. databaseConnection.conn(); //静态方法:直接使用类名.方法名调用
  27. }
  28. }

上面代码中,url为连接数据库时的地址,loaclhost为主机名,3306为端口号,bookstore为数据库名,user为创建数据库的用户名,password为密码!加载数据库驱动和连接数据库是编译会报错,需要try catch把异常环绕。

 这样,数据库就连接成功了!

下面开始书写学生信息管理系统

先创建数据库,然后建表

首先创建student表

  1. CREATE TABLE `student` (
  2. `学号` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  3. `姓名` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  4. `性别` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  5. `出生日期` date DEFAULT NULL,
  6. `地区` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  7. `民族` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '汉',
  8. `班级编号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  9. PRIMARY KEY (`学号`) USING BTREE
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;

插入相应数据

  1. INSERT INTO `student` VALUES ('2013110101', '张晓勇', '男', '1997-12-11', '山西', '汉', 'AC1301');
  2. INSERT INTO `student` VALUES ('2013110103', '王一敏', '女', '1998-01-01', '河北', '汉', 'AC1301');
  3. INSERT INTO `student` VALUES ('2013110201', '江山', '女', '1996-09-17', '内蒙古', '锡伯', 'AC1302');
  4. INSERT INTO `student` VALUES ('2013110202', '李明', '男', '1996-01-14', '广西', '壮', 'AC1302');
  5. INSERT INTO `student` VALUES ('2013310101', '黄菊', '女', '1995-09-30', '北京', '汉', 'IS1301');
  6. INSERT INTO `student` VALUES ('2013310103', '吴昊', '男', '1995-11-18', '河北', '汉', 'IS1301');
  7. INSERT INTO `student` VALUES ('2014210101', '刘涛', '男', '1997-04-03', '湖南', '侗', 'CS1401');
  8. INSERT INTO `student` VALUES ('2014210102', '郭志坚', '男', '1997-02-21', '上海', '汉', 'CS1401');
  9. INSERT INTO `student` VALUES ('2014310101', '王林', '男', '1996-10-09', '河南', '汉', 'IS1401');
  10. INSERT INTO `student` VALUES ('2014310102', '李怡然', '女', '1996-12-31', '辽宁', '汉', 'IS1401');

然后是score

  1. CREATE TABLE `score` (
  2. `学号` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  3. `课程号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  4. `成绩` float(5,2) DEFAULT NULL,
  5. PRIMARY KEY (`学号`,`课程号`) USING BTREE
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;

插入数据

  1. INSERT INTO `score` VALUES ('2013110101', '11003', '90.00');
  2. INSERT INTO `score` VALUES ('2013110101', '21001', '86.00');
  3. INSERT INTO `score` VALUES ('2013110103', '11003', '89.00');
  4. INSERT INTO `score` VALUES ('2013110103', '21001', '86.00');
  5. INSERT INTO `score` VALUES ('2013110201', '11003', '78.00');
  6. INSERT INTO `score` VALUES ('2013110201', '21001', '92.00');
  7. INSERT INTO `score` VALUES ('2013110202', '11003', '82.00');
  8. INSERT INTO `score` VALUES ('2013110202', '21001', '85.00');
  9. INSERT INTO `score` VALUES ('2013310101', '21004', '83.00');
  10. INSERT INTO `score` VALUES ('2013310101', '31002', '68.00');
  11. INSERT INTO `score` VALUES ('2013310103', '21004', '80.00');
  12. INSERT INTO `score` VALUES ('2013310103', '31002', '76.00');
  13. INSERT INTO `score` VALUES ('2014210101', '21002', '93.00');
  14. INSERT INTO `score` VALUES ('2014210101', '21004', '89.00');
  15. INSERT INTO `score` VALUES ('2014210102', '21002', '95.00');
  16. INSERT INTO `score` VALUES ('2014210102', '21004', '88.00');
  17. INSERT INTO `score` VALUES ('2014310101', '21001', '79.00');
  18. INSERT INTO `score` VALUES ('2014310101', '21004', '80.00');
  19. INSERT INTO `score` VALUES ('2014310102', '21001', '91.00');
  20. INSERT INTO `score` VALUES ('2014310102', '21004', '87.00');

再是course表

  1. CREATE TABLE `course` (
  2. `课程号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  3. `课程名` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  4. `学分` int NOT NULL,
  5. `学时` int NOT NULL,
  6. `学期` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  7. `前置课` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  8. PRIMARY KEY (`课程号`) USING BTREE
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;

插入数据

  1. INSERT INTO `course` VALUES ('11003', '管理学', '2', '32', '2', null);
  2. INSERT INTO `course` VALUES ('11005', '会计学', '3', '48', '2', null);
  3. INSERT INTO `course` VALUES ('21001', '计算机基础', '3', '48', '1', null);
  4. INSERT INTO `course` VALUES ('21002', 'OFFICE高级应用', '3', '48', '2', '21001');
  5. INSERT INTO `course` VALUES ('21004', '程序设计', '4', '64', '2', '21001');
  6. INSERT INTO `course` VALUES ('21005', '数据库', '4', '64', '4', '21004');
  7. INSERT INTO `course` VALUES ('21006', '操作系统', '4', '64', '5', '21001');
  8. INSERT INTO `course` VALUES ('31001', '管理信息系统', '3', '48', '3', '21004');
  9. INSERT INTO `course` VALUES ('31002', '信息系统_分析与设计', '2', '32', '4', '31001');
  10. INSERT INTO `course` VALUES ('31005', '项目管理', '3', '48', '5', '31001');

最后是class

  1. CREATE TABLE `class` (
  2. `班级编号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  3. `班级名称` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  4. `院系` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  5. `年级` int DEFAULT NULL,
  6. `人数` int DEFAULT NULL,
  7. PRIMARY KEY (`班级编号`) USING BTREE
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;

插入数据

  1. INSERT INTO `class` VALUES ('AC1301', '会计13-1班', '会计学院', '2013', '35');
  2. INSERT INTO `class` VALUES ('AC1302', '会计13-2班', '会计学院', '2013', '35');
  3. INSERT INTO `class` VALUES ('CS1401', '计算机14-1班', '计算机学院', '2014', '35');
  4. INSERT INTO `class` VALUES ('IS1301', '信息系统13-1班', '信息学院', '2013', null);
  5. INSERT INTO `class` VALUES ('IS1401', '信息系统14-1班', '信息学院', null, '30');

建完表后

开始写Java代码

类比较多,一个一个来

先写主类StudentInformationManageSystems

  1. package StudentInformationManageSystem;
  2. import java.util.Scanner;
  3. public class StudentInformationManageSystems {
  4. public static void main(String[] args) {
  5. System.out.println("-------------欢迎来到学生信息管理系统---------");
  6. StudentMain.studentmains();
  7. while (true){
  8. Scanner input = new Scanner(System.in);
  9. int scan = input.nextInt();
  10. switch (scan){
  11. case 1:
  12. StudentAdd.add();
  13. System.out.println("-----------------------");
  14. StudentMain.studentmains();
  15. break;
  16. case 2:
  17. StudentQuery.query();
  18. System.out.println("-----------------------");
  19. StudentMain.studentmains();
  20. break;
  21. case 3:
  22. StudentUpdate.update();
  23. System.out.println("-----------------------");
  24. StudentMain.studentmains();
  25. break;
  26. case 4:
  27. StudentDelete.delete();
  28. System.out.println("-----------------------");
  29. StudentMain.studentmains();
  30. break;
  31. case 5:
  32. StudentClass.stuclass();
  33. System.out.println("-----------------------");
  34. StudentMain.studentmains();
  35. break;
  36. case 6:
  37. StudentCourse.course();
  38. System.out.println("-----------------------");
  39. StudentMain.studentmains();
  40. break;
  41. case 7:
  42. StudentScore.score();
  43. System.out.println("-----------------------");
  44. StudentMain.studentmains();
  45. break;
  46. case 8:
  47. System.exit(0);
  48. default:
  49. System.out.println("无效的输入!");
  50. System.exit(0);
  51. }
  52. }
  53. }
  54. }

按顺序来,再写studentmains

  1. package StudentInformationManageSystem;
  2. public class StudentMain {
  3. public static void studentmains(){
  4. System.out.println("1.增加学生信息");
  5. System.out.println("2.查询学生信息");
  6. System.out.println("3.修改学生信息");
  7. System.out.println("4.删除学生信息");
  8. System.out.println("5.查询学生所在班级");
  9. System.out.println("6.查询学生所选课程");
  10. System.out.println("7.查询学生所课程成绩");
  11. System.out.println("8.退出");
  12. System.out.println("-------------------");
  13. System.out.println("请输入你的选择:");
  14. }
  15. }

然后写StudentAdd

  1. package StudentInformationManageSystem;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7. public class StudentAdd {
  8. public static void add() {
  9. Connection connection;
  10. try {
  11. Class.forName("com.mysql.cj.jdbc.Driver");
  12. String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
  13. String name = "root";
  14. String password = "123456";
  15. connection = DriverManager.getConnection(url, name, password);
  16. Statement stmt = connection.createStatement();
  17. Scanner input = new Scanner(System.in);
  18. System.out.println("请输入学号:");
  19. int id = input.nextInt();
  20. System.out.println("请输入姓名:");
  21. String studnetname = input.next();
  22. System.out.println("请输入性别:");
  23. String gender = input.next();
  24. System.out.println("请输入出生日期:");
  25. String brithday = input.next();
  26. System.out.println("请输入地区:");
  27. String area = input.next();
  28. System.out.println("请输入民族:");
  29. String nation = input.next();
  30. System.out.println("请输入班级编号:");
  31. String classId = input.next();
  32. String sql = "insert into bookstore.student(学号, 姓名, 性别, 出生日期, 地区, 民族, 班级编号) " +
  33. "values(" + id + ",'" + studnetname + "','" + gender + "','" + brithday + "','" + area + "','" + nation + "','" + classId + "') ";
  34. stmt.executeUpdate(sql);
  35. System.out.println("学生添加成功!");
  36. stmt.close();
  37. connection.close();
  38. } catch (SQLException | ClassNotFoundException exception) {
  39. throw new RuntimeException(exception);
  40. }
  41. }
  42. }

然后是StudentQuery

  1. package StudentInformationManageSystem;
  2. import java.sql.*;
  3. public class StudentQuery {
  4. public static void query(){
  5. Connection connection;
  6. try {
  7. //加载驱动
  8. Class.forName("com.mysql.cj.jdbc.Driver");
  9. //获取连接
  10. String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
  11. String name = "root";
  12. String password = "123456";
  13. connection = DriverManager.getConnection(url,name,password);
  14. Statement stmt = connection.createStatement();
  15. String sql = "select * from student";
  16. ResultSet resultSet = stmt.executeQuery(sql);
  17. while(resultSet.next()){
  18. int id = resultSet.getInt("学号");
  19. String Studentname = resultSet.getString("姓名");
  20. String gender = resultSet.getString("性别");
  21. String brithday = resultSet.getString("出生日期");
  22. String area = resultSet.getString("地区");
  23. String nation = resultSet.getString("民族");
  24. String classId = resultSet.getString("班级编号");
  25. System.out.println("学号:"+id+" 姓名:"+Studentname+" 性别:"+gender+" 出生日期:"
  26. +brithday+" 地区:"+area+" 民族:"+nation+" 班级编号:"+classId);
  27. }
  28. stmt.close();
  29. connection.close();
  30. } catch (SQLException | ClassNotFoundException ex) {
  31. throw new RuntimeException(ex);
  32. }
  33. }
  34. }

再是StudentUpdate

  1. package StudentInformationManageSystem;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7. public class StudentUpdate {
  8. public static void update(){
  9. Connection connection;
  10. try {
  11. Class.forName("com.mysql.cj.jdbc.Driver");
  12. String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
  13. String name = "root";
  14. String password = "123456";
  15. connection = DriverManager.getConnection(url, name, password);
  16. Statement stmt = connection.createStatement();
  17. Scanner input = new Scanner(System.in);
  18. System.out.println("请输入要修改学生的学号:");
  19. int id = input.nextInt();
  20. System.out.println("请输入要修改的列:");
  21. String colum = input.next();
  22. System.out.println("请输入要修改的值:");
  23. String value = input.next();
  24. String sql = "UPDATE bookstore.student SET " + colum + " = '" + value + "' WHERE 学号 = " + id;
  25. stmt.executeUpdate(sql);
  26. System.out.println("学生修改成功!");
  27. stmt.close();
  28. connection.close();
  29. }catch (SQLException | ClassNotFoundException e) {
  30. throw new RuntimeException(e);
  31. }
  32. }
  33. }

然后是StudentDelete

  1. package StudentInformationManageSystem;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7. public class StudentDelete {
  8. public static void delete(){
  9. Connection connection;
  10. try {
  11. String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
  12. String name = "root";
  13. String password = "123456";
  14. connection = DriverManager.getConnection(url, name, password);
  15. Statement stmt = connection.createStatement();
  16. Scanner input = new Scanner(System.in);
  17. System.out.println("请输入要删除的学号:");
  18. int id = input.nextInt();
  19. String sql = "delete from bookstore.student where 学号 = " + id;
  20. stmt.executeUpdate(sql);
  21. System.out.println("学生删除成功!");
  22. stmt.close();
  23. connection.close();
  24. } catch (SQLException exception) {
  25. throw new RuntimeException(exception);
  26. }
  27. }
  28. }

再是StudentClass

  1. package StudentInformationManageSystem;
  2. import java.sql.*;
  3. import java.util.Scanner;
  4. public class StudentClass {
  5. public static void stuclass() {
  6. Connection connection;
  7. try {
  8. Class.forName("com.mysql.cj.jdbc.Driver");
  9. String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
  10. String name = "root";
  11. String password = "123456";
  12. connection = DriverManager.getConnection(url, name, password);
  13. Statement stmt = connection.createStatement();
  14. Scanner input = new Scanner(System.in);
  15. System.out.println("请输入学生姓名:");
  16. String stuname = input.next();
  17. String sql = "select * from bookstore.class where 班级编号 = (select 班级编号 from bookstore.student where 姓名 = '"+stuname+"')";
  18. ResultSet resultSet = stmt.executeQuery(sql);
  19. System.out.println("班级编号 | 班级名称 | 院系 | 年级 | 人数 |");
  20. while (resultSet.next()){
  21. String classid = resultSet.getString("班级编号");
  22. String classname = resultSet.getString("班级名称");
  23. String college= resultSet.getString("院系");
  24. String grade = resultSet.getString("年级");
  25. String popluations = resultSet.getString("人数");
  26. System.out.println(classid+" | "+classname+" |" +
  27. " "+college+" | "+grade+" | "+popluations+" | ");
  28. }
  29. stmt.close();
  30. connection.close();
  31. } catch (SQLException | ClassNotFoundException exception) {
  32. throw new RuntimeException(exception);
  33. }
  34. }
  35. }

然后是StudentCourse

  1. package StudentInformationManageSystem;
  2. import java.sql.*;
  3. import java.util.Scanner;
  4. public class StudentCourse {
  5. public static void course() {
  6. Connection connection;
  7. try {
  8. Class.forName("com.mysql.cj.jdbc.Driver");
  9. String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
  10. String name = "root";
  11. String password = "123456";
  12. connection = DriverManager.getConnection(url, name, password);
  13. Statement stmt = connection.createStatement();
  14. Scanner input = new Scanner(System.in);
  15. System.out.println("请输入学生姓名:");
  16. String stuname = input.next();
  17. String sql = "select * from bookstore.course where 课程号 =" +
  18. " any (select 课程号 from bookstore.score where 学号 = any (select 学号 from bookstore.student where 姓名 = '"+stuname+"'))";
  19. ResultSet resultSet = stmt.executeQuery(sql);
  20. System.out.println("课程号 | 课程名 | 学分 | 学时 | 学期 |");
  21. while (resultSet.next()){
  22. String coureid = resultSet.getString("课程号");
  23. String courename = resultSet.getString("课程名");
  24. String score = resultSet.getString("学分");
  25. String time = resultSet.getString("学时");
  26. String term = resultSet.getString("学期");
  27. System.out.println(coureid+" | "+courename+" | "+score+" | "+time+" | "+term+" | ");
  28. }
  29. stmt.close();
  30. connection.close();
  31. } catch (SQLException | ClassNotFoundException exception) {
  32. throw new RuntimeException(exception);
  33. }
  34. }
  35. }

再是StudentScore

  1. package StudentInformationManageSystem;
  2. import java.sql.*;
  3. import java.util.Scanner;
  4. public class StudentScore {
  5. public static void score() {
  6. Connection connection;
  7. try {
  8. Class.forName("com.mysql.cj.jdbc.Driver");
  9. String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
  10. String name = "root";
  11. String password = "123456";
  12. connection = DriverManager.getConnection(url, name, password);
  13. Statement stmt = connection.createStatement();
  14. Scanner input = new Scanner(System.in);
  15. System.out.println("请输入学生姓名:");
  16. String stuname = input.next();
  17. System.out.println("请输入课程名:");
  18. String coursename = input.next();
  19. String sql = "select 成绩 from score where 学号 = any (select 学号 from student where 姓名 = '"+stuname+"')and" +
  20. " 课程号 = any (select 课程号 from course where 课程名 = '"+coursename+"')";
  21. ResultSet resultSet = stmt.executeQuery(sql);
  22. while (resultSet.next()){
  23. int score = resultSet.getInt("成绩");
  24. System.out.println("----------------");
  25. System.out.println("成绩:"+score);
  26. System.out.println("----------------");
  27. }
  28. } catch (SQLException | ClassNotFoundException exception) {
  29. throw new RuntimeException(exception);
  30. }
  31. }
  32. }

这样整个代码就完成了,最后呈上运行结果

 

 截图太大了,这样我就不一个一个呈现了,大家做出来以后自行操作吧,有什么问题可以在评论区询问,我看到都会一一回复,最后祝大家敲代码愉快!

ps:文章有什么错误可以帮我纠正,谢谢大家!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号