1.2 封装jdbc的操作进行增删改查(掌握)

JdbcUtil.java

package com.xh.jdbc.util;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class JdbcUtil {

    /**
     * @description: 加载驱动
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("驱动加载异常");
        }
    }

    /**
     * @description: 获取连接
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    public  static Connection conn(){
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/casemanage?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "1214521");
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接异常");
        }
        return null;
    }
    /**
     * @description: 设置参数
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    public static void setParam(PreparedStatement preparedStatement,Object...ac){
        if(ac != null){
            for (int i = 0; i <ac.length ; i++) {
                try {
                    preparedStatement.setObject(i+1,ac[i]);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return;
    }
    /**
     * @description: 关闭连接
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    public static void close(PreparedStatement preparedStatement1,Connection conn1, ResultSet resultSet1){

        try {
            if(preparedStatement1 != null){
                preparedStatement1.close();
            }
            if(conn1 != null){
                conn1.close();
            }
            if(resultSet1 != null){
                resultSet1.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    public static int Update(String sql,Object... ac){
        Connection conn = null;
        PreparedStatement prepared = null;
        int j = 0;
        try {
            //获得连接
            conn = conn();
            prepared = conn.prepareStatement(sql);
            //参数赋值
            setParam(prepared,ac);
            j = prepared.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(prepared,conn,null);
        }
        return j;
    }


    public static List<Map> select(String sql, Object...ac){
        List<Map> objects = new ArrayList<>();
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        try {
            conn = conn();
            preparedStatement = conn.prepareStatement(sql);
            setParam(preparedStatement,ac);
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()){
                TreeMap<String, Object> map = new TreeMap<>();

                for (int i = 1; i <= columnCount; i++) {
                    String columnLabel = metaData.getColumnLabel(i);
                    map.put(columnLabel,resultSet.getObject(i));
                }
                objects.add(map);
            }
            close(preparedStatement,conn,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return objects;
    }

    public static void main(String[] args){

//        Object[] a = new Object[]{3};
//        FZjdbc01.update("insert sc value(2009,25,?)",a);
//        System.out.println("受影响的行数"+update);


        Object[] objects = {2001};
        System.out.println(JdbcUtil.select("select * from sc where s_no = ?", objects));


    }
}

封装完成后就可以很方便的调用封装好的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杵意

谢谢金主打赏呀!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值