A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© EchoL 初级黑马   /  2019-4-18 22:46  /  440 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package day02.demo02;
import java.io.FileReader;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    /**
     * 得到配置文件中的Driver,URL,User,Password;
     */
    public static Connection getConection() throws SQLException {
        Properties properties = new Properties();
        FileReader fr = null;
        Connection connection = null;
        String driver = null;
        String url = null;
        String user = null;
        String password = null;
        try {
            fr = new FileReader("echo\\jdbcConfig.properties");
            properties.load(fr);
            driver = properties.getProperty("Driver");
            url = properties.getProperty("URL");
            user = properties.getProperty("User");
            password = properties.getProperty("Password");
            fr.close();
            //将驱动注册到内存中
            Class.forName(driver);
            //返回connection对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        connection = DriverManager.getConnection(url, user, password);
        return connection;
    }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马