使用JDBC完成注册功能,从0开始,从新搭建项目,熟悉下过程(切记切记)。提交注册成功的截图即可
第一步:创建数据库db5 并创建表employee
注册后表employee
第二步 :定义驱动类xml
<?xml version="1.0" encoding="UTF-8" ?>
<jdbc>
<!--定义驱动类-->
<property name = "driverClass">com.mysql.jdbc.Driver</property>
<property name = "url">jdbc:mysql://localhost:3306/db5</property>
<property name = "user">root</property>
<property name = "passWord">000000</property>
</jdbc>
第三步:封装JDBC工具类
/**
* JDBC工具类
*/
public class JDBCUtils {
public static String DRIVER_NAME;
public static String URL;
public static String USER;
public static String PASSWORD;
static {
try {
SAXReader reader = new SAXReader();
Document document = reader.read("resources/JDBC-config.xml");
DRIVER_NAME = document.selectSingleNode("//property[@name='driverClass']").getText();
URL = document.selectSingleNode("//property[@name='url']").getText();
USER = document.selectSingleNode("//property[@name='user']").getText();
PASSWORD = document.selectSingleNode("//property[@name='passWord']").getText();
Class.forName(DRIVER_NAME);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL,USER,PASSWORD);
}
public static void close(Connection connection, Statement statement){
if (connection != null && statement != null){
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection connection, Statement statement, ResultSet resultSet){
if (connection != null && statement != null && resultSet != null){
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
第四步:编写Employee类 生成Getter and Setter 方法 以及 toString()方法
public class Employee {
private int eid;
private String ename;
private int age;
private String sex;
private double salary;
private String empdate;
@Override
public String toString() {
return "Employee{" +
"eid=" + eid +
", ename='" + ename + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", salary=" + salary +
", empdate='" + empdate + '\'' +
'}';
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getEmpdate() {
return empdate;
}
public void setEmpdate(String empdate) {
this.empdate = empdate;
}
}
第五步:创建连接 插入数据 注册 登录
public class EmployeeDao {
public int employeeDao(Employee employee) {
Connection connection = null;
PreparedStatement ps = null;
try {
//获取连接
connection = JDBCUtils.getConnection();
//编写sql语句
ps = connection.prepareStatement("insert into employee values (?,?,?,?,?,?);");
//设置占位符
ps.setInt(1,employee.getEid());
ps.setString(2,employee.getEname());
ps.setInt(3,employee.getAge());
ps.setString(4,employee.getSex());
ps.setDouble(5,employee.getSalary());
ps.setString(6,employee.getEmpdate());
//执行sql语句
ps.executeUpdate();
//返回影响行数
return ps.getUpdateCount();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(connection,ps);
}
return 0;
}
}
第六步:编写测试类
public class TestEmployeeDao {
private EmployeeDao e = new EmployeeDao();
@Test
public void testEmployeeDao() throws Exception {
Employee employee = new Employee();
employee.setEid(6);
employee.setEname("厉飞雨");
employee.setAge(23);
employee.setSex("男");
employee.setSalary(7000);
employee.setEmpdate("2022-5-6");
int i = e.employeeDao(employee);
if (i>0){
System.out.println("注册成功,欢迎"+employee.getEname());
} else {
System.out.println("注册失败,重新操作。");
}
}
}