博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java基础之JDBC三:简单工具类的提取及应用
阅读量:4345 次
发布时间:2019-06-07

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

简单工具类:

public class JDBCSimpleUtils {    /**     * 私有构造方法     */    private JDBCSimpleUtils() {    }    /**     * 驱动     */    public static String driver = null;    /**     * 连接字符串     */    public static String url = null;    /**     * 用户名     */    public static String user = null;    /**     * 密码     */    public static String password = null;    /**     * 读取配置文件, 并将读取到的值赋值给变量.     */    public static void readConfig() {        try {            //读取properties配置文件给变量赋值            Properties pp = new Properties();            pp.load(new FileReader("day04_classTest\\src\\config.properties"));            url = pp.getProperty("url");            user = pp.getProperty("username");            driver = pp.getProperty("driver");            password = pp.getProperty("password");        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * 静态代码块     * 变量赋值     * 并注册驱动     */    static {        try {            //变量赋值            readConfig();            //注册驱动            Class.forName(driver);        } catch (Exception e) {            //写入日志            //Logger.WriteLog(JDBCUtils.class.getName(),"");            e.printStackTrace();        }    }    /**     * 获取Connection     *     * @return 数据库连接     */    public static Connection getConnection() {        try {            return DriverManager.getConnection(url, user, password);        } catch (SQLException ex) {            //写入日志            //Logger.WriteLog(JDBCUtils.class.getName(),"");            ex.printStackTrace();            return null;        }    }    public static Statement getStatement(Connection conn) {        if (conn == null) {            return null;        }        try {            return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,                    ResultSet.CONCUR_UPDATABLE);            // 设置数据集可以滚动,可以更新        } catch (SQLException ex) {            //写入日志            //Logger.WriteLog(JDBCUtils.class.getName(),"");            release(conn);        }        return null;    }    /**     * 获取一个带参数的 PreparedStatement     * 该 PreparedStatement 已经设置数据集 可以滚动,可以更新     *     * @param conn      数据库连接     * @param cmdText   需要 ? 参数的 SQL 语句     * @param cmdParams SQL 语句的参数表     * @return 如果获取失败将返回 null,调用时记得检查返回值     */    public static PreparedStatement getPreparedStatement(Connection conn, String cmdText, Object... cmdParams) {        if (conn == null) {            return null;        }        PreparedStatement pstmt = null;        try {            pstmt = conn.prepareStatement(cmdText, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);            int i = 1;            for (Object item : cmdParams) {                pstmt.setObject(i, item);                i++;            }        } catch (SQLException e) {            e.printStackTrace();            release(conn, pstmt);            //写入日志            //Logger.WriteLog(JDBCUtils.class.getName(),"");        }        return pstmt;    }    /**     * 释放资源     *     * @param conn Connection     * @param stat Statement     * @param rs   ResultSet     */    public static void release(Connection conn, Statement stat, ResultSet rs) {        try {            if (rs != null) {                rs.close();                rs = null;            }        } catch (SQLException e) {            e.printStackTrace();            //写入日志            //Logger.WriteLog(JDBCUtils.class.getName(),"");        } finally {            try {                if (stat != null) {                    stat.close();                    stat = null;                }            } catch (SQLException e) {                e.printStackTrace();                //写入日志                //Logger.WriteLog(JDBCUtils.class.getName(),"");            } finally {                try {                    if (conn != null) {                        conn.close();                        conn = null;                    }                } catch (SQLException e) {                    e.printStackTrace();                    //写入日志                    //Logger.WriteLog(JDBCUtils.class.getName(),"");                }            }        }    }    /**     * 释放资源     *     * @param conn Connection     * @param stat Statement     */    public static void release(Connection conn, Statement stat) {        try {            if (stat != null) {                stat.close();                stat = null;            }        } catch (SQLException e) {            e.printStackTrace();            //写入日志            //Logger.WriteLog(JDBCUtils.class.getName(),"");        } finally {            try {                if (conn != null) {                    conn.close();                    conn = null;                }            } catch (SQLException e) {                e.printStackTrace();                //写入日志                //Logger.WriteLog(JDBCUtils.class.getName(),"");            }        }    }    /**     * 释放资源     *     * @param conn Connection     */    public static void release(Connection conn) {        try {            if (conn != null) {                conn.close();                conn = null;            }        } catch (SQLException e) {            e.printStackTrace();            //写入日志            //Logger.WriteLog(JDBCUtils.class.getName(),"");        }    }}

 

简单工具类的应用:

/**     * 增删改的方法     *     * @param sql     * @param obj     */    private static void executeNoQuery(String sql, Object... obj) {        //获取Connection        Connection conn = JDBCSimpleUtils.getConnection();        //获取PreparedStatement        PreparedStatement ps = JDBCSimpleUtils.getPreparedStatement(conn, sql, obj);        try {            int i = ps.executeUpdate();            if (i > 0) {                System.out.println("ok");            } else {                System.out.println("error");            }        } catch (SQLException e) {            e.printStackTrace();        } finally {            JDBCSimpleUtils.release(conn, ps);        }    }    /**     * 查询返回ResultSet     *     * @param sql     */    private static void getResultSet(String sql) {        //获取Connection        Connection conn = JDBCSimpleUtils.getConnection();        //获取Statement        Statement stat = JDBCSimpleUtils.getStatement(conn);        ResultSet rs = null;        try {            rs = stat.executeQuery(sql);            while (rs.next()) {                int id = rs.getInt("uid");                String name = rs.getString("uname");                String psw = rs.getString("psw");                System.out.println(id + "--" + name + "--" + psw);            }        } catch (SQLException e) {            e.printStackTrace();        } finally {            JDBCSimpleUtils.release(conn, stat, rs);        }    }

 

转载于:https://www.cnblogs.com/blazeZzz/p/9168896.html

你可能感兴趣的文章
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>