JDBC学习笔记-02-获取数据库连接

获取连接的必备要素

这里以mysql为例子

  • Driver:相关数据库实现了sun标准JDBC接口的驱动jar包
  • url:一般这样写jdbc:mysql://localhost:3306/数据库名
  • username:数据库用户名
  • password: 数据库密码

数据库连接方法的迭代

方式一

@Test
	public void test1() throws SQLException {
		//1.获得相应的驱动,这里之前需要下载相应驱动jar包,
		//加载到当前项目下。获取驱动对象。
		//获取driver的实现类对象
		Driver driver = new com.mysql.jdbc.Driver();
			
		String url = "jdbc:mysql://localhost:3306/test";
		
		//将用户名封装在properties中
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "123");
		
		Connection conn = driver.connect(url, info);
		
		System.out.println(conn);
	}

方式二

@Test
	public void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
		//1. 获取driver实体类对象
		Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver)clazz.newInstance();
		
		//2. 提供要连接的数据库
		String url = "jdbc:mysql://localhost:3306/test";
		
		//3. 提供连接需要的用户名和密码
		Properties info = new Properties();
		info.setProperty("user","root");
		info.setProperty("password","123");
		
		//4.获取连接
		Connection connect = driver.connect(url, info);
		System.out.println(connect);
		
		
	}

方式三

//使用DriverMannager替换Driver
@Test
	public void test3() throws Exception, IllegalAccessException {
		//获取实现了Driver的实现类对象
		Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();
		
		//注册驱动
		DriverManager.deregisterDriver(driver);
		
		//提供另外三个连接的基本信息
		String url = "jdbc:mysql://localhost:3306/test";
		String user = "root";
		String password = "123";
		//获取连接
		Connection connection = DriverManager.getConnection(url, user, password);
		System.out.println(connection);
	}

方式四

@Test
		public void test4() throws Exception, IllegalAccessException {
			//提供另外三个连接的基本信息
			String url = "jdbc:mysql://localhost:3306/test";
			String user = "root";
			String password = "123";
			
			
			//加载Driver
			Class.forName("com.mysql.jdbc.Driver");//这部也可以省略,但是不要省略,因为oracle不一定有
			//因为在导入的driver中的META-INF/services/java.sql.Dirver就已经加载了。
			
			//相较于方式三,可以省略如下操做
//			Driver driver = (Driver) clazz.newInstance();
//			//注册驱动
//			DriverManager.deregisterDriver(driver);
//			为什么可以省略上述操做呢?
			/**
			 * 在MySQL的Driver实现类中,声明了如下的操做
			 * 
			 * static {
		try {
			java.sql.DriverManager.registerDriver(new Driver());
		} catch (SQLException E) {
			throw new RuntimeException("Can't register driver!");
		}
	}
			 */
			
			//获取连接
			Connection connection = DriverManager.getConnection(url, user, password);
			System.out.println(connection);
		}

这里可以省略是因为,mysql实现接口的驱动类里的静态代码块已经注册了驱动,如下:

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

方式五

//方式五:将数据库连接需要的4个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接
@Test
		public void test5() throws Exception, ClassNotFoundException {
			//1.读取配置文件中的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 driver = pros.getProperty("driver");
			
			Class.forName(driver);
			
			Connection conn = DriverManager.getConnection(url, user, password);
			
			System.out.println(conn);
			
			
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值