JDBC封装

1 篇文章 0 订阅

通过jdbc对数据库进行操作通常需要好几个步骤,而对数据库的操作经常要用到,所以将jdbc封装成一个工具类以便调用。

1.配置文件

新建一个jdbc.properties文件,放在项目的src目录下。文件内容为:

driver:com.mysql.cj.jdbc.Driver
url:jdbc:mysql://localhost:3306/jdbctest?useSSL=false&serverTimezone=UTC
user:root
pwd:123456

其中jdbctest为数据库名


2.java代码

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * Created by otote Cotter on 2018/9/11.
 */
public class MySqlUtil {
    public static Connection conn=null;
    public static PreparedStatement statement=null;
    public static ResultSet resultSet=null;
    public static String driver=null;
    public static String url=null;
    public static String user=null;
    public static  String pwd=null;
    /***
     * 加载配置文件  初始化
     */
    static {
        try {
            Properties properties=new Properties();
            //加载配置文件   通过类加载器
            properties.load(MySqlUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            user=properties.getProperty("user");
            pwd=properties.getProperty("pwd");
            //加载驱动
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /***
     * 获得连接
     * @return
     */
    public static Connection getConnection(){
        Connection connection=null;
        try {
            connection=DriverManager.getConnection(url, user, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }


    /***
     * 获取操作数据库的对象
     * @param sql sql语句
     * @param ob   参数    可变
     * @return PreparedStatement
     */
    public static PreparedStatement getStatement(String sql,Object...ob){
        //加载驱动
        try {
            //创建连接对象
            conn= getConnection();
            //创建执行对象
            statement=conn.prepareStatement(sql);
            //如果有参数  则添加参数
            if (ob.length>0){
                for(int i=0;i<ob.length;i++){
                    statement.setObject(i+1, ob[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statement;
    }

    /***
     * 查询
     * 返回查询的结果集合
     * @param sql sql语句
     * @param ob   可变参数
     * @return ResultSet结果集合
     */
    public static ResultSet mySelect(String sql,Object...ob){
        PreparedStatement statement=getStatement(sql, ob);
        try {
            resultSet=statement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    /****
     * 对数据库的增、删、改
     * @param sql sql语句
     * @param ob   可变参数
     * @return 操作完成的sql语句数量
     */
    public static int myUpdate(String sql,Object...ob){
        PreparedStatement statement=getStatement(sql, ob);
        //执行成功的条数
        int count=0;
        try {
            count=statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /***
     * 关闭连接  释放资源
     */
    public static void closeAll(){
        try {
            if (resultSet!=null){
                resultSet.close();
            }
            if(statement!=null){
                statement.close();
            }
            if (conn!=null){
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值