BaseDao包(含连接数据库)

package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

//负责数据库的增删改查操作
public class BaseDao {

    private static final String URI= "jdbc:mysql://localhost:3306/date2?"+"user=root&password=root&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";//换成你自己的数据库名字和密码
    private static final String DRIVER="com.mysql.cj.jdbc.Driver";
    /*public static Connection connectDB()throws Exception {
        //加载数据库驱动
        Class.forName(DRIVER);
        //获取数据库连接
        Connection conn= DriverManager.getConnection(URI);
        return conn;
    }*/
    //存放Connection对象的数组,数组被看成连接池
    static ArrayList<Connection> list = new ArrayList<Connection>();
    //获得链接
    public synchronized static Connection getConnection() {
        Connection con = null;
//如果连接池中有连接对象
        if (list.size() > 0) {
            return list.remove (0) ;}
//连接池没有连接对象,创建连接放到连接池中
        else{
            for(int i=0;i<5;i++){
                try{
                    Class. forName (DRIVER);
                } catch (ClassNotFoundException e){

                    e.printStackTrace() ;}
//创建连接
                try {
                    con = DriverManager. getConnection ("jdbc:mysql://localhost:3306/date2?characterEncoding=utf-8","root","200068") ;
                    list.add(con);
                }catch (SQLException e){
                    e.printStackTrace() ;
                }
            }
        }
        return list.remove(0);
    }

    //关闭结果集

    public static void close(ResultSet rs){
        if( rs!=null){
            try{
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
    //关闭预处理
    public static void close(PreparedStatement pst){
        if( pst!=null){
            try{
                pst.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    //关闭连接
    public static synchronized void close(Connection con){
        if( con!=null){
            list.add(con);
        }
    }

    //关闭所有连接有关的有对对象
    public static void close(ResultSet rs,PreparedStatement ps,Connection con) {
        close(rs);
        close(ps);
        close(con);

    }



    //新增,修改,删除处理

    public boolean updateByParams(String sql,Object param[]){
        boolean flag=false;
        Connection con=getConnection();
        PreparedStatement ps=null;
        try{
            ps=con.prepareStatement(sql);
            if(param!=null){
                for(int i=1;i<=param.length;i++) {
                    ps.setObject(i, param[i - 1]);
                }
            }
            int n=ps.executeUpdate();
            if(n>0)
                flag=true;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            close(null,ps,con);
        }
        return flag;
    }
    //新增,修改,删除处理(批量)
    public boolean BatchUpdateByParams(String sql,Object param[][]){
        Connection con=getConnection();
        PreparedStatement ps=null;
        try{
            ps=con.prepareStatement(sql);
            if(param!=null){
                for(int i=0;i<=param.length;i++) {
                    for (int j = 1; j <= param[i].length; i++) {
                        ps.setObject(j, param[i][j - 1]);
                    }
                    ps.addBatch();
                }
                ps.executeBatch();
            }
            return true;
        }catch (SQLException e){
            e.printStackTrace();
            return  false;
        }finally {
            close(null,ps,con);
        }
    }
    //查询
    public static List<Map<String,Object>> select (String sql, Object[] param){
        Connection con =getConnection();

        PreparedStatement ps=null ;

        ResultSet rs = null;

        List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
        try{

            ps = con.prepareStatement (sql) ;

            if (param!= null) {

                for (int i = 1; i <= param. length; i++) {

                    ps.setObject(i, param[i-1]) ;
                }
            }

            rs = ps.executeQuery();

            ResultSetMetaData rm=rs.getMetaData();
            //列数

            int count=rm.getColumnCount();

            while (rs.next()) {

                Map<String, Object> map = new HashMap<String, Object>();

                for (int i = 1; i <= count; i ++) {

//key为列名, value为列值

                    map.put(rm.getColumnName(i).toLowerCase(), rs.getObject(rm.getColumnName(i)));
                }
                list.add(map);}
        }catch (SQLException e) {
            e.printStackTrace();

        }finally{

            close(rs, ps, con);
        }
        return list;
    }







    //获取最后一个ID
    public int getLastId(String sql, String sqll, Object[] param) {
        Connection con = getConnection() ;


        PreparedStatement ps = null;

        PreparedStatement ps1 = null;

        ResultSet rs = null;

        int id=0;

        try{

            ps = con.prepareStatement (sql);

            if(param != null) {

                for(int i=1; i <=param. length; i++){

                    ps.setObject(i, param[i-1]);}
            }

            ps.executeUpdate();

            ps1=con.prepareStatement(sqll);

            rs= ps1.executeQuery();

            if(rs.next())

                id= rs.getInt(1);

            close (ps1);

        }catch (SQLException e){

            e.printStackTrace();
        }finally{

            close(rs, ps,con) ;}

        return id;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值