最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用c3p0連接數(shù)據(jù)庫實現(xiàn)增刪改查

 更新時間:2019年08月21日 10:44:55   作者:FBDriftz  
這篇文章主要為大家詳細介紹了使用c3p0連接數(shù)據(jù)庫實現(xiàn)增刪改查,具有一定的參考價值,感興趣的小伙伴們可以參考一下

學習spring之前,視頻中先給我們一個任務就是用c3p0連接數(shù)據(jù)庫來完成增刪改查

一、準備     

JAR包:

既然是連接數(shù)據(jù)庫第一個最重要的就是數(shù)據(jù)庫的驅動包mysql-connection-java-5.1.44-bin.jar      

接著就是C3P0-0.9.2.1.jar以及mchange-commons-java-0.2.3.4.jar

然后還少不了dbutils 使用的是commons-dbutils-1.7.jar   

一共是4個JAR包

二、配置 

配置數(shù)據(jù)庫連接:

創(chuàng)建c3p0-config.xml的配置文件,里面包含連接數(shù)據(jù)庫的信息

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
 <default-config>
 <property name="driverClass">com.mysql.jdbc.Driver</property>
 <property name="jdbcUrl">jdbc:mysql://localhost:3306/mybatis</property>
 <property name="user">root</property>
 <property name="password">123</property>
 <property name="acquireIncrement">5</property>
 <property name="initialPoolSize">10</property>
 <property name="minPoolSize">5</property>
 <property name="maxPoolSize">20</property>
 </default-config>
</c3p0-config>

創(chuàng)建C3P0Util.java 使用ComboPooledDataSource的對象獲取數(shù)據(jù)庫連接

package util;
 
import java.sql.Connection;
import java.sql.SQLException;
 
import javax.sql.DataSource;
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 
public class C3P0Util {
 
 private static ComboPooledDataSource ds=new ComboPooledDataSource();
 
 //獲取數(shù)據(jù)源
 public static DataSource getDataSource(){
 return ds;
 }
 //獲取一個連接
 public static Connection getConnection() throws SQLException{
 return ds.getConnection();
 }
}

由于沒有使用Mybatis的逆向工程,這里需要手動創(chuàng)建Customer.java 用于獲取數(shù)據(jù)庫表的所有列

package domain;
 
import java.io.Serializable;
 
public class Customer implements Serializable{
 
 private int cust_id;
 private String cust_name;
 private String cust_source;
 private String cust_industry;
 private String cust_level;
 private String cust_address;
 private String cust_phone;
 public int getCust_id() {
  return cust_id;
 }
 public void setCust_id(int cust_id) {
  this.cust_id = cust_id;
 }
 public String getCust_name() {
  return cust_name;
 }
 public void setCust_name(String cust_name) {
  this.cust_name = cust_name;
 }
 public String getCust_source() {
  return cust_source;
 }
 public void setCust_source(String cust_source) {
  this.cust_source = cust_source;
 }
 public String getCust_industry() {
  return cust_industry;
 }
 public void setCust_industry(String cust_industry) {
  this.cust_industry = cust_industry;
 }
 public String getCust_level() {
  return cust_level;
 }
 public void setCust_level(String cust_level) {
  this.cust_level = cust_level;
 }
 public String getCust_address() {
  return cust_address;
 }
 public void setCust_address(String cust_address) {
  this.cust_address = cust_address;
 }
 public String getCust_phone() {
  return cust_phone;
 }
 public void setCust_phone(String cust_phone) {
  this.cust_phone = cust_phone;
 }
 @Override
 public String toString() {
  return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name
   + ", cust_source=" + cust_source + ", cust_industry="
   + cust_industry + ", cust_level=" + cust_level
   + ", cust_address=" + cust_address + ", cust_phone="
   + cust_phone + "]";
 } 
}

三、接口

上面的配置文件和數(shù)據(jù)庫表信息文件都已經(jīng)寫好了,現(xiàn)在需要寫兩個接口提供所有的增刪改查的方法

創(chuàng)建ICustomerDao.java 持久層接口 ,也就是最底層和數(shù)據(jù)庫連接的接口類

package dao;
 
import java.util.List; 
import domain.Customer; 
public interface ICustomerDao {
 
 List<Customer> findAllCustomer(); 
 void saveCustomer(Customer customer); 
 void updateCustomer(Customer customer); 
 void deleteCustomer(int custId); 
 Customer findCustomerById(int custId);
 
}

創(chuàng)建ICustomerService.java 業(yè)務層接口,用于持久層和客戶端連接的接口類,和Dao的方法一樣

package service;
 
import java.util.List;
 
import domain.Customer;
 
public interface ICustomerService {
 
 //查詢所有客戶
 List<Customer> findAllCustomer();
 //保存客戶信息
 void saveCustomer(Customer customer);
 //更改客戶信息
 void updateCustomer(Customer customer);
 //根據(jù)Id刪除對象
 void deleteCustomer(int cust_id);
 //根據(jù)ID查詢用戶,返回用戶信息
 Customer findCustomerById(int cust_id);
}

四、實現(xiàn)類

創(chuàng)建CustomerDao.java 實現(xiàn)Dao接口,利用C3P0里的QueryRunner類獲取到數(shù)據(jù)庫連接信息連接數(shù)據(jù)庫

并將SQL語句傳給數(shù)據(jù)庫然后得到SQL的返回值。

package dao.impl;
 
import java.sql.SQLException;
import java.util.List;
 
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
 
import dao.ICustomerDao;
import domain.Customer;
import util.C3P0Util;
 
//客戶的持久層實現(xiàn)類
public class CustomerDao implements ICustomerDao {
 
 private QueryRunner runner=new QueryRunner(C3P0Util.getDataSource());
 @Override
 public List<Customer> findAllCustomer() {
 try {
  return runner.query("select * from cst_customer", new BeanListHandler<Customer>(Customer.class));
 } catch (SQLException e) {
  throw new RuntimeException(e);
 }
 }
 
 @Override
 public void saveCustomer(Customer customer) {
 try {
  runner.update("insert into cst_customer(cust_name,cust_source,cust_industry,cust_level,cust_address,cust_phone)values(?,?,?,?,?,?)",
   customer.getCust_name(),customer.getCust_source(),customer.getCust_industry(),
   customer.getCust_level(),customer.getCust_address(),customer.getCust_phone());
 } catch (SQLException e) {
  throw new RuntimeException(e);
 }
 
 }
 
 @Override
 public void updateCustomer(Customer customer) {
 try {
  runner.update("update cst_customer set cust_name=?,cust_source=?,cust_industry=?,cust_level=?,cust_address=?,cust_phone=? where cust_id=?",
   customer.getCust_name(),customer.getCust_source(),customer.getCust_industry(),
   customer.getCust_level(),customer.getCust_address(),customer.getCust_phone(),customer.getCust_id());
 } catch (SQLException e) {
  throw new RuntimeException(e);
 }
 }
 
 @Override
 public void deleteCustomer(int custId) {
 try {
  runner.update("delete from cst_customer where cust_id=?",custId);
 } catch (SQLException e) {
  throw new RuntimeException(e);
 }
 }
 
 @Override
 public Customer findCustomerById(int custId) {
 try {
  return runner.query("select * from cst_customer where cust_id=?", new BeanHandler<Customer>(Customer.class),custId);
 } catch (SQLException e) {
  throw new RuntimeException(e);
 }
 }
 
}

創(chuàng)建CustomerService.java 實現(xiàn)業(yè)務層,將需要查詢的數(shù)據(jù)傳給Dao層,并得到Dao層的返回值。

package service;
 
import java.util.List;
 
import dao.ICustomerDao;
import dao.impl.CustomerDao;
import domain.Customer;
 
//客戶的業(yè)務層實現(xiàn)類
public class CustomerServiceImpl implements ICustomerService {
 
 private ICustomerDao customerDao=new CustomerDao();
 @Override
 public List<Customer> findAllCustomer() {
 // TODO Auto-generated method stub
 return customerDao.findAllCustomer();
 }
 
 @Override
 public void saveCustomer(Customer customer) {
 customerDao.saveCustomer(customer);
 
 }
 
 @Override
 public void updateCustomer(Customer customer) {
 customerDao.updateCustomer(customer);
 
 }
 
 @Override
 public void deleteCustomer(int custId) {
 customerDao.deleteCustomer(custId);
 }
 
 @Override
 public Customer findCustomerById(int custId) {
 // TODO Auto-generated method stub
 return customerDao.findCustomerById(custId);
 }
 
}

五、測試類

創(chuàng)建CustomerServiceTest.java 獲取CustomerServiceImpl所有方法的測試方法

該類中在方法里創(chuàng)建CustomerServiceImpl對象 獲取原始方法,將參數(shù)傳入Customer中,通過CustomerService獲取到返回值并打印。    

package test;
 
import static org.junit.Assert.fail;
 
import java.util.List;
 
import org.junit.Test;
 
import domain.Customer;
import service.CustomerServiceImpl;
import service.ICustomerService;
 
public class CustomerServicerTest {
 
 @Test
 public void testFindAllCustomer() {
 ICustomerService cs=new CustomerServiceImpl();
 List<Customer> list=cs.findAllCustomer();
 for(Customer c: list){
  System.out.println(c);
 }
 }
 
 @Test
 public void testSaveCustomer() {
 ICustomerService cs=new CustomerServiceImpl();
 Customer c=new Customer();
 c.setCust_name("滴滴");
 c.setCust_source("dache");
 
 cs.saveCustomer(c);
 }
 
 @Test
 public void testUpdateCustomer() {
 fail("Not yet implemented");
 }
 
 @Test
 public void testDeleteCustomer() {
 fail("Not yet implemented");
 }
 
 @Test
 public void testFindCustomerById() {
 ICustomerService cs=new CustomerServiceImpl();
 Customer c=cs.findCustomerById(2);
 System.out.println(c);
 }
 
}

至此C3P0連接數(shù)據(jù)庫進行單表增刪改查功能完結。附上結構圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 使用SSM+Layui+Bootstrap實現(xiàn)汽車維保系統(tǒng)的示例代碼

    使用SSM+Layui+Bootstrap實現(xiàn)汽車維保系統(tǒng)的示例代碼

    本文主要實現(xiàn)對汽車維修廠的信息化管理功能,。實現(xiàn)的主要功能包含用戶管理、配置管理、汽車管理、故障管理、供應商管理、配件管理、維修訂單管理、統(tǒng)計信息、公告管理、個人信息管理,感興趣的可以了解一下
    2021-12-12
  • Java8 自定義CompletableFuture的原理解析

    Java8 自定義CompletableFuture的原理解析

    這篇文章主要介紹了Java8 自定義CompletableFuture的原理解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot項目中建議關閉Open-EntityManager-in-view原因

    SpringBoot項目中建議關閉Open-EntityManager-in-view原因

    這篇文章主要為大家解析了在Spring Boot項目中建議關閉Open-EntityManager-in-view的原因示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-02-02
  • Java spring的三種注入方式詳解流程

    Java spring的三種注入方式詳解流程

    Spring框架由Rod Johnson開發(fā),2004年發(fā)布了Spring框架的第一版。Spring是一個從實際開發(fā)中抽取出來的框架,因此它完成了大量開發(fā)中的通用步驟,留給開發(fā)者的僅僅是與特定應用相關的部分,從而大大提高了企業(yè)應用的開發(fā)效率
    2021-10-10
  • Java TreeSet類的簡單理解和使用

    Java TreeSet類的簡單理解和使用

    這篇文章主要介紹了Java TreeSet類的簡單理解和使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Java?API操作Hdfs的示例詳解

    Java?API操作Hdfs的示例詳解

    這篇文章主要介紹了Java?API操作Hdfs詳細示例,遍歷當前目錄下所有文件與文件夾,可以使用listStatus方法實現(xiàn)上述需求,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • Mybatis核心配置文件、默認類型別名、Mybatis獲取參數(shù)值的兩種方式(實例代碼)

    Mybatis核心配置文件、默認類型別名、Mybatis獲取參數(shù)值的兩種方式(實例代碼)

    這篇文章主要介紹了Mybatis核心配置文件、默認類型別名、Mybatis獲取參數(shù)值的兩種方式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-03-03
  • Java調度線程池ScheduledThreadPoolExecutor不執(zhí)行問題分析

    Java調度線程池ScheduledThreadPoolExecutor不執(zhí)行問題分析

    最近項目上反饋某個重要的定時任務突然不執(zhí)行了,很頭疼,開發(fā)環(huán)境和測試環(huán)境都沒有出現(xiàn)過這個問題。定時任務采用的是ScheduledThreadPoolExecutor,后來一看代碼發(fā)現(xiàn)踩了一個大坑。本文就來和大家聊聊這次的踩坑記錄與解決方法,需要的可以參考一下
    2023-03-03
  • JDK8的lambda方式List轉Map的操作方法

    JDK8的lambda方式List轉Map的操作方法

    account是一個返回本身的lambda表達式,其實還可以使用Function接口中的一個默認方法代替,使整個方法更簡潔優(yōu)雅,這篇文章主要介紹了JDK8的lambda方式List轉Map,需要的朋友可以參考下
    2022-07-07
  • Java數(shù)據(jù)庫連接池之DBCP淺析_動力節(jié)點Java學院整理

    Java數(shù)據(jù)庫連接池之DBCP淺析_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了Java數(shù)據(jù)庫連接池之DBCP的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論

黄冈市| 新泰市| 绥棱县| 锡林郭勒盟| 巴彦县| 巴东县| 海口市| 普兰店市| 镇安县| 太原市| 双牌县| 耿马| 山丹县| 吉安市| 十堰市| 公主岭市| 比如县| 溧水县| 台安县| 泰和县| 兖州市| 长沙市| 安多县| 西盟| 航空| 田林县| 方城县| 宁城县| 南昌市| 晋城| 光山县| 亚东县| 鲁甸县| 峡江县| 涞水县| 博湖县| 浦北县| 无极县| 贵定县| 海兴县| 华宁县|