JDBC的封装

JDBC的介绍和使用

引言

在java中jdbc的使用和封装

介绍

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC也是个商标名。

JDBC编程步骤

  1. 装载相应的数据库的JDBC驱动并进行初始化 导入专用的jar包(不同的数据可需要的jar包不同)

  2. 加载驱动(mysql的最新驱动为com.mysql.cj.jdbc.Driver)

    Class.forName(“com.mysql.cj.jdbc.Driver”);

  3. 创建连接(msql更新后需要加serverTimezone时区,Asia/Shanghai为东八区的时区)

    connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/ningda?useSSL=true&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&user=root&password=root”);

  4. 编写sql语句

    String sql = “select * from tb_user”;

  5. 创建prepareStatement预处理对象并执行sql语句

    statement = connection.prepareStatement(sql);
    
  6. 处理查询结果

    ResultSet resultSet = statement.executeQuery();
    while (resultSet.next()) {
    String name = resultSet.getString(“username”);
    String password = resultSet.getString(“password”);
    map.put(name, password);
    }

  7. 关闭数据库连接

    statement.close();
    connection.close();

封装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();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值