zoukankan      html  css  js  c++  java
  • JDBC利用.properties文件连接数据库

    package com.jdbc.utils;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Properties;
    
    public class JDBCUtilsX {
    
        private static Properties properties=null;
        static{
            properties=new Properties();
            try {
                properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         *
         * @return Connection
         */
        public static Connection getConnection(){
            try {
                Class.forName(properties.getProperty("driver"));
                return  DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("user"),properties.getProperty("password"));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    
        }
    
     public static void close(AutoCloseable... autoCloseable){
    
            for(AutoCloseable c:autoCloseable){
                if(c!=null){
                    try {
                        c.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
     }
    
    
    }
    

      db.properties文件的配置

    url=jdbc:mysql://localhost:3306/test     
    user=root
    password=123456
    driver=com.mysql.jdbc.Driver
  • 相关阅读:
    线程池问题
    高级I/O
    闹钟设计
    线程竞争问题
    线程基本函数
    SpringMvc支持跨域访问
    gitlab qq邮件配置
    gitlab断电
    docker run always
    电子书网
  • 原文地址:https://www.cnblogs.com/qurui1997/p/10639804.html
Copyright © 2011-2022 走看看