学生成绩管理系统 1.0(Java+MySql)

真难……

数据库建立不会,中文编码不会,插入数据不会,删除不会……

Java读入数据不会……数据库连接不会……

你也好意思说自己是学计算机的啊魂淡……

我会慢慢写2.0,3.0版的……噗……

src/wenr/entity/Student.java

package wenr.entity;

/**
数据库定义

CREATE TABLE Student (
    sid CHAR(10) PRIMARY KEY,
    sname CHAR(20) NOT NULL,
    sc DOUBLE(5,2),
    sm DOUBLE(5,2),
    se DOUBLE(5,2),
    st DOUBLE(5,2)
);

*/

public class Student {
    private String id;
    private String name;
    private double scoreChinese;
    private double scoreMath;
    private double scoreEnglish;
    private double scoreTot;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getScoreChinese() {
        return scoreChinese;
    }
    public void setScoreChinese(double scoreChinese) {
        this.scoreChinese = scoreChinese;
    }
    public double getScoreMath() {
        return scoreMath;
    }
    public void setScoreMath(double scoreMath) {
        this.scoreMath = scoreMath;
    }
    public double getScoreEnglish() {
        return scoreEnglish;
    }
    public void setScoreEnglish(double scoreEnglish) {
        this.scoreEnglish = scoreEnglish;
    }
    public double getScoreTot() {
        return scoreTot;
    }
    public void setScoreTot(double scoreTot) {
        this.scoreTot = scoreTot;
    }

}

src/wenr/oper/Mainpage.java

package wenr.oper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import wenr.db.DBClose;
import wenr.db.DBConnection;
import wenr.tool.InputNumber;

public class Mainpage {

    static Statement statement = null; 

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int choose = 0;

        statement = DBConnection.getconnection();

        do {

            System.out.println("===============================\n" +
                                "\t学生成绩管理系统\n\n" +
                                "\t1.查询个人信息\n" +
                                "\t2.查询全部信息\n" +
                                "\t3.添加信息\n" +
                                "\t4.删除信息\n" +
                                "\t0.退出\n" +
                                "===============================\n");

            choose = InputNumber.SimpleInteger(0, 4);
            if (choose == 1) {
                System.out.print("输入要查询的学号:");
                String qid = in.next();
                String result = query(qid);
                if (result.equals("")) {
                    System.out.println("结果为空.");
                } else {
                    System.out.println(result);
                }
            } else if (choose == 2) {
                System.out.println("1.按语文成绩排序\n2.按数学成绩排序\n3.按英语成绩排序\n4.按总成绩排序\n");
                int qKind = InputNumber.SimpleInteger(1, 4);
                String result = queryAll(qKind);
                if (result.equals("")) {
                    System.out.println("结果为空.");
                } else {
                    System.out.println(result);
                }
            } else if (choose == 3) {
                add();
            } else if (choose == 4){
                delete();
            }

        } while (choose != 0);

        System.out.println("Bye~");

        in.close();

        try {
            if (statement != null)
                statement.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static String query(String qid) {
        StringBuilder result = new StringBuilder();
        String sql = "select * from student where sid = \‘" + qid + "\‘";
        ResultSet resultSet = null;
        try {
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                result.append("学号\t\t姓名\t语文\t数学\t英语\t 总成绩");
                result.append("\n"   + resultSet.getString("sid"));
                result.append("\t" + resultSet.getString("sname"));
                result.append("\t" + resultSet.getString("sc"));
                result.append("\t" + resultSet.getString("sm"));
                result.append("\t" + resultSet.getString("se"));
                result.append("\t" + resultSet.getString("st") + "\n");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBClose.close(resultSet);
        return result.toString();
    }

    public static String queryAll(int qKind) {
        String sql = "select * from student order by ";
        if (qKind == 1) sql += "sc";
        if (qKind == 2) sql += "sm";
        if (qKind == 3) sql += "se";
        if (qKind == 4) sql += "st";
        StringBuilder result = new StringBuilder();
        ResultSet resultSet = null;
        try {
            resultSet = statement.executeQuery(sql);
            boolean first = true;
            while (resultSet.next()) {
                if (first) { result.append("学号\t\t姓名\t语文\t数学\t英语\t 总成绩"); first = false; }
                result.append("\n"   + resultSet.getString("sid"));
                result.append("\t" + resultSet.getString("sname"));
                result.append("\t" + resultSet.getString("sc"));
                result.append("\t" + resultSet.getString("sm"));
                result.append("\t" + resultSet.getString("se"));
                result.append("\t" + resultSet.getString("st"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBClose.close(resultSet);
        return result.toString();
    }

    public static void add() {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入学号(10个数字):");
        String id = InputNumber.getId();
        System.out.print("输入姓名:");
        String name = in.next();
        System.out.print("输入语文成绩:");
        double sChinese = InputNumber.getScore();
        System.out.print("输入数学成绩:");
        double sMath = InputNumber.getScore();
        System.out.print("输入英语成绩:");
        double sEnglish = InputNumber.getScore();
        double sTot = sChinese + sMath + sEnglish;
        String sql = "insert into student values(\‘" + id + "\‘,\‘" + name + "\‘, "
                + sChinese + "," + sMath + "," + sEnglish + "," + sTot + ")";
        try {
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void delete() {
        Scanner in = new Scanner(System.in);
        System.out.println("输入要删除的学号:");
        String qid = in.next();
        String result = query(qid);
        if ("".equals(result)) {
            System.out.println("不存在该学号.");
        } else {
            System.out.println(result);
            System.out.print("确认删除?(1确定,0返回):");
            int choose = InputNumber.SimpleInteger(0, 1);
            if (choose == 1) {
                String sql = "delete from student where sid= \‘" + qid + "\‘";
                System.out.println(sql);
                try {
                    statement.executeUpdate(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                System.out.println("已删除");
            }
        }
    }

}

src/wenr/tool/InputNumber.java

package wenr.tool;

import java.util.Scanner;

public class InputNumber {
    public static int SimpleInteger(int low, int high) {
        Scanner in = new Scanner(System.in);
        int result = -1;
        String input = null;

        System.out.print("请按提示输入单个数字:");

        while (true) {
            input = in.next();
            if (input.length() == 1 && Character.isDigit(input.charAt(0))){
                result = Integer.parseInt(input);
                if (result <= high && result >= low) break;
            }
            System.out.print("输入错误.请按提示输入单个数字:");
        }
        return result;
    }

    public static String getId() {
        Scanner in = new Scanner(System.in);
        String id = null;

        re:
        while (true) {
            id = in.next();
            if (id.length() == 10) {
                for (int i = 0; i < 10; i++) {
                    if (!Character.isDigit(id.charAt(i))) break;
                    if (i == 9) break re;
                }
            }
            System.out.print("输入错误.请输入学号(10个数字):");
        }
        return id;
    }

    public static double getScore() {
        Scanner in = new Scanner(System.in);
        String score = null;
        while (true) {
            score = in.next();
            if (score.matches("\\d+(.\\d+)?")) break;
            System.out.print("输入成绩格式不正确,请重新输入:");
        }
        double result = 0;
        try {
            result = Double.parseDouble(score);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}

src/wenr/db/DBConnection.java

package wenr.db;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {

    //MySQL JDBC驱动字符串
    public static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver";
    //数据库Url,用来标识要连接的数据库,其中数据库名、用户名、密码是根据你自己的数据库情况设定
    public static final String URL = "jdbc:mysql://localhost:3306/SSMS?"
            + "user=root&password=123456&useUnicode=true&characterEncoding=UTF-8";

    public static Statement statement;

    public static Statement getconnection() {
        Connection connection = null;

        try {
            Class.forName(DRIVER_MYSQL);     //加载JDBC驱动

            connection = DriverManager.getConnection(URL);    //创建数据库连接对象
            statement = connection.createStatement();       //创建Statement对象
        } catch (Exception e) {
            e.printStackTrace();
        }

        return statement;
    }
}

src/wenr/db/DBClose.java

package wenr.db;

import java.sql.ResultSet;
import java.sql.SQLException;

public class DBClose {
    public static void close(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

运行结果:

时间: 2024-10-21 11:06:06

学生成绩管理系统 1.0(Java+MySql)的相关文章

学生成绩管理系统 2.0(图形界面)

这个写的……太……羞耻了…… 本来以为能写的很好的……图形界面什么的……啊啊啊好难…… 写一半写不下去了……然后就什么功能都没有…… 1 package com.wenr.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList;

学生成绩管理系统3.0(JSP+Servlet+MySQL)

源代码:戳这里! 环境:MyEclipse 2016 CI & MySQL5.5 & apache-tomcat-8.5.9 实现功能: 项目结构: 部分页面效果: 蠢新手,蠢瞎写,请多指教.

java开发的简易学生成绩管理系统

经过1个月的紧张学习和应用,终于做出了这个简易的学生成绩管理系统. 代码如下(仅供参考学习) view包下的菜单 package com.xujulong.www.view: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import com.xujulong.www.po.Manager; import

简易学生成绩管理系统

做了一个小型的成绩管理系统.主要代码如下: /* * 功能:简易学生成绩管理系统 */package day7; import java.util.Scanner; public class HomeWork { static Scanner scan=new Scanner(System.in); static String[] user=new String[2]; static String[] password=new String[2]; static String[] name=ne

《C语言编写 学生成绩管理系统》

/* (程序头部凝视開始)* 程序的版权和版本号声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名: 学生成绩管理系统 * 作 者: 刘江波 * 完毕日期: 2012 年 6 月 23 日* 版 本 号: v.623 * 对任务及求解方法的描写叙述部分 * 程序头部的凝视结束 */ #include "stdio.h" #include"string" /*定义学生结构体*/ struc

学生信息管理系统 1.0

学生信息管理系统V1.0 1.实现用户的注册与登录功能. 1.1:注册时检测用户名,如果有重复则需要重新输入,用户信息存入数据库. 1.2:登录时,有验证码验证. 2.实现用户查询学生表的基本信息. 3.用户可以更改.删除学生表信息. 4.系统可以正确显示系统时间. 5.系统正确显示用户权限(功能暂未实现). 因为很多东西还没有学到,只能用最基础的代码实现功能. 1)欢迎页面 1 <%@ page language="java" contentType="text/ht

C语言练手自己编写学生成绩管理系统

#include<stdio.h> #include<stdlib.h> /*定义学生结构体*/ struct Student { char ID[20]; char Name[20]; float Mark1; float Mark2; float Mark3; float Avarage; }; /*声明学生数组及学生数量*/ struct Student students[1000]; int num=0; /*计算平均成绩*/ float Avg(struct Studen

【学生成绩管理系统】 大二c语言作业

几年前写的了,只能在命令行窗口运行,虽然比较挫,还是有一定参考价值... #include <cstdio> #include <conio.h> #include <iostream> #include <process.h> #include <stdlib.h> #include <algorithm> #include <cstring> #include <cmath> #define N 5 con

c++学生成绩管理系统

虽然比较水 =.= 但是写了两节课+一个中午 都是强迫症的锅 http://www.cnblogs.com/wenruo/p/4940182.html #include <cstdio> #include <cstring> #include <iostream> using namespace std; class student // student 类为管理系统的一个节点 { friend class studentMessage; student *next;