学生成绩管理系统
(1)添加学生的信息:姓名、学号、班级、出生年月、性别等
(2)输入成绩:输入学生科目、成绩
(3)查询:按学号查询学生姓名和各科成绩
(4)统计:按班级、分数段、科目等统计学生成绩
(5)修改:修改学生的信息、成绩等,只有管理员可以实现
(6)保存:数据库中。
package com.sdmu.add;
import java.util.Scanner;
public class login {
public void login1() {
System.out.println("你有三次机会");
for(int i=0;i<3;i++) {
Scanner s=new Scanner(System.in);
System.out.println("请输入学号:");
String inputId=s.next();
System.out.println("密码:");
String password=s.next();
if(inputId.equals("123")&&password.equals("123")) {
demo d=new demo();
d.init();
}
else {
System.out.println("输入错误");
}
}
}
package com.sdmu.add;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Scanner;
public class demo {
static Connection conn=null;
static Scanner sc=new Scanner(System.in);
public static void init() {
System.out.println("欢迎使用学生成绩管理系统");
System.out.println("..............");
System.out.println("1....请依次添加学生的信息:姓名,学号,班级,出生年月,性别");
System.out.println("2........请依次输入学生姓名,学号,科目成绩");
System.out.println("3....输入学号查学生姓名和各科成绩");
System.out.println("4....统计分数");
System.out.println("5....修改学生信息");
int a=sc.nextInt();
switch(a)
{
case 1: add(); break;
case 2: inputGrade(); break;
case 3: adminLogin.adminLogin1(); break;
case 4: stsum(); break;
case 5: adminLogin.updateinformation();break;
default :break;
}
}
/*
* 添加学生信息
*/
public static void add() {
System.out.println("1....请依次添加学生的信息:姓名,学号,班级,出生年月,性别");
String stname=sc.next();
String stid=sc.next();
String stclass=sc.next();
String age=sc.next();
String sex=sc.next();
ArrayListlist=new ArrayList<>();//数据结构
list.add(stname);
list.add(stid);
list.add(stclass);
list.add(age);
list.add(sex);
System.out.println(list.toString());
String sql =
"insert into 1class (stname,stid,stclass,age,sex) values(?,?,?,?,?)";
try {
conn=C3p0.getDBManger().getConnection();
PreparedStatement sta = conn.prepareStatement(sql);
sta.setString(1, stname);
sta.setString(2, stid);
sta.setString(3, stclass);
sta.setString(4, age);
sta.setString(5, sex);
sta.executeUpdate();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("退出请按0"+"....."+"继续请按1");
int a1=sc.nextInt();
if(a1==1) {
add();
}
else {
init();
}
}
/*
*输入成绩
*/
public static void inputGrade() {
System.out.println("2....请依次输入学生姓名,学号,语文成绩,数学成绩");
String stname=sc.next();
String stid=sc.next();
String Chinesegrade=sc.next();
String mathgrade=sc.next();
ArrayListlist=new ArrayList<>();
list.add(stname);
list.add(stid);
list.add(Chinesegrade);
list.add(mathgrade);
System.out.println(list.toString());
String sql =
"insert into 1classgrade (stname,stid,Chinesegrade,mathgrade) values(?,?,?,?)";
try {
conn=C3p0.getDBManger().getConnection();
PreparedStatement sta = conn.prepareStatement(sql);
sta.setString(1, stname);
sta.setString(2, stid);
sta.setString(3, Chinesegrade);
sta.setString(4, mathgrade);
sta.executeUpdate();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("退出请按0"+"....."+"继续请按2");
int a1=sc.nextInt();
if(a1==2) {
inputGrade();
}
else {
init();
}
}
/*统计分数
*/
public static void stsum() {
System.out.println("4....统计分数");
ResultSet rs=null;
String sql="select *from 1classgrade ";
int sum=0;
try {
conn=C3p0.getDBManger().getConnection();
PreparedStatement sta = conn.prepareStatement(sql);
ResultSet rset = sta.executeQuery("select * from 1classgrade ");
int rowCount = 0;
while(rset.next()) {
rowCount++;
}
//System.out.println(rowCount);
rs=sta.executeQuery();
for (int i = 0; i < rowCount; i++) {
while(rs.next()) {
String s=rs.getString("Chinesegrade");
int a=Integer.parseInt(s);
//System.out.println(a);
sum=sum+a;
}
}
System.out.println(sum);
} catch (Exception e) {
e.printStackTrace();
}
}
}`
package com.sdmu.add;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class adminLogin {
static Connection conn=null;
static Scanner sc=new Scanner(System.in);
public static void adminLogin1() {
System.out.println("你有三次机会");
for(int i=0;i<3;i++) {
Scanner s=new Scanner(System.in);
System.out.println("请输入管理员账号:");
String inputId=s.next();
System.out.println("请输入管理员密码:");
String password=s.next();
if(inputId.equals("admin")&&password.equals("admin")) {
System.out.println("输入8查询学生信息");
System.out.println("输入9更改学生信息");
int i1=sc.nextInt();
switch(i1) {
case 8: search();break;
case 9: updateinformation();break;
}
}
else {
System.out.println("输入错误");
}
}
}
/*
* :按学号查询学生姓名和各科成绩
*/
public static void search() {
//adminLogin1();
System.out.println("请输入学号..........");
String st=sc.next();
ResultSet rs=null;
String sql="select *from 1classgrade where stid=?";
try {
conn=C3p0.getDBManger().getConnection();
PreparedStatement sta = conn.prepareStatement(sql);
sta.setString(1, st);
rs=sta.executeQuery();
if(rs.next()) {
System.out.println(rs.getString("stname"));
System.out.println(rs.getString("stid"));
System.out.println(rs.getString("Chinesegrade"));
System.out.println(rs.getString("mathgrade"));
}else {
System.out.println("查无此人");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("退出请按0"+"....."+"继续请按3");
int a1=sc.nextInt();
if(a1==3) {
search();
}
else {
System.out.println("gundan");
}
}
/*
* 修改学生信息
*/
public static void updateinformation() {
adminLogin1();
System.out.println("请输入学号修改学生信息");
String st=sc.next();
System.out.println("请输入要修改的姓名,学号,语文成绩,数学成绩");
String st1=sc.next();
String st2=sc.next();
String st3=sc.next();
String st4=sc.next();
ResultSet rs=null;
String sql="update 1classgrade set stname=?,stid=?,Chinesegrade=?,mathgrade=?where stid=?";
try {
conn=C3p0.getDBManger().getConnection();
PreparedStatement sta = conn.prepareStatement(sql);
sta.setString(1, st1);
sta.setString(2, st2);
sta.setString(3, st3);
sta.setString(4, st4);
sta.setString(5, st);
int re= sta.executeUpdate();
if(re==1) {
System.out.println("更新完成");
}
else {
System.out.println("无此人");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//public static void main(String[] args) {
// adminLogin.adminLogin1();
//}
}
package com.sdmu.add;
import java.sql.Connection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0 {
ComboPooledDataSource cpds = new ComboPooledDataSource();
private C3p0() {
try {
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/demo1?characterEncoding=UTF-8");
cpds.setUser("root");
cpds.setPassword("123456");
cpds.setMaxPoolSize(20);
cpds.setMinPoolSize(5);
cpds.setInitialPoolSize(5);
cpds.setMaxStatements(180);
cpds.getConnection().close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
private static C3p0 c3p0 = new C3p0();
public static C3p0 getDBManger(){
return c3p0;
}
public Connection getConnection() throws Exception{
return cpds.getConnection();
}
}