第一步,连接数据库
mysql> USE test;
Database changed
mysql> CREATE TABLE mydb
-> (
-> zhanghao VARCHAR(25),
-> mima VARCHAR(18),
-> );
第二步,构造学生类
package sql;
//实体类 封装学生数据
public class Student {
String name;
String studentId;
String sex;
int grade;
//有参构造函数
public Student(String name,String studentId,String sex,int grade){
this.name= name;
this.studentId= studentId;
this.sex = sex;
this.grade = grade;
}
//方法:get & set
public int getGrade(){
return grade;
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public void setGrade(int g){
this.grade = g;
}
public String getStudentId(){
return studentId;
}
}
第三步,测试学生类
package sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//输入的数据 导入包---Scanner(import)
import java.util.*;
public class Test {
//数组、List --display
static List<Student> StudentList = new LinkedList<Student>();
public static void main(String[] agrs) throws Exception{
//输入用户名和密码
Scanner sc=new Scanner(System.in);
//用户名输入三次登陆系统自动结束
for(int i=3;i>0;i--){
System.out.println("请输入账号");
String zh=sc.nextLine();
System.out.println("请输入密码");
String mm=sc.nextLine();
Class.forName("com.mysql.jdbc.Driver");
String jdbc="jdbc:mysql://127.0.0.1:3306/test";
Connection conn=DriverManager.getConnection(jdbc, "root", "123");
String sql="select * from mydb where zhanghao=? and mima=?"; //sql语句
PreparedStatement state=conn.prepareStatement(sql); //容器
state.setString(1, zh); //将第n个值替换成某个值
state.setString(2, mm);
ResultSet re=state.executeQuery(); //上传数据库返回结果集
if(re.next()){ //如果取到了值,那么输出
System.out.println("登陆成功"+re.getString(1)+",欢迎你");
//所有的实现过程全部放在select();
select(StudentList);
return;
}
else{
System.out.println("登陆失败,账号或密码输入错误,请重新输入,您还有"+(i-1)+"次机会");
}
}
System.out.println("连续三次输入错误,系统被迫退出!");
}
//学生信息管理系统的菜单选择 --- 增 删 改 查
private static void select(List<Student> StudentList ){
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:退出系统 *");
System.out.println("***************");
System.out.println("您想选择的操作是:");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch(choice){
//增加学生
case 1:
System.out.print("请输入学生的姓名:");
Scanner Sname = new Scanner(System.in);
String name = Sname.nextLine();
System.out.print("请输入学生的性别:");
Scanner Ssex = new Scanner(System.in);
String sex = Ssex.nextLine();
System.out.print("请输入学生的学号:");
Scanner SId = new Scanner(System.in);
String studentId = SId.nextLine();
System.out.print("请输入学生的成绩:");
Scanner Sgrade = new Scanner(System.in);
int grade = Sgrade.nextInt();
StudentList.add(new Student(name,studentId,sex,grade));
System.out.println("添加成功!!!!!");
select(StudentList);
break;
//删除学生成绩
case 2:
System.out.print("请告诉我需要删除学生的学号:");
Scanner Sid = new Scanner(System.in);
String SstudentId = Sid.nextLine();
boolean isfindDelete = false;
//大部分的操作都是循环来遍历操作 依靠for
for (int i = 0; i < StudentList.size(); i++) {
if(SstudentId.equals(StudentList.get(i).getStudentId())){
System.out.println("发现了该学生,正在删除...");
StudentList.remove(i);
System.out.println("删除成功!!!");
isfindDelete =true;
}
}
if(!isfindDelete){
System.out.println("抱歉,没有找到");
}
select(StudentList);
break;
//修改学生成绩
case 3:
System.out.print("请告诉我需要修改成绩学生的学号:");
Scanner GId = new Scanner(System.in);
String GstudentId = GId.nextLine();
boolean isfindChange = false;
for (int j = 0; j < StudentList.size(); j++) {
if(GstudentId.equals(StudentList.get(j).getStudentId())){
System.out.println("发现了该学生,正在修改...");
System.out.println("学生原成绩为"+StudentList.get(j).getGrade());
System.out.print("请输入修改后学生的成绩:");
Scanner Ggrade = new Scanner(System.in);
int grade2 = Ggrade.nextInt();
StudentList.get(j).setGrade(grade2);
System.out.println("修改成功!!!");
isfindChange =true;
}else{
}
}
if(!isfindChange){
System.out.println("抱歉,没有找到");
}
select(StudentList);
break;
//查看学生成绩
case 4:
System.out.print("请告诉我需要查询学生的学号:");
Scanner CId = new Scanner(System.in);
String CstudentId = CId.nextLine();
boolean isfindData = false;
for (int i = 0; i < StudentList.size(); i++) {
if(CstudentId.equals(StudentList.get(i).getStudentId())){
System.out.println("名字:"+StudentList.get(i).getName());
System.out.println("性别:"+StudentList.get(i).getSex());
System.out.println("学号:"+StudentList.get(i).getStudentId());
System.out.println("成绩:"+StudentList.get(i).getGrade());
isfindData = true;
}
}
if(!isfindData){
System.out.println("抱歉,没有找到");
}
select(StudentList);
break;
default:
System.out.println("您输入的数字有误,请重新输入:");
break;
//退出系统
case 5:
System.out.print("谢谢使用");
System.exit(0); //JVM退出
}
}
}