Druid简单实现数据库的增删改查方式

  package DruidExample;

  /*

  * 品牌

  * alt+鼠标左键整列编辑

  * 鼠标选中 ctrl+ r全选 直接替换全部

  *Alt + insert 生成构造方法,和tostring方法

  * 在实体类型中,基本数据类型建议使用对应包装类型

  * */

  public class Brand {

  //id 主键

  private int id;

  //品牌名称

  private String brand_name;

  //企业名称

  private String company_name;

  //排序字段

  private int ordered;

  //描述信息

  private String description;

  //状态:0:禁用 1:启用

  private int STATUS;

  public int getId() {

  return id;

  }

  public void setId(int id) {

  this.id = id;

  }

  public String getBrand_name() {

  return brand_name;

  }

  public void setBrand_name(String brand_name) {

  this.brand_name = brand_name;

  }

  public String getCompany_name() {

  return company_name;

  }

  public void setCompany_name(String company_name) {

  this.company_name = company_name;

  }

  public int getOrdered() {

  return ordered;

  }

  public void setOrdered(int ordered) {

  this.ordered = ordered;

  }

  public String getDescription() {

  return description;

  }

  public void setDescription(String description) {

  this.description = description;

  }

  public int getSTATUS() {

  return STATUS;

  }

  public void setSTATUS(int STATUS) {

  this.STATUS = STATUS;

  }

  @Override

  public String toString() {

  return "Brand{" +

  "id=" + id +

  ", brand_name='" + brand_name + ''' +

  ", company_name='" + company_name + ''' +

  ", ordered=" + ordered +

  ", description='" + description + ''' +

  ", STATUS=" + STATUS +

  '}';

  }

  }

  ```java

  package DruidExample;

  import com.alibaba.druid.pool.DruidDataSourceFactory;

  import org.junit.Test;

  import javax.sql.DataSource;

  import java.io.FileInputStream;

  import java.sql.Connection;

  import java.sql.PreparedStatement;

  import java.sql.ResultSet;

  import java.util.ArrayList;

  import java.util.List;

  import java.util.Properties;

  /*

  * 品牌数据的增删改查功能

  *1 获取Connection 连接

  *2 定义sql语句:select * from tb_brand; 变化项

  *3 获取PreparedStatement对象

  *4 设置参数:不需要 变化项

  *5 执行sql

  *6 处理结果:list 变化项

  *7 释放资源

  * */

  public class BrandTest {

  **查询所有相关信息**

  /*

  * 查询所有功能的分析

  * 1.sql:select * from tb_brand;

  * 2.参数:不需要

  * 3.结果:List

  * */

  @Test

  public void testSelectAll() throws Exception { //查询语句

  //1 获取连接的connection 对象

  // 加载配置文件

  Properties prop = new Properties();

  prop.load(new FileInputStream("src/druid.properties"));

  // 获取连接池对象

  DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

  //获取数据库链接 Connection

  Connection connection = dataSource.getConnection();

  System.out.println(connection);

  //2 定义sql语句

  String sql = "select * from tb_brand";

  //3 获取pstmt 对象

  PreparedStatement pstmt = connection.prepareStatement(sql);

  //4 设置参数

  //5 执行sql

  ResultSet rs = pstmt.executeQuery();

  //6 处理结果List封装Brand对象,装载到List集合当中去

  Brand brand = null;

  List brands = new ArrayList<>();

  while (rs.next()){

  //获取数据

  int id = rs.getInt("id");

  String brandName = rs.getString("brand_name");

  String companyName = rs.getString("company_name");

  int ordered = rs.getInt("ordered");

  String description = rs.getString("description");

  int status = rs.getInt("STATUS");

  //封装Brand对象

  brand = new Brand();

  brand.setId(id);

  brand.setBrand_name(brandName);

  brand.setCompany_name(companyName);

  brand.setOrdered(ordered);

  brand.setDescription(description);

  brand.setSTATUS(status);

  //装载集合

  brands.add(brand);

  }

  System.out.println(brands+"

  ");//打印集合查看效果

  }

  **向数据库添加相关操作**

  /*

  * 添加

  * 1 sql :insert into tb_brand(brand_name,company_name,ordered,descript,status) value(?,?,?,?,?);

  * 2 参数: 需要除iD之外的所有参数信息

  * 3 结果 :boolean

  * */

  @Test

  public void testAdd() throws Exception { //添加不需要id 由数据库主键自增自动生成

  //接收页面提交的参数 (模拟)

  String brandName = "8848钛晶手机";

  String companyName = "8848";

  int ordered = 1;

  String description = "成功人士的标配";

  int STATUS = 1;

  //1 获取连接的connection 对象

  // 加载配置文件

  Properties prop = new Properties();

  prop.load(new FileInputStream("src/druid.properties"));

  // 获取连接池对象

  DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

  //获取数据库链接 Connection

  Connection conn = dataSource.getConnection();

  System.out.println(conn);

  //2 定义sql语句

  String sql = "insert into tb_brand(brand_name,company_name,ordered,description,status) value(?,?,?,?,?);";

  //3 获取pstmt 对象

  PreparedStatement pstmt = conn.prepareStatement(sql);

  //4 设置参数

  pstmt.setString(1,brandName);

  pstmt.setString(2,companyName);

  pstmt.setInt(3,ordered);

  pstmt.setString(4,description);

  pstmt.setInt(5,STATUS);

  //5 执行sql

  int count = pstmt.executeUpdate();//影响行数

  //6 处理结果

  System.out.println(count>0);//输出结果是boolean类型的值

  //7 释放资源

  pstmt.close();

  conn.close();

  }

  **更新数据库信息**

  /*

  * 修改功能的实现 通过id进行修改

  * 1. sql : update tb_brand

  * set brand_naem = ?,

  * company_name = ?,

  * ordered = ?,

  * descript = ?,

  * STATUS = ?,

  * where id = ?

  *2.参数 : 需要 ,需要用到所有的数据

  *3.结果 :boolean类型

  * */

  @Test

  public void testUpdate() throws Exception { //添加不需要id 由数据库主键自增自动生成

  //接收页面提交的参数 (模拟)

  String brandName = "魅族经典";

  String companyName = "flyme你手中的信仰";

  int ordered = 1000;

  String description = "这是一种奢华,也是一种拥有";

  int status = 1;

  int id = 4;

  //1 获取连接的connection 对象

  // 加载配置文件

  Properties prop = new Properties();

  prop.load(new FileInputStream("src/druid.properties"));

  // 获取连接池对象

  DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

  //获取数据库链接 Connection

  Connection conn = dataSource.getConnection();

  System.out.println(conn);

  //2 定义sql语句

  String sql = "update tb_brand

  " +

  " set brand_name = ?,

  " +

  " company_name = ?,

  " +

  " ordered = ?,

  " +

  " description = ?,

  " +

  " status = ?

  " +

  " where id = ?;";

  //3 获取pstmt 对象

  PreparedStatement pstmt = conn.prepareStatement(sql);

  //4 设置参数

  pstmt.setString(1,brandName);

  pstmt.setString(2,companyName);

  pstmt.setInt(3,ordered);

  pstmt.setString(4,description);

  pstmt.setInt(5,status);

  pstmt.setInt(6,id);

  //5 执行sql

  int count = pstmt.executeUpdate();//影响行数

  //6 处理结果

  System.out.println(count>0);//输出结果是boolean类型的值

  //7 释放资源

  pstmt.close();

  conn.close();

  }

  **删除数据库相关信息**

  /*

  * 删除功能的实现 通过id进行删除

  * 1. sql : delete from tb_brand where id =4;

  *2.参数 : 需要 ,需要用到id参数

  *3.结果 :boolean类型

  * */

  @Test

  public void testDeleteId() throws Exception { //添加不需要id 由数据库主键自增自动生成

  //接收页面提交的参数 (模拟)

  int id = 3;

  //1 获取连接的connection 对象

  // 加载配置文件

  Properties prop = new Properties();

  prop.load(new FileInputStream("src/druid.properties"));

  // 获取连接池对象

  DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

  //获取数据库链接 Connection

  Connection conn = dataSource.getConnection();

  System.out.println(conn);

  //2 定义sql语句

  String sql = "delete from tb_brand where id =?";

  //3 获取pstmt 对象

  PreparedStatement pstmt = conn.prepareStatement(sql);

  //4 设置参数

  pstmt.setInt(1,id);

  //5 执行sql

  int count = pstmt.executeUpdate();//影响行数

  //6 处理结果

  System.out.println(count>0);//输出结果是boolean类型的值

  //7 释放资源

  pstmt.close();

  conn.close();

  }

  }