首先,我在数据库中导入数据表:
CREATE TABLE`zhouzhigang`.`examstudent_2` (
`FlowID` INT NOT NULLAUTO_INCREMENT COMMENT '流水号',
`Type` INT DEFAULT NULLCOMMENT '四级/六级',
`IdCard` VARCHAR(18)DEFAULT NULL COMMENT '身份证号',
`ExamCard` VARCHAR(15)DEFAULT NULL COMMENT '准考证号',
`StudentName` VARCHAR(20)DEFAULT NULL COMMENT '学生姓名',
`Location` VARCHAR(20)DEFAULT NULL COMMENT '区域',
`Grade` INT DEFAULT NULLCOMMENT '成绩',
PRIMARY KEY (`FlowID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
我首先来干这样一件事:创建一个Student类:
package com.zhou.jdbc;
public class Student {
/**
* 这些字段都是跟数据库中examstudent数据表示对应的:
*/
// 流水号
private int flowId;
// 考试的类型
private int type;
// 身份证号
private String idCard;
// 准考证号
private String examCard;
// 学生姓名
private String studentName;
// 地址
private String location;
// 考试分数
private int grade;
/**
* 当然会有对应的getter和setter方法:
*/
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;
}
/**
* 还会有一个有参数的构造器:
* @param flowId
* @param type
* @param idCard
* @param examCard
* @param studentName
* @param location
* @param 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() {
super();
}
/**
* 还会有一个toString()方法来做测试:
*/
@Override
public String toString() {
return "Student [flowId=" + flowId + ", type=" + type + ", idCard="
+ idCard + ", examCard=" + examCard + ", studentName="
+ studentName + ", location=" + location + ", grade=" + grade
+ "]";
}
}
------------------------------------------------------------------------------------------------------------------------
写一个JdbcTools的工具类:
package com.zhou.jdbc;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/**
* 版本1:后面这些方法还可以升级,比方说我们不能每次操作都获取一个数据库连接,比方说后面会学数据库连接池;
*
* 操作JDBC的工具类:其中封装了一些工具方法:
*
* 1.获取连接的方法;
*
* @author Administrator
*
*/
public class JdbcTools {
/**
* 1.获取连接的方法:这个方法在前面已经写过了,只是这个是工具方法,需要加上static: ---通过读取配置文件从数据库服务器获取一个连接;
*
* 用DriverManager来把之前那个通用的方法写一遍:
*
* @throws Exception
*/
public static Connection getConnection() throws Exception {
// 1.准备连接数据库的四个字符串:
// 1).创建Properties对象:
Properties properties = new Properties();
// 2).获取jdbc.properties对应的输入流:
InputStream in = JdbcTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
// 3).加载2) 对应的输入流:
properties.load(in);
// 4).具体决定user、password、jdbcUrl、driverClass四个字符串:
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String jdbcUrl = properties.getProperty("jdbcUrl");
String driverClass = properties.getProperty("dirver");
// 2.加载数据库驱动程序(实际上这一步应该叫:注册驱动,对应的Driver实现类中有注册驱动的静态代码块):
Class.forName(driverClass);
// 3.获取数据库的连接:
// Connection connection = DriverManager.getConnection(jdbcUrl, user,
// password);
// return connection;
return DriverManager.getConnection(jdbcUrl, user, password);
}
/**
* 2.关闭数据库资源的方法: 它关闭一个Statement,再关闭一个Connection:
*/
public static void release(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* 3.关闭数据库资源的方法: 它关闭三个:ResultSet、Statement、Connection:
*/
public static void release_2(ResultSet resultSet ,Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
/**
* 4.执行SQL的方法:
* SQL:INSERT、UPDATE、DELETE,而不包含SELECT;
*/
public static void update(String sql){
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {// 关闭连接:这里不再写那个嵌套的try……catch……finally了
release(statement, connection);
}
}
}
--------------------------------------------------------------------------------------------------------------------------------
写实现功能的代码:
package
com.zhou.jdbc;
import java.util.Scanner;
import org.junit.Test;
publicclass JDBC_Test_OPP {
/**
* 写完publicvoid addNewStudent(Student student)之后呢,我们可以来写这样一个测试的方法
*/
@Test
publicvoidtestAddNewStudent(){
// 首先获取一个Student对象,从控制台的输入中获取:
Student student = getStudentFromConsole();
// 然后调用addNewStudent(Student student)方法添加一个学生信息:
addNewStudent(student);
}
/**
* 创建用从控制台获取Student对象的方法:getStudentFromConsole()
*
* @return
*/
private Student getStudentFromConsole() {
// 创建一个Scanner类的对象;
Scanner scanner= new Scanner(System.in);
// 然后呢?创建一个Student类的对:
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;
}
/**
* 第一个我们叫什么呢?叫addNewStudent 这样就写成了一个方法,我要添加一个学生进来,我们当然可以硬写,怎么写呢?
* 就是真的从控制台逐步读入那些信息,然后把这些信息插入到数据库里面;
*/
publicvoid addNewStudent(Student student) {
/**
* 这个方法基本在写流程的时候已经写过了:
*/
// 1.准备一条SQL语句:
// String sql =“INSERT INTO examstudent”+" VALUES(1,4,’’,’’,’’,’’,90)”;
String sql = "INSERT INTOexamstudent" + " VALUES("
+ student.getFlowId() + "," + student.getType() + ",'"
+ student.getIdCard()+ "','" + student.getExamCard() + "','"
+ student.getStudentName() + "','" + student.getLocation()
+ "'," + student.getGrade() + ")";
// 2.调用JdbcTools工具类的update(sql)方法执行插入操作;
JdbcTools.update(sql);
}
}