目录
环境准备(数据库表tb_brand)
环境准备(实体类Brand)
测试
查询所有数据
添加功能
修改信息
删除数据
细节
需要的jar包
druid.properties
DruidDemo.java
环境准备(数据库表tb_brand)
CREATE table tb_brand ( -- id 主表 id int primary key auto_increment, -- 品牌名称 brand_name varchar(20), -- 企业名称 company_name varchar(20), -- 排序字段 ordered int, -- 描述信息 description varchar(100), -- 状态:0:禁用1:启用 status int ); -- 添加数据 INSERT into tb_brand (brand_name,company_name,ordered,description,status) VALUES('三只松鼠','三只松鼠股份有限公司',5,'好吃不上火',0), ('华为','华为科技有限公司',100,'华为致力于把数字世界带入每个人、每个家庭、每个组织、构建万物互联的智能世界',1), ('小米','小米科技有限公司',50,'are you ok',1); SELECt * FROM tb_brand;
注:创建表时,最后一个不加逗号
环境准备(实体类Brand)package com.chunmei.pojo; public class Brand { public String getBrandName() { return brandName; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getCompanyName() { return companyName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Integer getOrdered() { return ordered; } public void setOrdered(Integer ordered) { this.ordered = ordered; } public void setCompanyName(String companyName) { this.companyName = companyName; } public void setBrandName(String brandName) { this.brandName = brandName; } //id 主键 private Integer id ; //品牌名称 private String brandName ; // 企业名称 private String companyName ; // 排序字段 private Integer ordered ; // 描述信息 private String description ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } // 状态:0:禁用1:启用 private Integer status ; @Override public String toString() { return "Brand{" + "id=" + id + ", brandName='" + brandName + ''' + ", companyName='" + companyName + ''' + ", ordered=" + ordered + ", description='" + description + ''' + ", status=" + status + '}'; } }
注:getting setting 方法& toString 方法
测试 查询所有数据1.获取Connection
2.定义SQL:
3.获取PreparedStatement对象
4.设置参数:不需要
5.执行SQL
6.处理结果:List
7.释放资源
package com.chunmei.example; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.chunmei.pojo.Brand; import org.junit.Test; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Properties; //品牌数据增改改查 public class BrandTest { @Test public void testSelectAll() throws Exception { //1.获取Connection //3.加载配置文件 Properties prop=new Properties(); prop.load(new FileInputStream("E:/cm/jdbc/jdbc_demo/src/druid.properties")); //4.获取连接池对象 DataSource dataSource= DruidDataSourceFactory.createDataSource(prop); //5.获取数据库连接Connection Connection con=dataSource.getConnection(); //2.定义sql语句 String sql="select * from tb_brand;"; //3.获取pstmt对象 PreparedStatement pstmt = con.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.setBrandName(brandName); brand.setCompanyName(companyName); brand.setOrdered(ordered); brand.setDescription(description); brand.setStatus(status); //装在集合 brands.add(brand); } System.out.println(brands); //7.释放资源 rs.close(); con.close(); pstmt.close(); } }
运行结果
添加功能1.sql
2.参数需要,除了id之外的所有参数信息
3.结果:Boolean
package com.chunmei.example; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.chunmei.pojo.Brand; import org.junit.Test; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Properties; //品牌数据添加 public class BrandTest { @Test public void testSelectAll() throws Exception { //接受页面提交的参数 String brandName="小黄鸭"; String companyName="yaya公司"; int ordered=1; String description="可爱鸭"; int status=1; //1.获取Connection //3.加载配置文件 Properties prop=new Properties(); prop.load(new FileInputStream("E:/cm/jdbc/jdbc_demo/src/druid.properties")); //4.获取连接池对象 DataSource dataSource= DruidDataSourceFactory.createDataSource(prop); //5.获取数据库连接Connection Connection con=dataSource.getConnection(); //2.定义sql语句 String sql="INSERT into tb_brand (brand_name,company_name,ordered,description,status)value (?,?,?,?,?);"; //3.获取pstmt对象 PreparedStatement pstmt = con.prepareStatement(sql); //4.设置参数 pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setInt(3,ordered); pstmt.setString(4,brandName); pstmt.setInt(5,status); //5.执行sql int count = pstmt.executeUpdate();//影响行数 //6.处理结果 System.out.println(count>0); //7.释放资源 con.close(); pstmt.close(); } }
结果
修改信息
1.sql
2.参数需要,除了id之外的所有参数信息
3.结果:Boolean
package com.chunmei.example; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.chunmei.pojo.Brand; import org.junit.Test; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Properties; //品牌数据修改 public class BrandTest { @Test public void testSelectAll() throws Exception { //接受页面提交的参数 String brandName="小米"; String companyName="小米科技有限公司"; int ordered=1000; String description="are you okk"; int status=1; int id=3; //1.获取Connection //3.加载配置文件 Properties prop=new Properties(); prop.load(new FileInputStream("E:/cm/jdbc/jdbc_demo/src/druid.properties")); //4.获取连接池对象 DataSource dataSource= DruidDataSourceFactory.createDataSource(prop); //5.获取数据库连接Connection Connection con=dataSource.getConnection(); //2.定义sql语句 String sql="update tb_brandn"+ "set brand_name=?,n" + "company_name=?,n" + "ordered=?,n" + "description=?,n" + "status=?n"+ "where id=?"; //3.获取pstmt对象 PreparedStatement pstmt = con.prepareStatement(sql); //4.设置参数 pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setInt(3,ordered); pstmt.setString(4,brandName); pstmt.setInt(5,status); pstmt.setInt(6,id); //5.执行sql int count = pstmt.executeUpdate();//影响行数 //6.处理结果 System.out.println(count>0); //7.释放资源 con.close(); pstmt.close(); } }删除数据
1.sql
2.参数需要,需要id
3.结果:Boolean
package com.chunmei.example; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.chunmei.pojo.Brand; import org.junit.Test; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Properties; //品牌数据删除 public class BrandTest { @Test public void testSelectAll() throws Exception { //接受页面提交的参数 int id=4; //1.获取Connection //3.加载配置文件 Properties prop=new Properties(); prop.load(new FileInputStream("E:/cm/jdbc/jdbc_demo/src/druid.properties")); //4.获取连接池对象 DataSource dataSource= DruidDataSourceFactory.createDataSource(prop); //5.获取数据库连接Connection Connection con=dataSource.getConnection(); //2.定义sql语句 String sql="delete from tb_brand where id=?"; //3.获取pstmt对象 PreparedStatement pstmt = con.prepareStatement(sql); //4.设置参数 pstmt.setInt(1,id); //5.执行sql int count = pstmt.executeUpdate();//影响行数 //6.处理结果 System.out.println(count>0); //7.释放资源 con.close(); pstmt.close(); } }细节 需要的jar包
链接:https://pan.baidu.com/s/1UPdVp9QXWyfXu4XfHaKcdg 提取码:iqx6
druid.properties# druid.properties文件的配置 driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/dorm?useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true username=root password=123456 # 初始化连接数量 initialSize=5 # 最大连接数 maxActive=10 # 最大超时时间 maxWait=3000DruidDemo.java
package com.chunmei.druid; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileInputStream; import java.sql.Connection; import java.util.Properties; import java.util.PropertyPermission; public class DruidDemo { public static void main(String[] args) throws Exception { Properties prop=new Properties(); prop.load(new FileInputStream("jdbc_demo/src/druid.properties")); //4.获取连接池对象 DataSource dataSource=DruidDataSourceFactory.createDataSource(prop); //5.获取数据库连接Connection Connection connection=dataSource.getConnection(); System.out.println(connection); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)