欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

學(xué)習(xí)spring之前,視頻中先給我們一個(gè)任務(wù)就是用c3p0連接數(shù)據(jù)庫(kù)來(lái)完成增刪改查

一、準(zhǔn)備     

JAR包:

既然是連接數(shù)據(jù)庫(kù)第一個(gè)最重要的就是數(shù)據(jù)庫(kù)的驅(qū)動(dòng)包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個(gè)JAR包

二、配置 

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

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

<?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的對(duì)象獲取數(shù)據(jù)庫(kù)連接

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;
 }
 //獲取一個(gè)連接
 public static Connection getConnection() throws SQLException{
 return ds.getConnection();
 }
}

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

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ù)庫(kù)表信息文件都已經(jīng)寫(xiě)好了,現(xiàn)在需要寫(xiě)兩個(gè)接口提供所有的增刪改查的方法

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

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è)務(wù)層接口,用于持久層和客戶(hù)端連接的接口類(lèi),和Dao的方法一樣

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

四、實(shí)現(xiàn)類(lèi)

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

并將SQL語(yǔ)句傳給數(shù)據(jù)庫(kù)然后得到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;
 
//客戶(hù)的持久層實(shí)現(xiàn)類(lèi)
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 實(shí)現(xiàn)業(yè)務(wù)層,將需要查詢(xún)的數(shù)據(jù)傳給Dao層,并得到Dao層的返回值。

package service;
 
import java.util.List;
 
import dao.ICustomerDao;
import dao.impl.CustomerDao;
import domain.Customer;
 
//客戶(hù)的業(yè)務(wù)層實(shí)現(xiàn)類(lèi)
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);
 }
 
}

五、測(cè)試類(lèi)

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

該類(lèi)中在方法里創(chuàng)建CustomerServiceImpl對(duì)象 獲取原始方法,將參數(shù)傳入Customer中,通過(guò)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ù)庫(kù)進(jìn)行單表增刪改查功能完結(jié)。附上結(jié)構(gòu)圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Java代碼實(shí)現(xiàn)支付充值的通用流程

    基于Java代碼實(shí)現(xiàn)支付充值的通用流程

    本文給大家分享一段java核心代碼實(shí)現(xiàn)支付充值的通用流程,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-05-05
  • JAVA 對(duì)50取余數(shù)的五種方法試下

    JAVA 對(duì)50取余數(shù)的五種方法試下

    在數(shù)學(xué)計(jì)算中經(jīng)常會(huì)遇到余數(shù),本文主要介紹了JAVA 對(duì)50取余數(shù)的五種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式

    SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式

    這篇文章主要介紹了SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 關(guān)于JVM默認(rèn)堆內(nèi)存大小問(wèn)題

    關(guān)于JVM默認(rèn)堆內(nèi)存大小問(wèn)題

    這篇文章主要介紹了關(guān)于JVM默認(rèn)堆內(nèi)存大小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Mybatis-plus原生pages分頁(yè)未生效的解決方案

    Mybatis-plus原生pages分頁(yè)未生效的解決方案

    本文主要介紹了Mybatis-plus原生pages分頁(yè)未生效的解決方案,包含介紹了未生效的5種原因以及解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • mybatis中oracle實(shí)現(xiàn)分頁(yè)效果實(shí)例代碼

    mybatis中oracle實(shí)現(xiàn)分頁(yè)效果實(shí)例代碼

    實(shí)現(xiàn)分頁(yè)的方式有很多,但常用的是通過(guò)SQL來(lái)顯示分頁(yè)。這篇文章主要介紹了mybatis中oracle實(shí)現(xiàn)分頁(yè)效果實(shí)例代碼,有興趣的可以了解一下。
    2017-04-04
  • java 類(lèi)加載與自定義類(lèi)加載器詳解

    java 類(lèi)加載與自定義類(lèi)加載器詳解

    本文主要介紹了java 類(lèi)加載與自定義類(lèi)加載器。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Spring Boot與Docker部署實(shí)踐

    Spring Boot與Docker部署實(shí)踐

    這篇文章主要介紹了Spring Boot與Docker部署實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • java使用鏈表實(shí)現(xiàn)約瑟夫環(huán)

    java使用鏈表實(shí)現(xiàn)約瑟夫環(huán)

    這篇文章主要為大家詳細(xì)介紹了java使用鏈表實(shí)現(xiàn)約瑟夫環(huán),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • SpringBoot項(xiàng)目中使用緩存Cache的正確方法分享

    SpringBoot項(xiàng)目中使用緩存Cache的正確方法分享

    緩存可以通過(guò)將經(jīng)常訪(fǎng)問(wèn)的數(shù)據(jù)存儲(chǔ)在內(nèi)存中,減少底層數(shù)據(jù)源如數(shù)據(jù)庫(kù)的壓力,從而有效提高系統(tǒng)的性能和穩(wěn)定性。本文就來(lái)講講SpringBoot項(xiàng)目中使用緩存Cache的正確姿勢(shì)吧
    2023-04-04

最新評(píng)論