封装jdbc,实现通用增删改查

JDBC(Java DataBase Connectivity),即Java数据库连接。简而言之,就是通过Java语言来操作数据库。

我们先创建数据库表

这里的text改为varchar(25) 

封装jdbc,实现通用增删改查_第1张图片

创建java项目

封装jdbc,实现通用增删改查_第2张图片

项目结构

封装jdbc,实现通用增删改查_第3张图片

pom依赖



    4.0.0

    org.example
    untitled
    1.0-SNAPSHOT

    
        11
        11
    
    
        
            mysql
            mysql-connector-java
            8.0.31
        
    

 

通过jdbc获取数据库连接

package org.example.db;


import java.sql.*;

public class JDBCconn {
    public static Connection getConn() {
        try {
            // 四要素
            String url = "jdbc:mysql://localhost:3307/test";
            String user = "root";
            String password = "123456";
            String driverName = "com.mysql.cj.jdbc.Driver";
            //实例化驱动
            Class.forName(driverName);
            //获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void close(Connection conn, PreparedStatement ps) {
        try {
            if (conn != null) conn.close();
            if (ps != null) ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs){
        try {
            if(conn!=null) conn.close();
            if(ps!=null) ps.close();
            if(rs!=null) rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

和数据库表对应的实体类

package org.example.entity;


public class User111 {

  private Integer id;
  private String name;
  private String password;
  private String address;
  private String phone;

  public Integer getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }


  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }


  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }


  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  @Override
  public String toString() {
    return "User111{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", password='" + password + '\'' +
            ", address='" + address + '\'' +
            ", phone='" + phone + '\'' +
            '}';
  }
}

通用增删改查

package org.example.db;


import java.sql.*;

public class JDBCconn {
    public static Connection getConn() {
        try {
            // 四要素
            String url = "jdbc:mysql://localhost:3307/test";
            String user = "root";
            String password = "123456";
            String driverName = "com.mysql.cj.jdbc.Driver";
            //实例化驱动
            Class.forName(driverName);
            //获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void close(Connection conn, PreparedStatement ps) {
        try {
            if (conn != null) conn.close();
            if (ps != null) ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs){
        try {
            if(conn!=null) conn.close();
            if(ps!=null) ps.close();
            if(rs!=null) rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Main.java测试

package org.example;

import org.example.db.BaseCrud; 
import org.example.entity.User111;

public class Main {
    public static void main(String[] args) {
        System.out.println(BaseCrud.all(User111.class, "select id,name,password,address,phone from user111"));
    }
}

封装jdbc,实现通用增删改查_第4张图片

 

你可能感兴趣的:(java,数据库,java,mysql)