zoukankan      html  css  js  c++  java
  • properties + IO 读取配置文件

    1.项目目录树

      

    2.配置文件config.properties
      username = sushe
      password = sushe
      url = jdbc:mysql://172.16.100.10:3306/sushe?useUnicode=true&characterEncoding=GBK
      jdbcDriverName = com.mysql.jdbc.Driver

    3.JdbcBean对象,用来存储jdbc链接信息

    package com.dlab.bean;
        
        public class JdbcBean {
        
            private String userName;
            
            private String password;
            
            private String Url;
            
            private String jdbcDriverName;
        
            public String getUserName() {
                return userName;
            }
        
            public void setUserName(String userName) {
                this.userName = userName;
            }
        
            public String getPassword() {
                return password;
            }
        
            public void setPassword(String password) {
                this.password = password;
            }
        
            public String getUrl() {
                return Url;
            }
        
            public void setUrl(String url) {
                Url = url;
            }
        
            public String getJdbcDriverName() {
                return jdbcDriverName;
            }
        
            public void setJdbcDriverName(String jdbcDriverName) {
                this.jdbcDriverName = jdbcDriverName;
            }
            
            
        }

    4.读取配置文件工具JdbcConfig.java

    package com.dlab.util;
        
        import java.io.IOException;
        import java.io.InputStream;
        import java.util.Properties;
        
        import com.dlab.bean.JdbcBean;
        
        public class JdbcConfig {
        
            private JdbcBean jdbcBean;
            
            public JdbcConfig()  throws IOException{
                    
                    //使用InputStream读取配置文件
                    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
                    Properties configReader = new Properties();
                    //加载配置文件
                    configReader.load(in);
                    
                    jdbcBean = new JdbcBean();
                    
                    jdbcBean.setUserName(configReader.getProperty("username"));
                    jdbcBean.setPassword(configReader.getProperty("password"));
                    jdbcBean.setUrl(configReader.getProperty("url"));
                    jdbcBean.setJdbcDriverName(configReader.getProperty("jdbcDriverName"));
            
            }
            
            //返回JdbcBean对象
            public JdbcBean getConfigInfo(){
                return jdbcBean;
            }
            
        }

    5.JDBC工具DBUtil.java,获取Connection

    package com.dlab.util;
        
        import java.io.IOException;
        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.PreparedStatement;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        
        import com.dlab.bean.JdbcBean;
        
        public class DBUtil {
        
            /**
             * 采用单例模式
             */
            private static DBUtil instance = new DBUtil();
            private Connection conn;
            
            private DBUtil (){
                JdbcBean jdbcBean;
                try {
                    jdbcBean = new JdbcConfig().getConfigInfo();
                    Class.forName(jdbcBean.getJdbcDriverName());
                    conn = DriverManager.getConnection(jdbcBean.getUrl(), jdbcBean.getUserName(), jdbcBean.getPassword());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
            }
            
            public static synchronized DBUtil getInstance (){
                return instance;
            }
            
            //返回Connection
            public Connection getConn(){
                return conn;
            }
            
            //Connection的静态关闭方法
            public static void close(Connection conn){
                if(conn != null){
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            //PreparedStatement的静态关闭方法
            public static void close(PreparedStatement pstmt){
                if(pstmt != null){
                    try {
                        pstmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            //ResultSet的静态关闭方法
            public static void close(ResultSet rs){
                if(rs != null){
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    6.测试程序DBTest.java

    package com.dlab.dbtest;
        
        import java.sql.Connection;
        import java.sql.PreparedStatement;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        
        import com.dlab.util.DBUtil;
        
        public class DBTest {
        
            public static void main(String[] args) {
                String sql = "select * from Admin";
                Connection conn = null;
                PreparedStatement pstmt = null;
                ResultSet rs = null;
                
                try {
                    conn = DBUtil.getInstance().getConn();
                    pstmt = conn.prepareStatement(sql);
                    rs = pstmt.executeQuery();
                    while(rs.next()){
                        System.out.println(rs.getString("Admin_Username"));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    DBUtil.close(rs);
                    DBUtil.close(pstmt);
                    DBUtil.close(conn);
                }
                
            }
        
        }
  • 相关阅读:
    ClipboardJS实现将页面内容复制到剪贴板
    全文索引FULLTEXT 的使用
    [1].Array.diff
    Leetcode——Queue队列
    Matplotlib——scatter散点图
    Matplotlib的一些小细节——tick能见度
    Matplotlib的一些小细节——Annotation标注
    Matplotlib的一些小细节——Legend图例
    Matplotlib基本知识(figure & axes
    锁升级简记
  • 原文地址:https://www.cnblogs.com/djoker/p/6289647.html
Copyright © 2011-2022 走看看