录入学员信息
来源:互联网 发布:天谕流光女捏脸数据 编辑:程序博客网 时间:2024/05/03 17:59
环境准备:
包结构:
======================
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()); }}
未完待续!
阅读全文
0 0
- class 学员信息录入
- 录入学员信息
- 获取键盘录入信息
- 录入学生信息
- 制作信息录入窗体
- java__for录入会员信息
- 学生信息录入
- 身份证信息录入
- 删除FireFox录入信息记录
- 1-1-3录入信息
- 实现会员信息录入功能
- 如何快速录入房产证信息?
- [易飞]录入信息传递设置信息
- 要求循环录入2个班的学员成绩,假设每个班都有3个学员, 依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
- TextBox里录入的信息相当于Combox
- 题目:北京市二手房交易信息录入程序
- 项目小结---信息录入的验证
- 05.[推荐]录入信息的技巧
- shell正则表达式
- 215. Kth Largest Element in an Array(数组中第k大元素)
- JavaScript基本概念(四)
- vue-devtools在Chrom浏览器上的安装
- 优化 Vue 项目编译文件大小
- 录入学员信息
- 【springboot 入门篇】第0篇 spring-boot详细解释
- 我的第一篇博客--成长的第一站
- C++悬挂指针: new与delete的一些理解
- bitmap算法的介绍
- 机器学习和深度学习的资料大全
- 微信小程序 6 媒体api
- s3c2440之外部中断
- linux用户管理