5.6作业--JDBC注册功能实现

使用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("注册失败,重新操作。");
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值