Data
Stay hungry,Stay foolish!

导航

 

目录:

package com.it.common;

import com.it.utils.Account;
import com.itheima.uitl.JDBCUtil02;
import org.junit.Test;

import javax.sql.rowset.JdbcRowSet;
import java.sql.*;

public class CommonCRUDUtil {

    class A implements ResultHandler<Account>{
        @Override
        public Account handler(ResultSet resultSet) {
            try {
                while (resultSet.next()){
                    Account account = new Account();
                    String name = resultSet.getString("name");
                    int money = resultSet.getInt("money");
                    account.setMoney(money);
                    account.setName(name);
                    return account;
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
            return null;
        }
    }

    //通用的查方法
    @Test
    public void testFindall(){
        Account account = findall("select * from account where id=?",new A(),6);

        System.out.println(account.toString());
    }

    public <T> T findall(String sql, ResultHandler<T> handler, Object...args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil02.getConn();
            ps = conn.prepareStatement(sql);
            ParameterMetaData metaData = ps.getParameterMetaData();
            int count = metaData.getParameterCount();
            for (int i = 0; i < count; i++) {
                ps.setObject(i+1, args[i]);
            }
            // 查询得到结果集
            ResultSet resultSet = ps.executeQuery();
            // 把得到的结果集交给调用者,让它去封装数据
            T t = (T) handler.handler(resultSet);
            return t;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil02.release(conn, ps);
        }
        return null;
    }





    // 通用的增删改方法
    @Test
    public void testUpdate(){
        updata02("insert into account values(?,?,?)",8,6000,"womengl");
    }

    public void update(String sql, Object...args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil02.getConn();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil02.release(conn, ps);
        }
    }


    public void updata02(String sql, Object ... args){
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = JDBCUtil02.getConn();
            ps = conn.prepareStatement(sql);

            // 获取参数源数据对象
            ParameterMetaData metaData = ps.getParameterMetaData();
            // 获取参数的总数
            int count = metaData.getParameterCount();
            for (int i = 0; i < count; i++) {
                ps.setObject(i+1, args[i]);
            }
            ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil02.release(conn, ps);
        }


    }
}
package com.it.common;

import java.sql.ResultSet;

public interface ResultHandler<T> {
    // 定义了一个数据封装的规则,或者是规范
    T handler(ResultSet resultSet);
}

package com.itheima.uitl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtil02 {

    static ComboPooledDataSource dataSource = null;

    static{
        dataSource = new ComboPooledDataSource();
    }

//    public static DataSource getDataSource(){
//        return dataSource;
//    }

    /**
     * 获取连接对象
     * @return
     * @throws SQLException
     */
    public static Connection getConn() throws SQLException{
        return dataSource.getConnection();
    }

    /**
     * 释放资源
     * @param conn
     * @param st
     * @param rs
     */
    public static void release(Connection conn , Statement st , ResultSet rs){
        closeRs(rs);
        closeSt(st);
        closeConn(conn);
    }
    public static void release(Connection conn , Statement st){
        closeSt(st);
        closeConn(conn);
    }


    private static void closeRs(ResultSet rs){
        try {
            if(rs != null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            rs = null;
        }
    }

    private static void closeSt(Statement st){
        try {
            if(st != null){
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            st = null;
        }
    }

    private static void closeConn(Connection conn){
        try {
            if(conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            conn = null;
        }
    }
}

jdbc配置

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/wzdata
name=root
password=root

c3p0-config

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
   <!-- This is default config! -->
   <default-config>
      <property name="driverClass">com.mysql.jdbc.Driver</property>
      <property name="jdbcUrl">jdbc:mysql://localhost:3306/wzdata</property>
      <property name="user">root</property>
      <property name="password">123456</property>


      <property name="initialPoolSize">10</property>
      <property name="maxIdleTime">30</property>
      <property name="minPoolSize">30</property>
      <property name="maxPoolSize">100</property>
   </default-config>
</c3p0-config>
posted on 2019-11-05 18:29  进击中的青年  阅读(917)  评论(0编辑  收藏  举报