connDB

来源:互联网 发布:淘宝预约快递寄件 编辑:程序博客网 时间:2024/05/17 09:32

jar包
mysql-connector-java-5.1.7-bin.jar

db.properties文件

#oracle数据库#driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl#user=scott#password=tiger#mysql数据库driver=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/testuser=rootpassword=root

测试类,实现连接mysql数据库:
通过Driver实现类对象连接数据库

public void testDriver() throws SQLException {//      通过Driver实现类对象连接数据库        Driver driver = new com.mysql.jdbc.Driver();//      协议:子协议://数据库所在的主机地址:数据库端口号/数据库名//      准备连接数据库的基本信息  url user password        String url = "jdbc:mysql://localhost:3306/test";        Properties info = new Properties();        info.put("user", "root");        info.put("password", "root");//      调用Driver接口的 connect(url,info)获取连接对象        Connection connect = driver.connect(url, info);        System.out.println(connect);    }

/*
* 编写一个通用的方法,在不修改源程序情况下获得任何数据库的连接
* 解决:把数据库驱动Driver实现类的全类名、url、user、password放入一个
* 配置文件中,可以修改配置文件实现和具体的数据库连接
*/

public Connection getConnection() throws Exception{        String driverClass = null;        String jdbcUrl = null;        String user = null;        String password = null;//      读取类路径下的jdbc.properties文件        FileReader fr = new FileReader(new File("jdbc.properties"));//      InputStream in =//getClass().getClassLoader().getResourceAsStream("jdbc.properties");        Properties properties = new Properties();        properties.load(fr);        driverClass = properties.getProperty("driver");        jdbcUrl = properties.getProperty("jdbcUrl");        user = properties.getProperty("user");        password = properties.getProperty("password");//      通过反射创建driver对象        Driver driver = (Driver) Class.forName(driverClass).newInstance();        Properties info = new Properties();        info.put("user", user);        info.put("password", password);        Connection connection = driver.connect(jdbcUrl, info);        return connection;    }    @org.junit.Test    public void testGetConnection() throws Exception{        System.out.println(getConnection());    }

DriverManager 驱动管理类

public class TestDriverManager {    /*     * DriverManager 驱动的管理类     */    @Test    public void testDriverManager() throws Exception {//      准备连接的数据库 的基本信息        String driverClass = null;        String jdbcUrl = null;        String user = null;        String password = null;//      读取类路径下的jdbc.properties文件        InputStream in =                getClass().getClassLoader().getResourceAsStream("jdbc.properties");        Properties properties = new Properties();        properties.load(in);        driverClass = properties.getProperty("driver");        jdbcUrl = properties.getProperty("jdbcUrl");        user = properties.getProperty("user");        password = properties.getProperty("password");//      加载驱动程序(对应的Driver 实现类中 有注册驱动的静态代码块)//      DriverManager.registerDriver(//              (Driver)Class.forName(driverClass).newInstance());//      加载时,已经注册过了        Class.forName(driverClass);//      通过DriverManager 的 getConnection(url,user,password) 获取数据库连接        Connection connection = DriverManager.getConnection(jdbcUrl, user,password);        System.out.println(connection);    }}
原创粉丝点击