packagewenr.oper;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.Scanner;importwenr.db.DBClose;importwenr.db.DBConnection;importwenr.tool.InputNumber;public classMainpage {static Statement statement = null;public static voidmain(String[] args) {
Scanner in= newScanner(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 staticString query(String qid) {
StringBuilder result= newStringBuilder();
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);returnresult.toString();
}public static String queryAll(intqKind) {
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= newStringBuilder();
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);returnresult.toString();
}public static voidadd() {
Scanner in= newScanner(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 voiddelete() {
Scanner in= newScanner(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("已删除");
}
}
}
}