JDBC 笔记02(获取数据库连接)

连接的方式一

    @Test
    public void testConnection_01() throws SQLException {

        Driver driver = new com.mysql.jdbc.Driver();

        //url = 协议://ip地址:端口号/数据库
        String url = "jdbc:mysql://localhost:3306/mysupermarket";
        //将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        Connection connect = driver.connect(url, info);

        System.out.println(connect);
    }

连接的方式二(对方式一的迭代)

    @Test
    public void testConnection_02() throws Exception {
        //1、获取Driver实现类对象(反射)
        Class aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //2、提供要连接的数据库
        String url = "jdbc:mysql://localhost:3306/mysupermarket";

        //3、提供连接需要的用户名和密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        //4、获取连接
        Connection connect = driver.connect(url, info);

        System.out.println(connect);
    }

连接的方式三

    @Test
    public void testConnection_03() throws Exception{
        //1、获取Driver实现类对象(反射)
        Class aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //2、提供需要的3个基本信息
        String url = "jdbc:mysql://localhost:3306/mysupermarket";
        String user = "root";
        String password = "123456";

        //3、注册驱动
        DriverManager.registerDriver(driver);
        //4、获取连接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
    }

连接的方式四(方式三的优化)

@Test
    public void testConnection_04() throws Exception{
        //1、提供需要的3个基本信息
        String url = "jdbc:mysql://localhost:3306/mysupermarket";
        String user = "root";
        String password = "123456";

        //2、加载Driver
        Class.forName("com.mysql.jdbc.Driver");

        /*
        * 省略了驱动的注册,
        * 因为在mysql的Driver实现类中声明的
        * static{
        *     try{
        *         java.aql.DriverManager.registerDriver(new driver);
        *     }
        *     catch{
        *         、、、、、、
        *     }
        * }
        * 语句块自动注册了驱动。
        * */

        //3、获取连接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
    }

连接的方式五(最终版)

  • 使用配置文件读取获取连接需要的4个基本信息:

配置文件代码如下:

user=root
password=azhu251264
url=jdbc:mysql://localhost:3306/mysupermarket
driverClass=com.mysql.jdbc.Driver
    @Test
    public void testConnection_05() throws Exception{
        //1、读取配置文件
        InputStream inputStream = ConnectionTest_01.class.getClassLoader().
            getResourceAsStream("jdbc.properties");

        Properties properties = new Properties();
        properties.load(inputStream);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");

        //2、加载驱动
        Class.forName(driverClass);

        //3、获取连接
        Connection connection = DriverManager.getConnection(url,user,password);

        System.out.println(connection);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值