栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Eclipse 获取数据库连接的6种方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Eclipse 获取数据库连接的6种方式

第一种:

@Test
	void testConn01() {
		
		// 创建驱动
		try {
//			Driver driver = new com.mysql.jdbc.Driver();
			
			// 耦合度太高
			Driver driver = new com.mysql.cj.jdbc.Driver();
			//2.提供url,指明具体操作的数据
			String url = "jdbc:mysql://localhost:3306/db_web";
			//3.提供Properties的对象,指明用户名和密码
			Properties info = new Properties();
			info.setProperty("user", "root");
			info.setProperty("password", "root");
			
			// 如果数据库已经是8.0,需要的驱动也必须是8.0以上
			// 如果使用了8.0的驱动,则必须设置时区参数
			info.setProperty("serverTimezone", "Asia/Shanghai");
			//4.调用driver的connect(),获取连接
			Connection conn = driver.connect(url, info);
			System.out.println(conn);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

第二种:

@Test
	void testConn02() {
		
		// 创建驱动
		try {
			Driver driver = null;
			
			// 通过反射降低耦合度
			Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
			driver = (Driver) clazz.getConstructor().newInstance();
			
			//2.提供url,指明具体操作的数据
			String url = "jdbc:mysql://localhost:3306/db_web";
			//3.提供Properties的对象,指明用户名和密码
			Properties info = new Properties();
			info.setProperty("user", "root");
			info.setProperty("password", "root");
			
			// 如果数据库已经是8.0,需要的驱动也必须是8.0以上
			// 如果使用了8.0的驱动,则必须设置时区参数
			info.setProperty("serverTimezone", "Asia/Shanghai");
			//4.调用driver的connect(),获取连接
			Connection conn = driver.connect(url, info);
			System.out.println(conn);
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}

第三种:

@Test
	void testConn03() {
		// 创建驱动
		try {
			Driver driver = null;
			// 通过反射获取Driver类
			Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
			// 得到Driver对象
			driver = (Driver) clazz.newInstance();
			
			// 使用第二种方法,也是我们推荐的方式,DriverManeger来进行连接的获取
			DriverManager.registerDriver(driver);
			
			//2.提供url,指明具体操作的数据
			String url = "jdbc:mysql://localhost:3306/db_web";
			//3.提供Properties的对象,指明用户名和密码
			Properties info = new Properties();
			info.setProperty("user", "root");
			info.setProperty("password", "root");
			info.setProperty("serverTimezone", "Asia/Shanghai");
			
			Connection connection = DriverManager.getConnection(url, info);
			System.out.println(connection);
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

第四种:

@Test
	void testConn04() {
		// 创建驱动
		try {
			
			//2.提供url,指明具体操作的数据
			String url = "jdbc:mysql://localhost:3306/db_web";
			//3.提供Properties的对象,指明用户名和密码
			Properties info = new Properties();
			info.setProperty("user", "root");
			info.setProperty("password", "root");
			info.setProperty("serverTimezone", "Asia/Shanghai");
			
			Connection connection = DriverManager.getConnection(url, info);
			System.out.println(connection);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

第五种:

@Test
	void testConn05() {
		try {
			// 1、加载驱动【可以不写】
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			// 2、通过DriverManager类来获取驱动
			
			// DriverManager.getConnection("jdbc:mysql:///db_web?user=root&password=root&serverTimezome=Asia/Shanghai");
			
			// 第二种构造函数, 提供Properties的对象,指明用户名和密码
//			Properties info = new Properties();
//			info.setProperty("user", "root");
//			info.setProperty("password", "root");
//			info.setProperty("serverTimezone", "Asia/Shanghai");
//			DriverManager.getConnection("jdbc:mysql:///db_web", info);
			
			Connection connection = DriverManager.getConnection("jdbc:mysql:///db_web?serverTimezone=Asia/Shanghai", "root", "root");
			System.out.println(connection);
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	

第六种:

    // 最终的书写方式
	// 使用Java的配置文件解决硬编码问题
	@Test
	void testConn06() {
		try {
			Properties properties = new Properties();
			properties.load(this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"));
			
			String classDriver = properties.getProperty("jdbc.classDriver");
			String url = properties.getProperty("jdbc.url");
			String user = properties.getProperty("jdbc.user");
			String password = properties.getProperty("jdbc.password");
			Class.forName(classDriver);
			Connection connection = DriverManager.getConnection(url, user, password);
			System.out.println(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
	}

配置文件:

jdbc.classDriver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/school?serverTime=Aisa/Shanghai
jdbc.user=root
jdbc.password=123456

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659439.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号