最基本的JDBC

JDBC 基础 笔记

1. 开始 写 JDBC

  • 第一步 创建 一个普通的 java项目

  • 然后在项目根目录创建 lib 文件夹 floder 用于存放项目支持的jar包 -- (mysql-jdbc-3.1.12-bin.jar) 这个是 mysql的驱动jar包

  • 导入 jar

2. 开始

所有sql 语法:

创建数据库的语法: create database 库名;

创建数据表的语法: create table 表名(字段名 字段数据类型(字段长度),字段2 字段类型(字段长度),字段3.......);

给表插入数据的语法:insert into 表名(字段1,字段2,字段3) values(字段1的值,字段2的值,字段3的值);

  • 创建 数据库和数据表 编写 sql 指令

     创建数据库
     create database myday;
     create: 创建
     database: 库
     myday: 库名
     
     使用数据库
     use myday;
     use: 使用
     
     创建数据表
     create table employee_info(
      id int primary key auto_increment comment '员工编号',
      name varchar(32) comment '姓名',
      sex varchar(4) comment '性别',
      age int comment '年龄',
      position varchar(32) comment '职位'
     );
     table : 表
     employee_info:表名
     comment: 对字段的注释 用来让别人操作你的数据库看的懂你表中字段的含义
  • 创建 DbHeloper 类 连接数据库 在 :package com.hp.db;

     package com.hp.db;
     
     import java.sql.Connection;
     import java.sql.DriverManager;
     
     public class DbHelper {
     
      Connection conn = null;
     
         public DbHelper(){
             try {
                 Class.forName("com.mysql.jdbc.Driver");
                 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myday?useUnicode=true&characterEncoding=utf8","root","mysql");
            } catch (Exception e) {
                 e.printStackTrace();
            }
        }
         public Connection getConn(){
             return conn;
        }
     }
  • 创建 对应数据表的实体类 Employee 在:package com.hp.entity;

     package com.hp.entity;
     
     public class Employee {
      private int id;
      private String name;
      private String sex;
      private int age;
      private String position;
     
      public int getId() {
      return id;
      }
      public void setId(int id) {
      this.id = id;
      }
      public String getName() {
      return name;
      }
      public void setName(String name) {
      this.name = name;
      }
      public String getSex() {
      return sex;
      }
      public void setSex(String sex) {
      this.sex = sex;
      }
      public int getAge() {
      return age;
      }
      public void setAge(int age) {
      this.age = age;
      }
      public String getPosition() {
      return position;
      }
      public void setPosition(String position) {
      this.position = position;
      }
      public Employee(int id, String name, String sex, int age, String position) {
      this.id = id;
      this.name = name;
      this.sex = sex;
      this.age = age;
      this.position = position;
      }
      public Employee() {
      }
     }
     
  • 创建 接口 dao类

     package com.hp.dao;
     
     import java.util.List;
     
     import com.hp.entity.Employee;
     
     public interface EmpDao{
      //增
      public boolean addEmp(Employee emp);
     
      //删
      public int delEmp(int id);
     
      //改
      public int updEmp(Employee emp);
     
      //查
      public List<Employee> findAll();
     }
     
  • 创建 接口实现类 实现接口

     package com.hp.dao;
     
     import java.sql.Connection;
     import java.sql.PreparedStatement;
     import java.sql.ResultSet;
     import java.sql.SQLException;
     import java.util.ArrayList;
     import java.util.List;
     
     import com.hp.db.DbHelper;
     import com.hp.entity.Employee;
     
     public class EmpDaoImpl implements EmpDao {
      Connection conn;
      PreparedStatement ps;
      ResultSet rs;
     
      public boolean addEmp(Employee emp) {
      boolean flag = false;
      conn = new DbHelper().getConn();
      String sql = "insert into employee_info(id,name,sex,age,position) values(?,?,?,?,?)";
      try {
      ps = conn.prepareStatement(sql);
      ps.setInt(1, emp.getId());
      ps.setString(2, emp.getName());
      ps.setString(3, emp.getSex());
      ps.setInt(4, emp.getAge());
      ps.setString(5, emp.getPosition());
      if(ps.executeUpdate()>0) {
      flag = true;
      }
      } catch (SQLException e) {
      e.printStackTrace();
      }
      return flag;
      }
     
      public int delEmp(int id) {
      return 0;
      }
     
      public int updEmp(Employee emp) {
      return 0;
      }
     
      public List<Employee> findAll() {
      List<Employee> ul = new ArrayList<Employee>();
      Employee emp = null;
      conn = new DbHelper().getConn();
      String sql = "select * from employee_info";
      try {
      ps = conn.prepareStatement(sql);
      rs = ps.executeQuery();
      while (rs.next()) {
      emp = new Employee();
      emp.setId(rs.getInt("id"));
      emp.setName(rs.getString("name"));
      emp.setSex(rs.getString("sex"));
      emp.setAge(rs.getInt("age"));
      emp.setPosition(rs.getString("position"));
      ul.add(emp);
      }
      } catch (SQLException e) {
      e.printStackTrace();
      }
      return ul;
      }
     }
  • 最后运行 测试

     package com.hp.test;
     
     import java.util.List;
     import java.util.Scanner;
     
     import org.junit.Test;
     
     import com.hp.dao.EmpDaoImpl;
     import com.hp.entity.Employee;
     
     public class MyTest {
      public static void main(String[] args) {
      EmpDaoImpl empl = new EmpDaoImpl();
      System.out.println("------请选择------");
      System.out.println("-----1.显示所有员工信息-----");
      System.out.println("-----2.新增员工-----");
      Scanner sc = new Scanner(System.in);
      int begin = sc.nextInt();
      if(begin == 1) {
      System.out.println("所有员工的信息如下");
      List<Employee> findAll = empl.findAll();
      for (Employee employee : findAll) {
      System.out.println("编号:"+employee.getId()+"\t"+"姓名:"+employee.getName()+"\t"+"性别:"+employee.getSex()+"\t"+"年龄:"+employee.getAge()+"\t"+"职位:"+employee.getPosition());
      }
      }else if(begin == 2) {
      System.out.println("------新增员工------");
      System.out.println("请输入员工的id");
      int id = sc.nextInt();
      System.out.println("请输入员工的名字");
      String name = sc.next();
      System.out.println("请输入员工的性别");
      String set = sc.next();
      System.out.println("请输入员工的年龄名字");
      int age = sc.nextInt();
      System.out.println("请输入员工的职位");
      String position = sc.next();
      if(empl.addEmp(new Employee(id,name,set,age,position))) {
      System.out.println("增加成功");
      }else {
      System.out.println("增加失败");
      }
      }else {
      System.out.println("你的输入有误");
      }
      }
     }
     
  • 测试的内容 全查

     ------请选择------
     -----1.显示所有员工信息-----
     -----2.新增员工-----
     1
     所有员工的信息如下
     编号:1 姓名:张三 性别: 年龄:22 职位:职员
     编号:2 姓名:李四 性别: 年龄:22 职位:经理
     编号:3 姓名:3 性别:3 年龄:3 职位:3
  • 测试的内容 增加

     ------请选择------
     -----1.显示所有员工信息-----
     -----2.新增员工-----
     2
     ------新增员工------
     请输入员工的id
     3
     请输入员工的名字
     3
     请输入员工的性别
     3
     请输入员工的年龄名字
     3
     请输入员工的职位
     3
     增加成功
     
  •  

posted @ 2020-05-12 15:13  刘盛哲的学习笔记  阅读(425)  评论(0编辑  收藏  举报