博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java工具类--数据库操作封装类
阅读量:4364 次
发布时间:2019-06-07

本文共 2533 字,大约阅读时间需要 8 分钟。

  java对数据库操作简单处理,如下代码即可,封装了 增删改查及获取连接、关闭连接。

代码如下:

package com.test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;/** * 操作数据库工具类 * * */public class DbUtil {    /**     * 连接数据     *     * @return conn     */    public static Connection getConnection(String driver,String url,String username,String password) {        Connection conn = null;        try {            Class.forName(driver);            conn = DriverManager.getConnection(url, username, password);        } catch (Exception e) {            e.printStackTrace();        }        return conn;    }    /**     * 关闭连接对象     *     * @param conn     *            连接对象     * @param pstmt     *            预编译对象     * @param rs     *            结果集     */    public static void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {        try {            if (rs != null) {                rs.close();            }            if (pstmt != null) {                pstmt.close();            }            if (conn != null) {                conn.close();            }        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * 增删改操作     *     * @param sql     *            SQL命令     * @param param     *            参数     * @return     */    public static int executUpdate(Connection conn,String sql, Object[] param) {        int result = 0;        PreparedStatement pstmt = null;        try {            pstmt = conn.prepareStatement(sql);            if (param != null) {                for (int i = 0; i < param.length; i++) {                    pstmt.setObject(i + 1, param[i]);                }            }            result = pstmt.executeUpdate();        } catch (Exception e) {            e.printStackTrace();        } finally {            closeAll(conn, pstmt, null);        }        return result;    }    /**     * 查询     *     * @return int     * @date 2015-7-25 上午11:10:06     */    public static ResultSet executQuery(Connection conn,String sql, String[] param) {        PreparedStatement pstmt = null;        ResultSet result = null;        try {            pstmt = conn.prepareStatement(sql);            if (param != null) {                for (int i = 0; i < param.length; i++) {                    pstmt.setString(i + 1, param[i]);                }            }            result = pstmt.executeQuery();        } catch (Exception e) {            e.printStackTrace();        }          return result;    }}

转载于:https://www.cnblogs.com/haha12/p/4679297.html

你可能感兴趣的文章
数据结构七大排序
查看>>
你真的了解iOS的深浅拷贝吗?
查看>>
对症下药,找到Visual Studio每次编译都提示不是最新的根本原因
查看>>
19 反射
查看>>
MTK Android Driver :Camera
查看>>
Servlet含义与工作原理
查看>>
内容页访问母版页页控件的方法
查看>>
BZOJ4825 单旋
查看>>
博客搬家到https://laowei.wang
查看>>
hdu 2196 Computer(树形DP)
查看>>
Django生命请求周期
查看>>
深入Asyncio(十二)Asyncio与单元测试
查看>>
python正则实现简单计算器
查看>>
网络状况判断
查看>>
SET || BZOJ 1588: [HNOI2002]营业额统计 || Luogu P2234 [HNOI2002]营业额统计
查看>>
SQL Server 更改隔离级
查看>>
svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法...
查看>>
2018年11月16日编程体会
查看>>
EASYUI DATAGRID 多列复选框CheckBox
查看>>
fit_transform和transform的区别
查看>>