JDBC-四六级成绩录入,删除,查询(练习)
Posted Vodka~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC-四六级成绩录入,删除,查询(练习)相关的知识,希望对你有一定的参考价值。
//CET.java
package PreparedStatementCURD;
import Instrument.Operation;
import User.CetStu;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
/**
* @author Vodka
* @date 2021/07//15:49
*/
public class CET {
public static void main(String[] args){
InsertCET();
QueryScore();
DeleteCET();
}
//四六级成绩查询
public static void QueryScore(){
Scanner in = new Scanner(System.in);
String Symbol = null;
String ExamCard = null;
String IDCard = null;
//选择查询的证件类型
do{
System.out.println("请选择您要查询的证件类型:");
System.out.println("a:准考证号");
System.out.println("b:身份证号");
Symbol = in.next();
if (!Objects.equals(Symbol, "a") && !Objects.equals(Symbol,"b")){
System.out.println("您的证件选择有误,请重新输入!");
}else{
break;
}
} while (true);
//输入相应证件号
switch(Symbol){
case "a":
System.out.println("请输入您的准考证号:");
ExamCard = in.next();
break;
case "b":
System.out.println("请输入您的身份证证号:");
IDCard = in.next();
break;
}
//通过相应的证件号查询四六级成绩,并返回结果
String CardNum = ExamCard == null ? IDCard : ExamCard;
//利用反射获取CET表的对应类 (CetStu) .再利用获取到的类,创建实例,并返回
String sql = "select * from CET where IDCard = ? or ExamCard = ?";
List<CetStu> CS =Operation.TableSearch(CetStu.class,sql,CardNum,CardNum);
if(CS.isEmpty()) System.out.println("没有该生信息,请重新查询!");
//输出考生信息
for(CetStu obj : CS){
System.out.println("==========查询结果=============");
System.out.printf("学生姓名:%s\\n",obj.getStudentName());
System.out.printf("流水号:%d\\n" , obj.getFLowId());
System.out.printf("考生号:%s\\n" , obj.getExamCard());
System.out.printf("身份证号:%s\\n" , obj.getIDCard());
System.out.printf("考试类别:%s\\n" , obj.getSpecies());
System.out.printf("考试区域:%s\\n" , obj.getLocation());
System.out.printf("成绩:%d\\n" , obj.getGrade());
System.out.printf("考试时间:%s\\n" , obj.getInTime());
}
}
//四六级数据插入
public static void InsertCET(){
Scanner in = new Scanner(System.in);
int Species ,Grade;
String IDCard , ExamCard ,StudentName ,Location ;
System.out.print("请输入学生考试种类:");
Species = in.nextInt();
System.out.print("请输入学生考试成绩:");
Grade = in.nextInt();
System.out.print("请输入学生身份证号:");
IDCard = in.next();
System.out.print("请输入学生考试证号:");
ExamCard = in.next() ;
System.out.print("请输入学生姓名:");
StudentName = in.next() ;
System.out.print("请输入学生考场位置:");
Location = in.next();
String sql = "Insert into CET (Species,IDCard , ExamCard ,StudentName ,Location ,Grade) values (?,?,?,?,?,?)";
if (Operation.Update(sql, Species, IDCard, ExamCard, StudentName, Location, Grade) > 0) {
System.out.println("四六级数据插入成功");
} else {
System.out.println("四六级数据插入失败");
}
}
//四六级数据删除
public static void DeleteCET(){
int StuNum = 0;
Scanner in = new Scanner(System.in);
System.out.println("请输入要删除的学生的身份证号或者考生号:");
String IDNum = in.next();
String sql = "Delete from CET where IDCard = ? or ExamCard = ?";
if(Operation.Update(sql,IDNum,IDNum)>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
}
//Operation.java
package Instrument;
import User.CetStu;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author Vodka
* @date 2021/07//15:53
*/
public class Operation {
//高内聚,低耦合,封装一个curd通用的函数,因为参数是不确定的,所以利用可变参数
public static int Update(String sql,Object...args) {
Connection conn = null;
PreparedStatement ps = null;
int ExecuteRS = 0;
try {
//建立连接
conn = GetConnection.getConnection();
//预编译
ps = conn.prepareStatement(sql);
for (int index = 0; index < args.length; index++) {
ps.setObject(index + 1, args[index]); //这里要注意两个变量下标起始值的不同
}
//执行sql语句
/*
* 对于ps.execute():
* 1.如果执行的是查询操作,查询成功就返回true
* 2.如果执行的是增删改,失败就返回false
* 对于ps.executeUpdate():
* 1.执行增删改成功,就返回DML操作的行数,失败就什么都不返回
*
* */
ExecuteRS = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null && ps != null) GetConnection.Close(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return ExecuteRS;
}
public static <T> List<T> TableSearch(Class<CetStu> TableClass, String sql,Object...arg) {
//这里利用反射的Class,用来动态获取具体的表的相应的类,再根据反射获得的类来创建相应的对象
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
ArrayList<T> TList = null;
try {
conn = GetConnection.getConnection();
ps = conn.prepareStatement(sql);
//填充查询语句中的占位符
for (int index = 0; index < arg.length; ++index) {
ps.setObject(index + 1, arg[index]);
}
//执行查询操作,使用Result的实例对象来接收查询结果集
resultSet = ps.executeQuery();
//从结果集中获取数据库相应表的元数据(即数据类型),以及(元数据的个数)表的列数
ResultSetMetaData rsmd = resultSet.getMetaData();
int ColumnNums = rsmd.getColumnCount();
TList = new ArrayList<>();
//遍历结果集
while (resultSet.next()) {
//利用反射,动态获取相应表的类,再创建对象
T t = (T) TableClass.getDeclaredConstructor().newInstance();
//遍历每一列,获取数据
for (int index = 0; index < ColumnNums; ++index) {
//通过ResultSet获取每一列的值
Object ColumnValue = resultSet.getObject(index + 1);
//通过rsmd,获取相应的列名,getColumnLabel()该方法既能获取列名,也可以获取列的别名
String ColumnLabel = rsmd.getColumnLabel(index + 1);
//利用反射机制,动态获取对象的相应类,再通过列名获取其在相应类中的属性域
Field field = t.getClass().getDeclaredField(ColumnLabel);
field.setAccessible(true); // 当要修改不是public的属性时,要设置为truee
field.set(t, ColumnValue);
}
//添加到表的集合对象中
TList.add(t);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
GetConnection.CloseResource(conn, ps, resultSet);
} catch (Exception e) {
e.printStackTrace();
}
}
//返回包含所有查询对象的集合
return TList;
}
}
//CetStu.java
package User;
import java.sql.Timestamp;
import java.util.Objects;
/**
* @author Vodka
* @date 2021/07//17:30
*/
public class CetStu {
private int FLowId;
private int Species;
private int Grade;
private java.sql.Timestamp InTime ;
private String IDCard;
private String ExamCard;
private String StudentName;
private String Location;
public CetStu(){
super();
}
public CetStu(int FLowId, int species, int grade, Timestamp inTime, String IDCard, String examCard, String studentName, String location) {
this.FLowId = FLowId;
Species = species;
Grade = grade;
InTime = inTime;
this.IDCard = IDCard;
ExamCard = examCard;
StudentName = studentName;
Location = location;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CetStu cetStu = (CetStu) o;
return FLowId == cetStu.FLowId &&
Species == cetStu.Species &&
Grade == cetStu.Grade &&
Objects.equals(InTime, cetStu.InTime) &&
Objects.equals(IDCard, cetStu.IDCard) &&
Objects.equals(ExamCard, cetStu.ExamCard) &&
Objects.equals(StudentName, cetStu.StudentName) &&
Objects.equals(Location, cetStu.Location);
}
@Override
public int hashCode() {
return Objects.hash(FLowId, Species, Grade, InTime, IDCard, ExamCard, StudentName, Location);
}
public void setFLowId(int FLowId) {
this.FLowId = FLowId;
}
public void setSpecies(int species) {
Species = species;
}
public void setGrade(int grade) {
Grade = grade;
}
public void setInTime(Timestamp inTime) {
InTime = inTime;
}
public void setIDCard(String IDCard) {
this.IDCard = IDCard;
}
public void setExamCard(String examCard) {
ExamCard = examCard;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public void setLocation(String location微信查询四六级成绩代码