【JDBC】自定义连接、增删改查工具类

import java.sql.*;
import java.util.ResourceBundle;

public class DBUtils {
    private static final String DRIVER;
    private static final String USERNAME;
    private static final String PASSWORD;
    private static final String URL;

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        DRIVER = bundle.getString("jdbc.driver");
        USERNAME = bundle.getString("jdbc.user");
        PASSWORD = bundle.getString("jdbc.password");
        URL = bundle.getString("jdbc.url");
    }

    /**
     * 获取连接方法
     *
     * @return 连接
     */
    public static Connection getConnection() {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            return DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取Statement语句对象
     *
     * @param connection 连接
     * @return Statement对象
     */
    public static Statement createStatement(Connection connection) {
        try {
            return connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 执行增加删除修改的操作
     *
     * @param statement
     * @param sql
     * @return
     */
    public static int executeUpdate(Statement statement, String sql) {
        try {
            return statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 执行增加删除和修改的操作
     *
     * @param sql 要执行的sql语句
     * @return 影响的行数
     */
    public static int executeUpdate(String sql) {
        Connection connection = getConnection();
        Statement statement = createStatement(connection);
        int i = executeUpdate(statement, sql);
        DBUtils.close(statement, connection);
        return i;
    }

    public static int executeUpdatePrepared(String sql, Object... args) {//0...n
        Connection connection = getConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            int index=1;
            for (Object arg : args) {
                statement.setObject(index++,arg);
            }
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }finally{
            DBUtils.close(statement,connection);
        }
    }

    /**
     * 查询方法
     *
     * @param sql
     * @return DBObject对象
     */
    public static DBObject executeQuery(String sql) {
        Connection connection = getConnection();
        Statement statement = createStatement(connection);
        ResultSet resultSet = null;
        try {
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new DBObject(connection, statement, resultSet);
    }

    public static DBObject executeQueryPrepared(String sql,Object...args){
        Connection connection = getConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            int index=1;
            for (Object arg : args) {
                statement.setObject(index++,arg);
            }
            ResultSet resultSet=statement.executeQuery();
            return new DBObject(connection,statement,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void close(DBObject dbObject) {
        close(dbObject.getResultSet(), dbObject.getStatement(), dbObject.getConnection());
    }

    /**
     * 关闭连接
     *
     * @param connection
     */
    public static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭语句对象
     *
     * @param statement
     */
    public static void close(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭结果集对象
     *
     * @param resultSet
     */
    public static void close(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接和语句对象
     *
     * @param statement
     * @param connection
     */
    public static void close(Statement statement, Connection connection) {
        close(statement);
        close(connection);
    }

    /**
     * 关闭连接、语句对象和结果集
     *
     * @param statement
     * @param connection
     * @param resultSet
     */
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        close(resultSet);
        close(statement);
        close(connection);
    }
}

import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


public class DBObject {
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;

    public DBObject() {
    }

    public DBObject(Connection connection, Statement statement, ResultSet resultSet) {
        this.connection = connection;
        this.statement = statement;
        this.resultSet = resultSet;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public Statement getStatement() {
        return statement;
    }

    public void setStatement(Statement statement) {
        this.statement = statement;
    }

    public ResultSet getResultSet() {
        return resultSet;
    }

    public void setResultSet(ResultSet resultSet) {
        this.resultSet = resultSet;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值