录入学员信息
程序员文章站
2022-03-21 08:26:56
...
环境准备:
包结构:
======================
student.java
package com.atguigu.jdbc;
public class Student {
private Integer id;
private int type;
private String idCard;
private String examCard;
private String studentName;
private String location;
private int grade;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(Integer id, int type, String idCard, String examCard,
String studentName, String location, int grade) {
this.id = id;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", type=" + type + ", idCard=" + idCard
+ ", examCard=" + examCard + ", studentName=" + studentName
+ ", location=" + location + ", grade=" + grade + "]";
}
}
===============================================
db.properties
jdbc.user=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///jdbc
//jdbc.jdbcUrl=jdbc:mysql://127.0.0.1/jdbc
/*
jdbc.user=atguigu
jdbc.password=atguigu
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
*/
数据库表:
=========================
第一步:
编写工具类:
JdbcUtils.java
package com.atguigu.jdbc;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
public static void closeConnection(Connection connection){
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//从数据库连接池中获取连接
public static Connection getConnection2() throws Exception{
return dataSource.getConnection();
}
public static Connection getConnection() throws Exception{
//1. 加载资源文件
Properties properties = new Properties();
InputStream inStream = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
properties.load(inStream);
//2. 获取数据库连接需要的四个基本信息:
String user = properties.getProperty("jdbc.user");
String password = properties.getProperty("jdbc.password");
String driverClass = properties.getProperty("jdbc.driverClass");
String jdbcUrl = properties.getProperty("jdbc.jdbcUrl");
//3. 加载驱动
Class.forName(driverClass);
//4. 从 DriverManager 中来获取连接
Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
return connection;
}
}
======================
第二步:
测试数据库连接并录入学员信息:
JdbcTest.java
package com.atguigu.jdbc.test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.junit.Test;
import com.atguigu.jdbc.JdbcUtils;
import com.atguigu.jdbc.Student;
import com.atguigu.jdbc.StudentDao;
public class JdbcTest {
@Test
public void testGetConnection() throws Exception {
Connection connection = JdbcUtils.getConnection();
System.out.println(connection);
}
/**
* Dao: Database Access Object, 即数据库访问对象. 把根数据库相关的所有操作(CRUD)都封装在这样的一个类中.
*
* @throws Exception
* @throws SQLException
*/
@Test
public void testSaveStudent() throws SQLException, Exception {
// 1. 从控制台输入一组数据, 转为一个 Student 对象
System.out.println("请输入考生的详细信息:");
Student student = getFromConsole();
// 2. 调用 StudentDao 中的一个方法.
StudentDao studentDao = new StudentDao();
studentDao.save(student);
System.out.println("信息录入成功!");
}
private Student getFromConsole() {
Scanner scanner = new Scanner(System.in);
System.out.println("type:");
int type = scanner.nextInt();
System.out.println("IdCard:");
String idCard = scanner.next();
System.out.println("ExamCard:");
String examCard = scanner.next();
System.out.println("StudentName:");
String studentName = scanner.next();
System.out.println("Location:");
String location = scanner.next();
System.out.println("Grade:");
int grade = scanner.nextInt();
Student student = new Student(null, type, idCard, examCard,
studentName, location, grade);
return student;
}
}
==================================
StudentDao.java
package com.atguigu.jdbc;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
/**
* StudentDao: Student 这个实体类和 Student 实体类对应的数据表的 数据库访问对象(DAO) 类
* 其中只包含对数据表的操作, 例如 CRUD。但不包含任何的业务逻辑.
*/
public class StudentDao {
//QueryRunner 是线程安全的!
private QueryRunner queryRunner = new QueryRunner();
public void save(Student student) throws SQLException, Exception{
String sql = "INSERT INTO examstudent(type, id_card, exam_card, "
+ "student_name, location, grade) VALUES(?,?,?,?,?,?)";
queryRunner.update(JdbcUtils.getConnection(), sql, student.getType(),
student.getIdCard(), student.getExamCard(), student.getStudentName(),
student.getLocation(), student.getGrade());
}
}
未完待续!
上一篇: 更改Maven软件源为阿里云源
下一篇: *经典常用的CSS属性收集整理_经验交流