学习笔记:JDBC获取数据库的连接

前置要求:
确认已经成功安装mysql数据库,
其次已经在当前工程下导入mysql的jdbc驱动。

获取数据库连接的基本步骤:
1.获得Driver实现类的对象
2.获得连接需要的几个基本信息
3.获取连接

方式一:new操作符的方式获取Driver对象

    @Test
    public void test1() throws SQLException {
        //1.获取driver实现类对象
        Driver driver = new com.mysql.jdbc.Driver();
        
        //2.将用户名和密码封装在properties中
        String url = "jdbc:mysql://localhost:3306/myemployees?useUnicode=true&characterEncoding=utf8";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","0000");
        //3.获取连接
        Connection conn = driver.connect(url,info);
        System.out.println(conn);
    }

方式二:通过反射的方式获取Driver对象

 @Test
    public void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //1.获取Driver的实现类对象,使用反射
        Class cla = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)cla.newInstance();
        //2.提供要连接的数据库
        String url = "jdbc:mysql://localhost:3306/myemployees?useUnicode=true&characterEncoding=utf8";
        //3.提供连接需要的用户名和密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","0000");
        //4.获取连接
        Connection conn = driver.connect(url,info);
        System.out.println(conn);

    }

方式三:调用DriverManager类的getConnection()方法来获取连接

 @Test
    public void test3() throws SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        //1.获取Driver的实现类对象,使用反射
        Class cla = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) cla.newInstance();
        //2.提供连接需要的基本信息
        String url = "jdbc:mysql://localhost:3306/myemployees?useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "0000";
        //3.注册驱动
        DriverManager.registerDriver(driver);
        //4.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
    }

方式四:可以省略方式三中注册驱动和创建对象的操作,因为在mysql的Driver实现类的静态代码块中实现了这两步操作

 @Test
    public void test4() throws SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        Class cla = Class.forName("com.mysql.jdbc.Driver");
       // Driver driver = (Driver) cla.newInstance();可以省略 在mysql的Driver实现类的静态代码块中实现了这两步操作

        String url = "jdbc:mysql://localhost:3306/myemployees?useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "0000";

        //DriverManager.registerDriver(driver);
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
    }

方式五:通过读取properties文件来获取基本信息

 @Test
    public void test5() throws IOException, ClassNotFoundException, SQLException {
        //1.读取文件中的四个基本信息
        InputStream is = ConnectionTest.class.getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);
        String user = pros.getProperty("user");
        String password =pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        //2.加载驱动
        Class.forName(driverClass);
        //3.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
    }
posted @ 2021-04-13 00:10  夏天的风key  阅读(63)  评论(0编辑  收藏  举报