• JDBC插入数据,获取自增长值


    package com.loaderman.demo.c_auto;
    
    public class Dept {
    
        private int id;
        private String deptName;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getDeptName() {
            return deptName;
        }
        public void setDeptName(String deptName) {
            this.deptName = deptName;
        }
        
        
    }
    package com.loaderman.demo.c_auto;
    
    public class Employee {
    
        private int empId;
        private String empName;
        // 关联的部门
        private Dept dept;
        
        
        public Dept getDept() {
            return dept;
        }
        public void setDept(Dept dept) {
            this.dept = dept;
        }
        public int getEmpId() {
            return empId;
        }
        public void setEmpId(int empId) {
            this.empId = empId;
        }
        public String getEmpName() {
            return empName;
        }
        public void setEmpName(String empName) {
            this.empName = empName;
        }
        
        
        
        
    }
    package com.loaderman.demo.c_auto;
    
    import com.loaderman.demo.utils.JdbcUtil;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    
    public class EmpDao {
    
        private Connection con;
        private PreparedStatement pstmt;
        private ResultSet rs;
    
        // 保存员工,同时保存关联的部门
        public void save(Employee emp){
    
            // 保存部门
            String sql_dept = "insert into dept(deptName) values(?)";
            // 保存员工
            String sql_emp = "INSERT INTO employee (empName,dept_id) VALUES (?,?)";
            // 部门id
            int deptId = 0;
    
            try {
                // 连接
                con = JdbcUtil.getConnection();
    
                /*****保存部门,获取自增长*******/
                // 【一、需要指定返回自增长标记】
                pstmt = con.prepareStatement(sql_dept,Statement.RETURN_GENERATED_KEYS);
                // 设置参数
                pstmt.setString(1, emp.getDept().getDeptName());
                // 执行
                pstmt.executeUpdate();
    
                // 【二、获取上面保存的部门子增长的主键】
                rs =  pstmt.getGeneratedKeys();
                // 得到返回的自增长字段
                if (rs.next()) {
                    deptId = rs.getInt(1);
                }
    
                /*****保存员工*********/
                pstmt = con.prepareStatement(sql_emp);
                // 设置参数
                pstmt.setString(1, emp.getEmpName());
                pstmt.setInt(2, deptId);
                pstmt.executeUpdate();
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JdbcUtil.closeAll(con, pstmt, rs);
            }
        }
    }
    package com.loaderman.demo.c_auto;
    
    import org.junit.Test;
    
    public class App {
    
    
        // 保存员工
        @Test
        public void testSave() throws Exception {
            // 模拟数据
            Dept d = new Dept();
            d.setDeptName("应用开发部");
            Employee emp = new Employee();
            emp.setEmpName("李俊杰");
            emp.setDept(d);   // 关联
    
            // 调用dao保存
            EmpDao empDao = new EmpDao();
            empDao.save(emp);
    
        }
    }

    思路:保存员工及其对应的部门

             步骤:

    1. 先保存部门
    2. 再得到部门主键,再保存员工
  • 相关阅读:
    UVa-10317
    UVa-1595
    UVa-10391
    UVa-10763
    UVa-10935
    UVa-1594
    UVa-1593
    从CSDN搬过来了
    memset会显著增加时间和空间的消耗吗
    memset对数组的初始化
  • 原文地址:https://www.cnblogs.com/loaderman/p/10007834.html
Copyright © 2020-2023  润新知