前期准备:
1. 准备一个student类
~~~
public class Student {
// 流水号
private int flowId;
// 考试的类型
private int type;
// 身份证号
private String idCard;
// 准考证号
private String examCard;
// 学生名
private String studentName;
// 学生地址
private String location;
// 考试分数.
private int grade;
public int getFlowId() {
return flowId;
}
public void setFlowId(int flowId) {
this.flowId = flowId;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getExamCard() {
return examCard;
}
public void setExamCard(String examCard) {
this.examCard = examCard;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Student(int flowId, int type, String idCard, String examCard,
String studentName, String location, int grade) {
super();
this.flowId = flowId;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
public Student() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [flowId=" + flowId + ", type=" + type + ", idCard="
+ idCard + ", examCard=" + examCard + ", studentName="
+ studentName + ", location=" + location + ", grade=" + grade
+ "]";
}
}
~~~
2. 一个从键盘输入数据的方法
~~~
private Student getStudentFromConsole() {
Scanner scanner = new Scanner(System.in);
Student student = new Student();
System.out.print("FlowId:");
student.setFlowId(scanner.nextInt());
System.out.print("Type: ");
student.setType(scanner.nextInt());
System.out.print("IdCard:");
student.setIdCard(scanner.next());
System.out.print("ExamCard:");
student.setExamCard(scanner.next());
System.out.print("StudentName:");
student.setStudentName(scanner.next());
System.out.print("Location:");
student.setLocation(scanner.next());
System.out.print("Grade:");
student.setGrade(scanner.nextInt());
return student;
}
~~~
3. 一个添加学生信息的方法
~~~
public void addNewStudent2(Student student) {
String sql = "INSERT INTO student(flowid, type, idcard, "
+ "examcard, studentname, location, grade) "
+ "VALUES(?,?,?,?,?,?,?)";
JDBCTools.update(sql, student.getFlowId(), student.getType(),
student.getIdCard(), student.getExamCard(),
student.getStudentName(), student.getLocation(),
student.getGrade());
}
~~~
![](https://box.kancloud.cn/5ed9026520b6a9bbc0997ecf79a9e59b_623x369.png)
![](https://box.kancloud.cn/3e9604ed1ec9ea859bb5110e116f1b52_805x573.png)