赞
踩
首先从MySQL官网上下载mysql-connector-j-8.0.33.jar包,链接:https://dev.mysql.com/downloads/connector/j/,jar包版本必须和你本身的mysql版本相匹配!
下载图片中我标记的那个,下载压缩包到随便文件夹中,自己能找到即可,然后解压到文件夹中
打开该文件夹
打开IDEA ,创建一个lib目录,将jar文件复制粘贴lib中
将lib目录设置为library
这样数据库就配置好了!
下面开始用代码连接数据库
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
-
-
- public class databaseConnection {
- public static void conn(){
- Connection connection;
- String url = "jdbc:mysql://localhost:3306/bookstore"; //数据库连接的url地址
- String user = "root"; //数据库用户名
- String password = "123456"; //数据库密码
- try {
- //加载数据库驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
- System.out.println("数据库驱动加载成功!");
- }catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try{
- //通过DriverManager获取数据库连接
- connection = DriverManager.getConnection(url,user,password);
- System.out.println("数据库连接成功!");
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- databaseConnection.conn(); //静态方法:直接使用类名.方法名调用
- }
- }
上面代码中,url为连接数据库时的地址,loaclhost为主机名,3306为端口号,bookstore为数据库名,user为创建数据库的用户名,password为密码!加载数据库驱动和连接数据库是编译会报错,需要try catch把异常环绕。
这样,数据库就连接成功了!
下面开始书写学生信息管理系统
先创建数据库,然后建表
首先创建student表
- CREATE TABLE `student` (
- `学号` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `姓名` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `性别` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `出生日期` date DEFAULT NULL,
- `地区` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
- `民族` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '汉',
- `班级编号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
- PRIMARY KEY (`学号`) USING BTREE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
插入相应数据
- INSERT INTO `student` VALUES ('2013110101', '张晓勇', '男', '1997-12-11', '山西', '汉', 'AC1301');
- INSERT INTO `student` VALUES ('2013110103', '王一敏', '女', '1998-01-01', '河北', '汉', 'AC1301');
- INSERT INTO `student` VALUES ('2013110201', '江山', '女', '1996-09-17', '内蒙古', '锡伯', 'AC1302');
- INSERT INTO `student` VALUES ('2013110202', '李明', '男', '1996-01-14', '广西', '壮', 'AC1302');
- INSERT INTO `student` VALUES ('2013310101', '黄菊', '女', '1995-09-30', '北京', '汉', 'IS1301');
- INSERT INTO `student` VALUES ('2013310103', '吴昊', '男', '1995-11-18', '河北', '汉', 'IS1301');
- INSERT INTO `student` VALUES ('2014210101', '刘涛', '男', '1997-04-03', '湖南', '侗', 'CS1401');
- INSERT INTO `student` VALUES ('2014210102', '郭志坚', '男', '1997-02-21', '上海', '汉', 'CS1401');
- INSERT INTO `student` VALUES ('2014310101', '王林', '男', '1996-10-09', '河南', '汉', 'IS1401');
- INSERT INTO `student` VALUES ('2014310102', '李怡然', '女', '1996-12-31', '辽宁', '汉', 'IS1401');
然后是score
- CREATE TABLE `score` (
- `学号` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `课程号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `成绩` float(5,2) DEFAULT NULL,
- PRIMARY KEY (`学号`,`课程号`) USING BTREE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
插入数据
- INSERT INTO `score` VALUES ('2013110101', '11003', '90.00');
- INSERT INTO `score` VALUES ('2013110101', '21001', '86.00');
- INSERT INTO `score` VALUES ('2013110103', '11003', '89.00');
- INSERT INTO `score` VALUES ('2013110103', '21001', '86.00');
- INSERT INTO `score` VALUES ('2013110201', '11003', '78.00');
- INSERT INTO `score` VALUES ('2013110201', '21001', '92.00');
- INSERT INTO `score` VALUES ('2013110202', '11003', '82.00');
- INSERT INTO `score` VALUES ('2013110202', '21001', '85.00');
- INSERT INTO `score` VALUES ('2013310101', '21004', '83.00');
- INSERT INTO `score` VALUES ('2013310101', '31002', '68.00');
- INSERT INTO `score` VALUES ('2013310103', '21004', '80.00');
- INSERT INTO `score` VALUES ('2013310103', '31002', '76.00');
- INSERT INTO `score` VALUES ('2014210101', '21002', '93.00');
- INSERT INTO `score` VALUES ('2014210101', '21004', '89.00');
- INSERT INTO `score` VALUES ('2014210102', '21002', '95.00');
- INSERT INTO `score` VALUES ('2014210102', '21004', '88.00');
- INSERT INTO `score` VALUES ('2014310101', '21001', '79.00');
- INSERT INTO `score` VALUES ('2014310101', '21004', '80.00');
- INSERT INTO `score` VALUES ('2014310102', '21001', '91.00');
- INSERT INTO `score` VALUES ('2014310102', '21004', '87.00');
再是course表
- CREATE TABLE `course` (
- `课程号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `课程名` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `学分` int NOT NULL,
- `学时` int NOT NULL,
- `学期` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
- `前置课` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
- PRIMARY KEY (`课程号`) USING BTREE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
插入数据
- INSERT INTO `course` VALUES ('11003', '管理学', '2', '32', '2', null);
- INSERT INTO `course` VALUES ('11005', '会计学', '3', '48', '2', null);
- INSERT INTO `course` VALUES ('21001', '计算机基础', '3', '48', '1', null);
- INSERT INTO `course` VALUES ('21002', 'OFFICE高级应用', '3', '48', '2', '21001');
- INSERT INTO `course` VALUES ('21004', '程序设计', '4', '64', '2', '21001');
- INSERT INTO `course` VALUES ('21005', '数据库', '4', '64', '4', '21004');
- INSERT INTO `course` VALUES ('21006', '操作系统', '4', '64', '5', '21001');
- INSERT INTO `course` VALUES ('31001', '管理信息系统', '3', '48', '3', '21004');
- INSERT INTO `course` VALUES ('31002', '信息系统_分析与设计', '2', '32', '4', '31001');
- INSERT INTO `course` VALUES ('31005', '项目管理', '3', '48', '5', '31001');
最后是class
- CREATE TABLE `class` (
- `班级编号` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `班级名称` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `院系` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `年级` int DEFAULT NULL,
- `人数` int DEFAULT NULL,
- PRIMARY KEY (`班级编号`) USING BTREE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
插入数据
- INSERT INTO `class` VALUES ('AC1301', '会计13-1班', '会计学院', '2013', '35');
- INSERT INTO `class` VALUES ('AC1302', '会计13-2班', '会计学院', '2013', '35');
- INSERT INTO `class` VALUES ('CS1401', '计算机14-1班', '计算机学院', '2014', '35');
- INSERT INTO `class` VALUES ('IS1301', '信息系统13-1班', '信息学院', '2013', null);
- INSERT INTO `class` VALUES ('IS1401', '信息系统14-1班', '信息学院', null, '30');
建完表后
开始写Java代码
类比较多,一个一个来
先写主类StudentInformationManageSystems
- package StudentInformationManageSystem;
-
- import java.util.Scanner;
-
- public class StudentInformationManageSystems {
- public static void main(String[] args) {
- System.out.println("-------------欢迎来到学生信息管理系统---------");
- StudentMain.studentmains();
- while (true){
- Scanner input = new Scanner(System.in);
- int scan = input.nextInt();
- switch (scan){
- case 1:
- StudentAdd.add();
- System.out.println("-----------------------");
- StudentMain.studentmains();
- break;
- case 2:
- StudentQuery.query();
- System.out.println("-----------------------");
- StudentMain.studentmains();
- break;
- case 3:
- StudentUpdate.update();
- System.out.println("-----------------------");
- StudentMain.studentmains();
- break;
- case 4:
- StudentDelete.delete();
- System.out.println("-----------------------");
- StudentMain.studentmains();
- break;
- case 5:
- StudentClass.stuclass();
- System.out.println("-----------------------");
- StudentMain.studentmains();
- break;
- case 6:
- StudentCourse.course();
- System.out.println("-----------------------");
- StudentMain.studentmains();
- break;
- case 7:
- StudentScore.score();
- System.out.println("-----------------------");
- StudentMain.studentmains();
- break;
- case 8:
- System.exit(0);
- default:
- System.out.println("无效的输入!");
- System.exit(0);
- }
- }
- }
- }
-
按顺序来,再写studentmains
- package StudentInformationManageSystem;
-
- public class StudentMain {
-
- public static void studentmains(){
- System.out.println("1.增加学生信息");
- System.out.println("2.查询学生信息");
- System.out.println("3.修改学生信息");
- System.out.println("4.删除学生信息");
- System.out.println("5.查询学生所在班级");
- System.out.println("6.查询学生所选课程");
- System.out.println("7.查询学生所课程成绩");
- System.out.println("8.退出");
- System.out.println("-------------------");
- System.out.println("请输入你的选择:");
- }
- }
然后写StudentAdd
- package StudentInformationManageSystem;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
-
- public class StudentAdd {
- public static void add() {
- Connection connection;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
- String name = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url, name, password);
- Statement stmt = connection.createStatement();
-
- Scanner input = new Scanner(System.in);
- System.out.println("请输入学号:");
- int id = input.nextInt();
- System.out.println("请输入姓名:");
- String studnetname = input.next();
- System.out.println("请输入性别:");
- String gender = input.next();
- System.out.println("请输入出生日期:");
- String brithday = input.next();
- System.out.println("请输入地区:");
- String area = input.next();
- System.out.println("请输入民族:");
- String nation = input.next();
- System.out.println("请输入班级编号:");
- String classId = input.next();
- String sql = "insert into bookstore.student(学号, 姓名, 性别, 出生日期, 地区, 民族, 班级编号) " +
- "values(" + id + ",'" + studnetname + "','" + gender + "','" + brithday + "','" + area + "','" + nation + "','" + classId + "') ";
- stmt.executeUpdate(sql);
- System.out.println("学生添加成功!");
- stmt.close();
- connection.close();
- } catch (SQLException | ClassNotFoundException exception) {
- throw new RuntimeException(exception);
- }
- }
- }
然后是StudentQuery
- package StudentInformationManageSystem;
-
- import java.sql.*;
-
- public class StudentQuery {
- public static void query(){
- Connection connection;
- try {
- //加载驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
- //获取连接
- String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
- String name = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url,name,password);
- Statement stmt = connection.createStatement();
- String sql = "select * from student";
- ResultSet resultSet = stmt.executeQuery(sql);
- while(resultSet.next()){
- int id = resultSet.getInt("学号");
- String Studentname = resultSet.getString("姓名");
- String gender = resultSet.getString("性别");
- String brithday = resultSet.getString("出生日期");
- String area = resultSet.getString("地区");
- String nation = resultSet.getString("民族");
- String classId = resultSet.getString("班级编号");
- System.out.println("学号:"+id+" 姓名:"+Studentname+" 性别:"+gender+" 出生日期:"
- +brithday+" 地区:"+area+" 民族:"+nation+" 班级编号:"+classId);
- }
- stmt.close();
- connection.close();
- } catch (SQLException | ClassNotFoundException ex) {
- throw new RuntimeException(ex);
-
- }
- }
- }
再是StudentUpdate
- package StudentInformationManageSystem;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
-
- public class StudentUpdate {
- public static void update(){
- Connection connection;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
- String name = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url, name, password);
- Statement stmt = connection.createStatement();
- Scanner input = new Scanner(System.in);
- System.out.println("请输入要修改学生的学号:");
- int id = input.nextInt();
- System.out.println("请输入要修改的列:");
- String colum = input.next();
- System.out.println("请输入要修改的值:");
- String value = input.next();
- String sql = "UPDATE bookstore.student SET " + colum + " = '" + value + "' WHERE 学号 = " + id;
- stmt.executeUpdate(sql);
- System.out.println("学生修改成功!");
- stmt.close();
- connection.close();
- }catch (SQLException | ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
- }
然后是StudentDelete
- package StudentInformationManageSystem;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
-
- public class StudentDelete {
- public static void delete(){
- Connection connection;
- try {
- String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
- String name = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url, name, password);
- Statement stmt = connection.createStatement();
- Scanner input = new Scanner(System.in);
- System.out.println("请输入要删除的学号:");
- int id = input.nextInt();
- String sql = "delete from bookstore.student where 学号 = " + id;
- stmt.executeUpdate(sql);
- System.out.println("学生删除成功!");
- stmt.close();
- connection.close();
- } catch (SQLException exception) {
- throw new RuntimeException(exception);
- }
- }
- }
再是StudentClass
- package StudentInformationManageSystem;
-
- import java.sql.*;
- import java.util.Scanner;
-
- public class StudentClass {
- public static void stuclass() {
- Connection connection;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
- String name = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url, name, password);
- Statement stmt = connection.createStatement();
- Scanner input = new Scanner(System.in);
- System.out.println("请输入学生姓名:");
- String stuname = input.next();
- String sql = "select * from bookstore.class where 班级编号 = (select 班级编号 from bookstore.student where 姓名 = '"+stuname+"')";
- ResultSet resultSet = stmt.executeQuery(sql);
- System.out.println("班级编号 | 班级名称 | 院系 | 年级 | 人数 |");
- while (resultSet.next()){
- String classid = resultSet.getString("班级编号");
- String classname = resultSet.getString("班级名称");
- String college= resultSet.getString("院系");
- String grade = resultSet.getString("年级");
- String popluations = resultSet.getString("人数");
- System.out.println(classid+" | "+classname+" |" +
- " "+college+" | "+grade+" | "+popluations+" | ");
- }
- stmt.close();
- connection.close();
- } catch (SQLException | ClassNotFoundException exception) {
- throw new RuntimeException(exception);
- }
- }
- }
然后是StudentCourse
- package StudentInformationManageSystem;
-
- import java.sql.*;
- import java.util.Scanner;
-
- public class StudentCourse {
- public static void course() {
- Connection connection;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
- String name = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url, name, password);
- Statement stmt = connection.createStatement();
- Scanner input = new Scanner(System.in);
- System.out.println("请输入学生姓名:");
- String stuname = input.next();
- String sql = "select * from bookstore.course where 课程号 =" +
- " any (select 课程号 from bookstore.score where 学号 = any (select 学号 from bookstore.student where 姓名 = '"+stuname+"'))";
- ResultSet resultSet = stmt.executeQuery(sql);
- System.out.println("课程号 | 课程名 | 学分 | 学时 | 学期 |");
- while (resultSet.next()){
- String coureid = resultSet.getString("课程号");
- String courename = resultSet.getString("课程名");
- String score = resultSet.getString("学分");
- String time = resultSet.getString("学时");
- String term = resultSet.getString("学期");
- System.out.println(coureid+" | "+courename+" | "+score+" | "+time+" | "+term+" | ");
- }
- stmt.close();
- connection.close();
- } catch (SQLException | ClassNotFoundException exception) {
- throw new RuntimeException(exception);
- }
- }
- }
再是StudentScore
- package StudentInformationManageSystem;
-
- import java.sql.*;
- import java.util.Scanner;
-
- public class StudentScore {
- public static void score() {
- Connection connection;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/bookstore?useSSL=false";
- String name = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url, name, password);
- Statement stmt = connection.createStatement();
- Scanner input = new Scanner(System.in);
- System.out.println("请输入学生姓名:");
- String stuname = input.next();
- System.out.println("请输入课程名:");
- String coursename = input.next();
- String sql = "select 成绩 from score where 学号 = any (select 学号 from student where 姓名 = '"+stuname+"')and" +
- " 课程号 = any (select 课程号 from course where 课程名 = '"+coursename+"')";
- ResultSet resultSet = stmt.executeQuery(sql);
- while (resultSet.next()){
- int score = resultSet.getInt("成绩");
- System.out.println("----------------");
- System.out.println("成绩:"+score);
- System.out.println("----------------");
- }
- } catch (SQLException | ClassNotFoundException exception) {
- throw new RuntimeException(exception);
- }
- }
- }
这样整个代码就完成了,最后呈上运行结果
截图太大了,这样我就不一个一个呈现了,大家做出来以后自行操作吧,有什么问题可以在评论区询问,我看到都会一一回复,最后祝大家敲代码愉快!
ps:文章有什么错误可以帮我纠正,谢谢大家!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。