JDBC connection to the database query print data

By day's video learning, understanding the connection principle jdbc came Summary:

Reading a database table rows cursor read one, getXxx () method for reading a data table columns  

next () method allows the cursor down

Database table can be seen as a class, each record is an object, the table to be encapsulated

Test class Jdbc08 .java

/ ** 
 * practice db4 query data and then print the emp 
 * define a method, query data table emp packaged as an object, and then mounted in the collection. 
 * 1. Define class class emp 
 * 2. The method defined in public return List <Emp > the findAll () 
 * 3. * from EMP-implemented method SELECT 
 * @author three nuts 
 * @date 2019/9/14 21:17 
 * / 
public  class Jdbc08 {
     public  static  void main (String [] args) { 
        List <Emp > All = new new Jdbc08 () the findAll ();. 
        all.forEach (the System.out :: the println); 
        System.out.println (all.size () + "records" ); 
    } 
    public List <Emp> the findAll ( ) { // query all data emp
        Connection conn =null;
        Statement statement =null;
        ResultSet rs =null;
        List<Emp> list=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "123");
            String sql="select * from emp";
            statement = conn.createStatement();
            rs = statement.executeQuery(sql);//rs is a cursor pointing line, getXxx ( "parameter") refers to a data column, so that a selected data
             // iterate a result, the package objects in the collection means 
            Emp EMP = null ; 
            List = new new   the ArrayList <> ();
             the while (rs .next ()) {
                 int ID = rs.getInt ( "ID" ); 
                String ename = rs.getString ( "ename" );
                 int the job_id = rs.getInt ( "the job_id" );
                 int MGR = rs.getInt ( " MGR " ); 
                a Date joindate = rs.getDate (" joindate " );
                 Double the salary = RS.getDouble("salary");
                double bouns = rs.getDouble("bonus");
                int dept_id = rs.getInt("dept_id");
                //封装对象
                emp=new Emp();
                emp.setId(id);
                emp.setEname(ename);
                emp.setJob_id(job_id);
                emp.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBouns(bouns);
                emp.setDept_id(dept_id);
                list.add(emp);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {//释放资源
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
/**
 * 晚安!
 * @author BinPeng
 * @date 2019/9/14 22:04
 */

Emp.java package type ALT + INSERT IDEA shortcut keys generated automatically !!!

package HeiMaSQL02.emp08;

import java.util.Date;

/**
 * 封装Emp表
 * @author BinPeng
 * @date 2019/9/14 21:24
 */
public class Emp {
    private int id;
    private String ename;
    private int job_id;
    private int mgr;
    private Date joindate;
    private double  salary;
    private double bouns;
    private int dept_id;

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", ename='" + ename + '\'' +
                ", job_id=" + job_id +
                ", mgr=" + mgr +
                ", joindate=" + joindate +
                ", salary=" + salary +
                ", bouns=" + bouns +
                ", dept_id=" + dept_id +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public int getJob_id() {
        return job_id;
    }

    public void setJob_id(int job_id) {
        this.job_id = job_id;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public Date getJoindate() {
        return joindate;
    }

    public void setJoindate(Date joindate) {
        this.joindate = joindate;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getBouns() {
        return bouns;
    }

    public void setBouns(double bouns) {
        this.bouns = bouns;
    }

    public int getDept_id() {
        return dept_id;
    }

    public void setDept_id(int dept_id) {
        this.dept_id = dept_id;
    }
}

 emp database table (a database gave you ready! terrific, right !!!)

/*
 Navicat MySQL Data Transfer

 Source Server         : first
 Source Server Type    : MySQL
 Source Server Version : 50725
 Source Host           : localhost:3306
 Source Schema         : db4

 Target Server Type    : MySQL
 Target Server Version : 50725
 File Encoding         : 65001

 Date: 14/09/2019 22:15:35
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for emp
-- ----------------------------
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp`  (
  `id` int(11) NOT NULL,
  `ename` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  `job_id` int(11) NULL DEFAULT NULL,
  `mgr` int(11) NULL DEFAULT NULL,
  `joindate` date NULL DEFAULT NULL,
  `salary` decimal(7, 2) NULL DEFAULT NULL,
  `bonus` decimal(7, 2) NULL DEFAULT NULL,
  `dept_id` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `emp_jobid_ref_job_id_fk`(`job_id`) USING BTREE,
  INDEX `emp_deptid_ref_dept_id_fk`(`dept_id`) USING BTREE,
  CONSTRAINT `emp_deptid_ref_dept_id_fk` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `emp_jobid_ref_job_id_fk` FOREIGN KEY (`job_id`) REFERENCES `job` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of emp
------------------------------ 
the INSERT  the INTO `emp` the VALUES ( 1001 , 'la ' , 4 , 1004 , ' 2000-12 -17 ' , 8000.00 , NULL , 20 is );
 the INSERT  the INTO `emp` the VALUES ( 1002 , ' acridine acridine ' , . 3 , 1006 , ' 2001-02-20 ' , 16000.00 , 3000.00 , 30 );
 the INSERT INTO `emp` VALUES (1003, '啊吖', 3, 1006, '2001-02-22', 12500.00, 5000.00, 30);
INSERT INTO `emp` VALUES (1004, '订单', 2, 1009, '2001-04-02', 29750.00, NULL, 20);
INSERT INTO `emp` VALUES (1005 , ' LI method ' , . 4 , 1006 , ' 2001-09-28 ' , 12500.00 , 14000.00 , 30 );
 the INSERT  the INTO `emp` the VALUES ( 1006 , ' Song method ' , 2 , 1009 , ' 2001-05-01 ' , 28500.00 , NULL , 30 );
 the INSERT  the INTO `emp` the VALUES ( 1007 ,'发', 2, 1009, '2001-09-01', 24500.00, NULL, 10);
INSERT INTO `emp` VALUES (1008, '猪发', 4, 1004, '2007-04-19', 30000.00, NULL, 20);
INSERT INTO `emp` VALUES (1009, 'utah', 1, NULL , ' 2001-11-17 ' , 50000.00 , NULL , 10 );
 the INSERT  the INTO `emp` the VALUES ( 1010 , ' NG her ' , 3 , 1006 , ' 2001-09-08 ' , 15000.00 , 0.00 , 30 );
 INSERT  INTO `emp` VALUES ( 1011 , ' Sha let him ' , 4 , 1004, ' 2007-05-23 ' , 11000.00 , NULL , 20 is );
 the INSERT  the INTO `emp` the VALUES ( 1012 , ' Li him ' , . 4 , 1006 , ' 2001-12-03 ' , 9500.00 , NULL , 30 );
 the INSERT  the INTO `emp` the VALUES ( 1013 , 'hungry dragon ' , 4 , 1004 , '2001-12-03', 30000.00, NULL, 20);
INSERT INTO `emp` VALUES (1014, '饿', 4, 1007, '2002-01-23', 13000.00, NULL, 10);

SET FOREIGN_KEY_CHECKS = 1;

 

Guess you like

Origin www.cnblogs.com/july7/p/11520548.html