Druid簡單實現(xiàn)數(shù)據(jù)庫的增刪改查方式
druid的使用步驟
- 1:導(dǎo)入相關(guān)的druid-jar包
- 2:定義配置文件
- 3:加載配置文件
- 4:獲取數(shù)據(jù)庫連接池對象
- 5:獲取鏈接
具體實現(xiàn)
相關(guān)包可以網(wǎng)上下載哦::白/嫖/隊
將jar包放到lib文件下:點擊選擇 add as Library;
創(chuàng)建一個Java文件,獲取連接池與數(shù)據(jù)庫的隊應(yīng)匹配
注:特別注意導(dǎo)入文件地址的問題發(fā)生,大多報錯都是地址不匹配
package DruidExample; /* * 品牌 * alt+鼠標左鍵整列編輯 * 鼠標選中 ctrl+ r全選 直接替換全部 *Alt + insert 生成構(gòu)造方法,和tostring方法 * 在實體類型中,基本數(shù)據(jù)類型建議使用對應(yīng)包裝類型 * */ public class Brand { //id 主鍵 private int id; //品牌名稱 private String brand_name; //企業(yè)名稱 private String company_name; //排序字段 private int ordered; //描述信息 private String description; //狀態(tài):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 + '}'; } }
將其中的文件進行編輯更改:druid.properties
druid.properties 配置更改
url=jdbc:mysql://127.0.0.1:3307/mydb?rewriteBatchedStatements=true username=root password=root driverClassName=com.mysql.cj.jdbc.Driver initialSize=10 maxActive=20 maxWait=1000 filters=wall
修改里面的相關(guān)內(nèi)容與之匹配:
創(chuàng)建Java文件,生成構(gòu)造方法和toString方法;
package DruidExample; /* * 品牌 * alt+鼠標左鍵整列編輯 * 鼠標選中 ctrl+ r全選 直接替換全部 *Alt + insert 生成構(gòu)造方法,和tostring方法 * 在實體類型中,基本數(shù)據(jù)類型建議使用對應(yīng)包裝類型 * */ public class Brand { //id 主鍵 private int id; //品牌名稱 private String brand_name; //企業(yè)名稱 private String company_name; //排序字段 private int ordered; //描述信息 private String description; //狀態(tài):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; /* * 品牌數(shù)據(jù)的增刪改查功能 *1 獲取Connection 連接 *2 定義sql語句:select * from tb_brand; 變化項 *3 獲取PreparedStatement對象 *4 設(shè)置參數(shù):不需要 變化項 *5 執(zhí)行sql *6 處理結(jié)果:list<Brand> 變化項 *7 釋放資源 * */ public class BrandTest { **查詢所有相關(guān)信息** /* * 查詢所有功能的分析 * 1.sql:select * from tb_brand; * 2.參數(shù):不需要 * 3.結(jié)果:List<Brand> * */ @Test public void testSelectAll() throws Exception { //查詢語句 //1 獲取連接的connection 對象 // 加載配置文件 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); // 獲取連接池對象 DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //獲取數(shù)據(jù)庫鏈接 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 設(shè)置參數(shù) //5 執(zhí)行sql ResultSet rs = pstmt.executeQuery(); //6 處理結(jié)果List<Brand>封裝Brand對象,裝載到List集合當中去 Brand brand = null; List<Brand> brands = new ArrayList<>(); while (rs.next()){ //獲取數(shù)據(jù) 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+"\n");//打印集合查看效果 } **向數(shù)據(jù)庫添加相關(guān)操作** /* * 添加 * 1 sql :insert into tb_brand(brand_name,company_name,ordered,descript,status) value(?,?,?,?,?); * 2 參數(shù): 需要除iD之外的所有參數(shù)信息 * 3 結(jié)果 :boolean * */ @Test public void testAdd() throws Exception { //添加不需要id 由數(shù)據(jù)庫主鍵自增自動生成 //接收頁面提交的參數(shù) (模擬) 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); //獲取數(shù)據(jù)庫鏈接 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 設(shè)置參數(shù) pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setInt(3,ordered); pstmt.setString(4,description); pstmt.setInt(5,STATUS); //5 執(zhí)行sql int count = pstmt.executeUpdate();//影響行數(shù) //6 處理結(jié)果 System.out.println(count>0);//輸出結(jié)果是boolean類型的值 //7 釋放資源 pstmt.close(); conn.close(); } **更新數(shù)據(jù)庫信息** /* * 修改功能的實現(xiàn) 通過id進行修改 * 1. sql : update tb_brand * set brand_naem = ?, * company_name = ?, * ordered = ?, * descript = ?, * STATUS = ?, * where id = ? *2.參數(shù) : 需要 ,需要用到所有的數(shù)據(jù) *3.結(jié)果 :boolean類型 * */ @Test public void testUpdate() throws Exception { //添加不需要id 由數(shù)據(jù)庫主鍵自增自動生成 //接收頁面提交的參數(shù) (模擬) String brandName = "魅族經(jīng)典"; 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); //獲取數(shù)據(jù)庫鏈接 Connection Connection conn = dataSource.getConnection(); System.out.println(conn); //2 定義sql語句 String sql = "update tb_brand\n" + " set brand_name = ?,\n" + " company_name = ?,\n" + " ordered = ?,\n" + " description = ?,\n" + " status = ?\n" + " where id = ?;"; //3 獲取pstmt 對象 PreparedStatement pstmt = conn.prepareStatement(sql); //4 設(shè)置參數(shù) 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 執(zhí)行sql int count = pstmt.executeUpdate();//影響行數(shù) //6 處理結(jié)果 System.out.println(count>0);//輸出結(jié)果是boolean類型的值 //7 釋放資源 pstmt.close(); conn.close(); } **刪除數(shù)據(jù)庫相關(guān)信息** /* * 刪除功能的實現(xiàn) 通過id進行刪除 * 1. sql : delete from tb_brand where id =4; *2.參數(shù) : 需要 ,需要用到id參數(shù) *3.結(jié)果 :boolean類型 * */ @Test public void testDeleteId() throws Exception { //添加不需要id 由數(shù)據(jù)庫主鍵自增自動生成 //接收頁面提交的參數(shù) (模擬) int id = 3; //1 獲取連接的connection 對象 // 加載配置文件 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); // 獲取連接池對象 DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //獲取數(shù)據(jù)庫鏈接 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 設(shè)置參數(shù) pstmt.setInt(1,id); //5 執(zhí)行sql int count = pstmt.executeUpdate();//影響行數(shù) //6 處理結(jié)果 System.out.println(count>0);//輸出結(jié)果是boolean類型的值 //7 釋放資源 pstmt.close(); conn.close(); } }
用于測試的數(shù)據(jù)庫數(shù)據(jù)
use mydb; CREATE table tb_brand( id int PRIMARY KEY auto_increment, brand_name VARCHAR(20), company_name VARCHAR(20), ordered int, description VARCHAR(100), STATUS int ); --添加數(shù)據(jù) insert into tb_brand(brand_name,company_name,ordered,description,status) VALUE('蘋果果','蘋果果技術(shù)有限公司',500,'吃個蘋果果解解渴',0), ('華為為','華為為技術(shù)有限公司',300,'華為為YYDS',1), ('小米米','小米米科技有限公司',200,'今天你發(fā)燒了嗎!',1); select * from tb_brand; UPDATE tb_brand set ordered = 200 where id=3;
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用springMVC通過Filter實現(xiàn)防止xss注入
這篇文章主要介紹了使用springMVC通過Filter實現(xiàn)防止xss注入的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07SpringCloudAlibaba整合Feign實現(xiàn)遠程HTTP調(diào)用的簡單示例
這篇文章主要介紹了SpringCloudAlibaba 整合 Feign 實現(xiàn)遠程 HTTP 調(diào)用,文章中使用的是OpenFeign,是Spring社區(qū)開發(fā)的組件,需要的朋友可以參考下2021-09-09解決lambda表達式內(nèi)出現(xiàn)異常無法throw拋出的問題
這篇文章主要介紹了lambda表達式內(nèi)出現(xiàn)異常無法throw拋出的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot獲取Request和Response方法代碼解析
這篇文章主要介紹了SpringBoot獲取Request和Response方法代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11