栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021.12.30C3P0-Mysql简单实现学习记录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021.12.30C3P0-Mysql简单实现学习记录

c3p0-config.xml


    
    
        10
        30
        100
        10
        200
    

    
    
        com.mysql.jdbc.Driver
        jdbc:mysql://192.168.111.131:3306/mybatisdb
        root
        root
        10
        30
        100
        10
        200
    

    
    
        com.mysql.jdbc.Driver
        jdbc:mysql://192.168.111.131:3306/mybatisdb
        root
        root
        10
        30
        100
        10
        200
    
mydb.properties 
mysqldriver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.111.131:3306/mybatisdb
user=root
password=root
initNum=101
min=31
max=151
increase=32
pom.xml



  4.0.0

  nj.zb.cn.kgc
  jdbcpooldemo
  1.0-SNAPSHOT

  jdbcpooldemo
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
      com.mchange
      c3p0
      0.9.5.4
    
    
      mysql
      mysql-connector-java
      5.1.25
    
  

  
    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  

 C3p0Utils
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class C3p0Utils {

    static ComboPooledDataSource source = new ComboPooledDataSource("linux01mysql");

    public static Connection getConnection() {

        Connection connection = null;

        try {
            connection = source.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }

    public static void close(Connection connection, PreparedStatement pstmt, ResultSet rs){
        if(rs!=null){

            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        if(pstmt!=null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }



    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            Connection connection = C3p0Utils.getConnection();

            System.out.println(connection);
        }

    }
}
 Student
public class Student {

    private Integer id;
    private String name;

    public Student() {
    }

    public Student(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
StudentDao
import java.util.List;

public interface StudentDao {
    public void insertStudent(Liststudents);
}
 StudentDaoImpl
import nj.zb.cn.kgc.mypool.MyPoolUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public void insertStudent(List students) {
//   insert into Student(id,name) values(1,'zs'),(2,'ls'),(3,'ww')
        String sql = "insert into Student(id,name) values";
        for (Student stu :
                students) {
            sql += "(" +stu.getId()+","+stu.getName()+ "),";
        }
//   insert into Student(id,name) values(1,'zs'),(2,'ls'),(3,'ww'),
        sql = sql.substring(0,sql.length()-1);

        Connection conn = C3p0Utils.getConnection();
//        Connection conn = MyPoolUtils.getConnection();

        try {
            PreparedStatement prest = conn.prepareStatement(sql);
            System.out.println(sql);
//            int i = prest.executeUpdate();
            C3p0Utils.close(conn,prest,null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List list = new ArrayList<>();
        Student zs = new Student(1, "zs");
        Student ls = new Student(2, "ls");
        list.add(zs);
        list.add(ls);

        StudentDao  studentDao = new StudentDaoImpl();
        studentDao.insertStudent(list);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号