package lianjie;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Student {
final static String drive="com.mysql.jdbc.Driver";
final static String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false";
static String user = "root";
static String password = "admin";
private String id;
private String name;
private String sex;
private String brithday;
private String english;
private String math;
private String computer;
public static String getUser() {
return user;
}
public static void setUser(String user) {
Student.user = user;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
Student.password = password;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
public String getEnglish() {
return english;
}
public void setEnglish(String i) {
this.english = i;
}
public String getMath() {
return math;
}
public void setMath(String math) {
this.math = math;
}
public String getComputer() {
return computer;
}
public void setComputer(String computer) {
this.computer = computer;
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Student s = new Student();
Class.forName(drive);
Connection conn = DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
System.out.println("----------学生成绩管理系统----------");
s.xianshi();
}
public void xianshi()throws Exception {
Student s = new Student();
Class.forName(drive);
Connection conn = DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
System.out.println("-------------1.显示全部成绩---------");
System.out.println("-------------2.查找-----------------");
System.out.println("-------------3.删除-----------------");
System.out.println("-------------4.增添-----------------");
System.out.println("-------------5.修改-----------------");
System.out.println("请输入你想选择的功能编号:");
Scanner i =new Scanner(System.in);
switch(i.nextInt()) {
case 1:
System.out.println(" 学号 "+" 姓名 "+" 性别 "+" 出生年月 "+"英语 "+" 数学 "+" 计算机");
ResultSet rs = stmt.executeQuery("SELECT * FROM STU");
while(rs.next()) {
System.out.println(rs.getString("id")+" "+rs.getString("name")+" "+rs.getString("sex")+" "+rs.getString("brithday")+" "+rs.getString("english")+" "+rs.getString("math")+" "+rs.getString("computer"));
}
System.out.println("1.返回主菜单");
Scanner x=new Scanner(System.in);
int y=x.nextInt();
switch(y) {
case 1:s.xianshi();
break;
}
case 2:
s.selectStudent();
System.out.println("1.返回主菜单");
System.out.println("2.返回上层");
Scanner x2=new Scanner(System.in);
int y2=x2.nextInt();
switch(y2) {
case 1:s.xianshi();
break;
case 2:s.selectStudent();
break;
}
break;
case 3:
s.deleteStudent();
System.out.println("1.返回主菜单");
System.out.println("2.返回上层");
Scanner x3=new Scanner(System.in);
int y3=x3.nextInt();
switch(y3) {
case 1:s.xianshi();
break;
case 2:s.deleteStudent();
break;
}
break;
case 4:
s.addStudent();
System.out.println("1.返回主菜单");
System.out.println("2.返回上层");
Scanner x4=new Scanner(System.in);
int y4=x4.nextInt();
switch(y4) {
case 1:s.xianshi();
break;
case 2:s.addStudent();
break;
}
break;
case 5:
s.updateStudent();
System.out.println("1.返回主菜单");
System.out.println("2.返回上层");
Scanner x5=new Scanner(System.in);
int y5=x5.nextInt();
switch(y5) {
case 1:s.xianshi();
break;
case 2:s.updateStudent();
break;
}
break;
}
}
public void selectStudent() throws SQLException {
Connection conn=DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement();
Student s=new Student();
System.out.println("请选择查询的方式:");
System.out.println("1.按学号进行查询");
System.out.println("2.按姓名进行查询");
Scanner k = new Scanner(System.in);
switch(k.nextInt()) {
case 1:
System.out.println("请输入学号进行查询:");
Scanner id=new Scanner(System.in);
String select="SELECT * FROM stu WHERE id='"+id.nextLine()+"'";
ResultSet rs1=stmt.executeQuery(select);
if(rs1.next()) {
rs1.previous();
while(rs1.next()) {
s.setId(rs1.getString("id"));
s.setName(rs1.getString("name"));
s.setSex(rs1.getString("sex"));
s.setBrithday(rs1.getString("brithday"));
s.setEnglish(rs1.getString("english"));
s.setMath(rs1.getString("math"));
s.setComputer(rs1.getString("computer"));
System.out.println("学号:"+s.getId()+" "+"姓名:"+s.getName()+" "+"性别:"
+s.getSex()+" "+"出生年月:"+s.getBrithday()+" "+"英语:"+s.getEnglish()+" "+"数学:"+s.getMath()+" "+"计算机:"+s.getComputer());
}
}
else System.out.println("该学号不存在!");
break;
case 2:
System.out.println("请输入姓名进行查询:");
Scanner name=new Scanner(System.in);
String select1="SELECT * FROM stu WHERE name='"+name.nextLine()+"'";
ResultSet rs2=stmt.executeQuery(select1);
if(rs2.next()) {
rs2.previous();
while(rs2.next()) {
s.setId(rs2.getString("id"));
s.setName(rs2.getString("name"));
s.setSex(rs2.getString("sex"));
s.setBrithday(rs2.getString("brithday"));
s.setEnglish(rs2.getString("english"));
s.setMath(rs2.getString("math"));
s.setComputer(rs2.getString("computer"));
System.out.println("学号:"+s.getId()+" "+"姓名:"+s.getName()+" "+"性别:"
+s.getSex()+" "+"出生年月:"+s.getBrithday()+" "+"英语:"+s.getEnglish()+" "+"数学:"+s.getMath()+" "+"计算机:"+s.getComputer());
}
}
else System.out.println("该姓名不存在!");
break;
}
}
public void deleteStudent() throws SQLException {
Connection conn=DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement();
System.out.println("请输入学生学号进行删除:");
Scanner delete=new Scanner(System.in);
String delete_id="DELETE FROM stu WHERE id="+"'"+delete.nextLine()+"'";
stmt.executeUpdate(delete_id);
System.out.println("删除成功!");
}
public void addStudent() throws SQLException {
Student add=new Student();
Connection conn=DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement();
System.out.println("请输入新增学生信息:");
System.out.println("请输入学号:");
Scanner id=new Scanner(System.in);
add.setId(id.nextLine());
System.out.println("输入姓名:");
Scanner name=new Scanner(System.in);
add.setName(name.nextLine());
System.out.println("输入性别:");
Scanner sex=new Scanner(System.in);
add.setSex(sex.nextLine());
System.out.println("输入出生年月:");
Scanner brithday=new Scanner(System.in);
add.setBrithday(brithday.nextLine());
System.out.println("输入英语成绩:");
Scanner english=new Scanner(System.in);
add.setEnglish(english.nextLine());
System.out.println("输入数学成绩:");
Scanner math=new Scanner(System.in);
add.setMath(math.nextLine());
System.out.println("输入计算机成绩:");
Scanner computer=new Scanner(System.in);
add.setComputer(computer.nextLine());
String addStudent="INSERT INTO stu(id,name,sex,brithday,english,math,computer)VALUES('"+add.getId()
+"','"+add.getName()+"','"+add.getSex()+"','"+add.getBrithday()+"','"+add.getEnglish()+"','"+add.getMath()+"','"+add.getComputer()+"')";
stmt.executeUpdate(addStudent);
System.out.println("添加成功!");
stmt.close();
conn.close();
}
public void updateStudent() throws Exception {
Student up=new Student();
Connection conn=DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement();
System.out.println("请输入新增学生信息:");
System.out.println("请输入学号:");
Scanner id=new Scanner(System.in);
up.setId(id.nextLine());
System.out.println("输入姓名:");
Scanner name=new Scanner(System.in);
up.setName(name.nextLine());
System.out.println("输入性别:");
Scanner sex=new Scanner(System.in);
up.setSex(sex.nextLine());
System.out.println("输入出生年月:");
Scanner brithday=new Scanner(System.in);
up.setBrithday(brithday.nextLine());
System.out.println("输入英语成绩:");
Scanner english=new Scanner(System.in);
up.setEnglish(english.nextLine());
System.out.println("输入数学成绩:");
Scanner math=new Scanner(System.in);
up.setMath(math.nextLine());
System.out.println("输入计算机成绩:");
Scanner computer=new Scanner(System.in);
up.setComputer(computer.nextLine());
String addStudent="INSERT INTO stu(id,name,sex,brithday,english,math,computer)VALUES('"+up.getId()
+"','"+up.getName()+"','"+up.getSex()+"','"+up.getBrithday()+"','"+up.getEnglish()+"','"+up.getMath()+"','"+up.getComputer()+"')";
stmt.executeUpdate(addStudent);
System.out.println("添加成功!");
stmt.close();
conn.close();
}
}