zoukankan      html  css  js  c++  java
  • 一个JDBC封装工具类

    前言

    在使用Java操作MySQL数据库的时候,会使用到JDBC驱动,但是在每次连接并使时都需要创建一堆的类和相关参数,还是有点麻烦的。在这里有一个封装好的JDBC工具类,里面包含了Java连接MySQL数据库时所需要的的属性和方法,代码如下。

    JDBCUtils.java

    /**
     * 对JDBC进行封装的工具类
     * 完成MySQL的连接和资源关闭
     */
    public class JDBCUtils {
        //定义相关的连接属性
        private static String user;//用户名
        private static String password;//密码
        private static String url;//url
        private static String driver;//驱动名
    
        //在static代码块进行初始化
        static {
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream("src\\mysql.properties"));//改路径是配置文件的路径
                //读取相关的属性值
                user = properties.getProperty("user");//获取用户名
                password = properties.getProperty("password");//获取密码
                url = properties.getProperty("url");//获取连接URL
                driver = properties.getProperty("driver");//获取驱动的路径
            } catch (IOException e) {
                //在实际开发中,可以这样处理IOException
                //编译异常-->运行处理
                //调用者可以选择捕获该异常,也可以选择默认处理,比较方便
                throw new RuntimeException(e);
            }
        }
    
        //编写方法,连接数据库,返回Connection给使用工具类的程序
        public static Connection getConnection(){
            try {
                return DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    
        //关闭相关资源
        /**
         * ResultSet 结果集
         * Statement 或 PreparedStatement
         * Connection
         * 如果需要关闭资源,就传入对象,否则传入null
         */
        public static  void close(ResultSet set, Statement statement,Connection connection){
            //判断是否为空
            try {
                if (set != null){
                    set.close();
                }
                if (statement != null){
                    statement.close();
                }
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    

    配置文件

    注意:配置文件的 " = " 两边不能有空格

    user=数据库用户名
    password=密码
    url=jdbc:mysql://localhost:3306/test
    driver=com.mysql.jdbc.Driver
    

    使用实例

    /**
     * 使用JDBCUtils工具类
     */
    public class JDBCUtils_Use {
        public static void main(String[] args) {
            //得到一个连接对象
            Connection connection = JDBCUtils.getConnection();
            //组织一个SQL语句
            // String sql = "update admin set name = ? where id = ?";
            String sql1 = "select * from `admin`";
            PreparedStatement preparedStatement = null;//扩大作用域
            ResultSet set = null;
            //创建一个PreparedStatement对象
            try {
                preparedStatement = connection.prepareStatement(sql1);
                //给占位符赋值
    //            preparedStatement.setString(1,"张三");
    //            preparedStatement.setInt(2,1);
                //开始执行SQL语句
                int rows = preparedStatement.executeUpdate();//这里的会返回一个受影响行数的int值,在JDBC中,使用不同的 preparedStatement 的方法,会有不同的返回值,比如下面的 executeQuery() 方法是一个返回一个结果集,可以通过遍历的方法将结果集的内容打印出来。
                set = preparedStatement.executeQuery();
    //            System.out.println(rows+" 行受到影响");
                //得到结果集
                //遍历该结果集
                while (set.next()){
                    int id = set.getInt("id");
                    String name = set.getString("name");
                    System.out.println(id +"\t"+ name);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                //关闭资源
                JDBCUtils.close(set,preparedStatement,connection);
            }
        }
    }
    
    
  • 相关阅读:
    0-Android系统各层中LOG的使用
    Android系统进程Zygote启动过程的源代码分析
    Android应用程序的Activity启动过程简要介绍和学习计划
    分享一个监测企业微信群人员变化的脚本...
    C++ 之 stl::string 写时拷贝导致的问题
    分享一个批量修改文件编码的python脚本
    分享stl sort函数坑点导致coredump问题
    关于使用repo时repo init和repo sync失败的一个解决方案
    sourceinsight sublimetext主题色配置
    父子进程之间的数据拷贝关系
  • 原文地址:https://www.cnblogs.com/nanfengashuai/p/15630667.html
Copyright © 2011-2022 走看看