首页 > 数据库技术 > 详细

JDBC_JDBCUtils1.0

时间:2017-08-25 15:31:40      阅读:171      评论:0      收藏:0      [点我收藏+]
package cn.JDBCUtils.com;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static Properties  props =null;
    static{
        try{
            InputStream in = JDBCUtils.class.getClassLoader()
                            .getResourceAsStream("dbconfig.properties");
            props = new Properties();
            props.load(in);}catch(Exception e){
                throw new RuntimeException();
            }
        try{
            Class.forName(props.getProperty("driverClassName"));
        }catch (Exception e) {
            throw new RuntimeException();
        }
        
    }
    public static Connection getConnection() throws SQLException{
        /*
         * 1、加载配置文件
         * 2、加载驱动类
         * 3、调用DriverManager.getConnection()
         * */
        return DriverManager.getConnection(props.getProperty("url"),props.getProperty("username"), props.getProperty("password"));
        
    }
}

//四大参数.propertise

driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://127.0.0.1\:3306/mydb1
username=root
password=123

 

JDBC_JDBCUtils1.0

原文:http://www.cnblogs.com/wangyinxu/p/7428161.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!