zoukankan      html  css  js  c++  java
  • 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数

    * 使用properties读取本地配置文件为代码传递参数
    * url、用户名、密码、驱动地址等配置可以在配置文件中使用


    main
    package zj_1_JDBC.properties;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    /*
    * 使用properties读取本地配置文件为代码传递参数
    * url、用户名、密码、驱动地址等配置可以在配置文件中使用
    *
    * 编辑工具类,整合 注册驱动和释放资源 方法。
    * */
    public class MainTest2 {
        public static void main(String[] args) {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
    
            //注册驱动
            conn = JDBCUtil_properties.getConn();
    
    
            try {
                //创建statement , 跟数据库打交道
                st = conn.createStatement();
    
                //执行查询 , 得到结果集
                String sql = "select * from users";
                rs = st.executeQuery(sql);
    
                //遍历查询每一条记录
                while (rs.next()) {
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    int age = rs.getInt("age");
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                //释放资源
                JDBCUtil_properties.closeRelease(conn,st,rs);
            }
        }
    }



    class类: JDBCUtil_properties
    package zj_1_JDBC.properties;
    
    import java.io.*;
    import java.sql.*;
    import java.util.Properties;
    
    /*
    *创建Properties对象,通过读取本地配置文件为代码传递参数。
    *
    *
    * Properties实现了Map接口,可以使用k,v键值对存储数据
    *Properties具有一些操作io流的内置方法
    *
    * Properties和IO流结合的功能:
            void load(Reader reader)读字符数据
            void list(PrintWriter out)写字符数据
    *
    * */
    public class JDBCUtil_properties {
        static String driverClass = null;
        static String url = null;
        static String username = null;
        static String password= null;
    
        static{
            try {
                //创建属性配置对象Properties
                Properties properties = new Properties();
                InputStream is = new FileInputStream("jdbc.properties");
                //使用类加载器,去读取src底下的资源文件。 后面在servlet
                //InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
    
                //导入输入流
                properties.load(is);
    
                //读取属性
                driverClass = properties.getProperty("driverClass");
                url = properties.getProperty("url");
                username = properties.getProperty("name");
                password = properties.getProperty("password");
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //获取连接对象
        public static Connection getConn() {
            Connection conn = null;
            try {
                Class.forName(driverClass);
                conn = DriverManager.getConnection(url,username,password);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return conn;
        }
    
        //释放资源
        public static void closeRelease(Connection conn , Statement st , ResultSet rs) {
            closeRs(rs);
            closeSt(st);
            closeConn(conn);
        }
    
        private static void closeRs(ResultSet rs) {
            try {
                if(rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                rs = null;
            }
        }
    
        private static void closeSt(Statement st) {
            try {
                if(st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                st = null;
            }
        }
    
        private static void closeConn(Connection conn) {
            try {
                if(conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                conn = null;
            }
        }
    }



    配置文件: jdbc.properties
    driverClass=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost/student
    name=root
    password=root

    输出

  • 相关阅读:
    iOS 组件化方案
    iOS 核心动画概览
    iOS @字面量
    iOS id 和 instancetype 的区别
    C++ 中的 const
    iOS 开发资料
    iOS 架构-App组件化开发
    iOS 知名大牛的一些博客
    iOS 键盘 隐藏系统的 toolBar
    iOS UIView 单独设置一个角为圆角,两个 三个角也行
  • 原文地址:https://www.cnblogs.com/longesang/p/11390304.html
Copyright © 2011-2022 走看看