获取数据库连接的五种方式

方式一:使用第三方的API

    第一步: 获取Driver 的实现类对象

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

    第二步: 选择正确的驱动程序,从而建立到数据库的连接

String url = "jdbc:mysql://localhost:3306/test";

       URL中代表的含义:

           jdbc:mysql:协议
           localhost: ip地址
           3306:默认mysql的端口号
           test:test数据库

    第三步:创建Properties对象,将用户名和密码封装在Properties中

Properties info = new Properties();
		
		info.setProperty("user", "root");
		info.setProperty("password", "123456");
		Connection conn = driver.connect(url, info);

    第四步:用输出语句证明连接成功

  System.out.println(conn);

   

方式二: 对方式一的迭代:不会出现第三方的API,使得程序具有更好的可移植性

    第一步: 获取Driver实现类对象,使用反射

Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();

    第二步:提供要连接的数据库

String url = "jdbc:mysql://localhost:3306/test";

    第三步:提供连接需要的用户名和密码

Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "123456");

    第四步:获取连接

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

方式三: 使用DriverManager替换Driver

    第一步:获取Driver实现类对象

Class clazz = Class.forName("com.mysql.jdbc.Driver");
                Driver driver = (Driver) clazz.newInstance();

    第二步.提供另外三个连接的基本信息

 String url = "jdbc:mysql://localhost:3306/test";
                String user = "root";
                String password = "123456";

    第三步:注册驱动

 DriverManager.registerDriver(driver);

    第四步:获取连接

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

方式四: 对方式三的简化,不显示的加载驱动

       那么驱动咋哪里加载呢, 驱动系统会默认加载,在mysql的Driver实现类中,声明了下面的代码块,所以不用加载显示的加载驱动

static {
             try {
                        java.sql.DriverManager.registerDriver(new Driver());
                  } catch (SQLException E) {
                        throw new RuntimeException("Can't register driver!");
                  }
 }

    第一步: 提供另外三个连接的基本信息

String url = "jdbc:mysql://localhost:3306/test";
				String user = "root";
				String password = "123456";

    第二步:获取Driver实现类对象(可以省略,但是不建议省略,因为mysql数据库可以省略,如果换一个其他的数据库就不能省略了)

Class clazz = Class.forName("com.mysql.jdbc.Driver");

    第三步:获取连接

Connection conn = DriverManager.getConnection(url, user, password);

方式五:(final版):将将数据库连接需要的4个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接

 此种方式的好处
     1.实现了数据与代码的分离,实现了解耦
     2.如果需要修改配置文件信息,可以避免程序重新打包

首先需要在src下新建一个jdbc.properties的文本文件

在文本文件中将4个基本信息声明在里面,注:信息里面不能有空格,因为在读取数据的情况下,空格也要读取 ,故4个基本信息声明在里面不能有空格.

    第一步:读取配置文件中的四个基本信息

InputStream is = connectionTest.class.getClassLoader().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");

    第二步:加载驱动

Class.forName(driverClass);

    第三步:获取连接

Connection conn = DriverManager.getConnection(url, user, password);

酒枯推荐

      推荐使用浏览器:Google Chrome

      推荐使用Java环境:IDEA(IntelliJ IDEA)

          本周推荐学习:JDBC的学习与应用

          推荐学习视频链接:https://www.bilibili.com/video/BV1eJ411c7rf?from=search&seid=2172690829084319707

          推荐练习Java环境:https://leetcode-cn.com/

猜你喜欢

转载自blog.csdn.net/weixin_52011642/article/details/112062458