jdbc简单封装

构成:

  1、资源文件db.properties,中存放了驱动类地址、数据库url、用户名、密码。

  2、jdbc工具类JdbcUtils.java。

 


信息:

//Oracle驱动地址
oracle.jdbc.driver.OracleDriver
//Oracle的url
jdbc:oracle:thin:@localhost:1521:数据库名

//MySQL驱动地址
com.mysql.jdbc.Driver
//MySQL的url
jdbc\:mysql\://localhost\:3306/数据库名?useSSL=false

代码:

  1、资源文件db.properties:

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/leaningjdbc?useSSL=false 
user=root 
pwd=123456

  2、源文件JdbcUtils.java,它实现了:

  1、获取数据库连接。

  2、获取sql命令对象,包括Statement和PreparedStatement对象。

  3、执行DML语句。

  4、释放资源。

package lurenjia.jdbcutils;

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

/**
 * 数据库连接工具类
 * @author lurenjia
 * @date 2022/12/28-16:02
 */
public class JdbcUtils {
    private static String driver;
    private static String url;
    private static String user;
    private static String pwd;
    //1.加载配置文件。2.完成数据库驱动的加载。
    static {
        //1.加载资源文件对象
        Properties pros = new Properties();
        //2.加载src目录下的资源文件
        try {
            pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //3.通过键名,获取值
        driver = pros.getProperty("driver");
        url = pros.getProperty("url");
        user = pros.getProperty("user");
        pwd = pros.getProperty("pwd");
        //4.加载数据库驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private JdbcUtils(){}

    /**
     * 获取数据库连接
     */
    public static Connection getConnection(){
        Connection connection=null;
        try {
            connection = DriverManager.getConnection(url,user,pwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    /**
     * 获取Statement对象
     * @param connection 数据库连接
     * @return 获取到的Statement对象
     */
    public static Statement getStatement(Connection connection){
        try {
            return connection.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取PreparedStatement对象
     * @param connection 数据库连接
     * @param sql 预编译sql语句
     * @return 获取到的PreparedStatement对象
     */
    public static PreparedStatement getPreparedStatement(Connection connection,String sql){
        try {
            return connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 关闭资源。
     * @param statement sql命令对象
     * @param connection 数据库连接对象
     */
    public static void close(Statement statement,Connection connection){
        try {
            if(statement!=null) {
                statement.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            if(connection!=null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 关闭资源。
     * @param resultSet 结果集对象
     * @param statement sql命令对象
     * @param connection 数据库连接对象
     */
    public static void close(ResultSet resultSet,Statement statement,Connection connection){
        try {
            if(resultSet!=null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        close(statement,connection);
    }

    /**
     * 使用PreparedStatement执行DML语句,获取更新的数据数量。
     * @param sql sql语句
     * @param objects 参数
     * @return 更改数据的行数,若失败则返回-1
     */
    public static int executeDML(String sql,Object...objects){
        //结果中修改的数据行数
        int count = -1;
        //定义jdbc对象
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        
        try {
            //获取jdbc对象
            connection = getConnection();
            //设置事务为手动提交
            connection.setAutoCommit(false);
            preparedStatement = getPreparedStatement(connection,sql);
            
            //设置sql语句参数
            for(int i =0;i< objects.length;i++){
                preparedStatement.setObject(i+1,objects[i]);
            }
            //执行dml语句
            count = preparedStatement.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            //发生错误时进行回滚
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }finally {
            //关闭资源
            close(preparedStatement,connection);
        }
        return count;
    }
}

 

posted @ 2022-12-28 17:14  在博客做笔记的路人甲  阅读(27)  评论(0编辑  收藏  举报