码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-11-04 01:40:03      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:des   oaf   pcc   new   har   cst   ftp   primary   zrc   

真难……

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

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();
        }
    }
}

 

运行结果:

技术分享

技术分享

技术分享

技术分享

技术分享

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

标签:des   oaf   pcc   new   har   cst   ftp   primary   zrc   

原文地址:http://www.cnblogs.com/wenruo/p/6028716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!