JDBC – 学习2 – 获取Connection对象、关闭资源

导读:本篇文章讲解 JDBC – 学习2 – 获取Connection对象、关闭资源,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

数据库连接字符串url: 主协议 : 子协议 : ip地址 : 数据库端口号 : 数据库名

一. 获取连接Connection

硬编码

方法1:硬编码 – Driver

public static Connection getConnection1() throws SQLException {
       
       // 1. 加载驱动
        Driver driver = new oracle.jdbc.driver.OracleDriver();

        // 2. 连接数据库
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";

        // 属性对象--连接字符串需要的一些信息 封装到 一个Properties对象上
        Properties info = new Properties();
        info.setProperty("user", "scott");
        info.setProperty("password", "123456");
        Connection conn = driver.connect(url, info);

        System.out.println(conn);
		
        // 3. 返回Connection对象
        return conn;
}	

反射、硬编码

方法2:反射、硬编码结合 – Class、Driver、DriverManager

@Test
public static Connection getConnection3() throws Exception {

		Class class_driver = Class.forName("oracle.jdbc.driver.OracleDriver");
		Driver driver = (Driver) class_driver.newInstance();

		// 1. 注册新驱动
		DriverManager.registerDriver(driver);

		// 2. 连接数据库的信息
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		String user = "scott";
		String password = "123456";

		// 3. 连接数据库
		Connection conn = DriverManager.getConnection(url, user, password);

		System.out.println(conn);

		return conn;
}

方法3:反射、硬编码结合 – Class、Driver

public static Connection getConnection2() throws Exception {

		// 1. 使用反射获取数据库驱动对象
		Class<?> class_driver = Class.forName("oracle.jdbc.driver.OracleDriver");
		Driver driver = (Driver) class_driver.newInstance();

		// 2. 数据库连接字符串 主协议:子协议:ip地址:数据库端口号:数据库名
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";

		// 3. 属性对象--连接字符串需要的一些信息 封装到 一个Properties对象上
		Properties info = new Properties();
		info.setProperty("user", "scott");
		info.setProperty("password", "123456");

		// 4. 连接数据库
		Connection conn = driver.connect(url, info);

		System.out.println(conn);

		return conn;
}

方法4:反射、硬编码结合 – Class、DriverManager

@Test
public static Connection getConnection4() throws Exception {
    
		// 1.加载类,静态代码块自动注册驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");

		// 2. 连接数据库的信息
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		String user = "scott";
		String password = "123456";

		// 3. 连接数据库
		Connection conn = DriverManager.getConnection(url, user, password);

		System.out.println(conn);

		return conn;
}

反射、配置文件 – 最终版

方法5:反射、配置文件结合 – Class、FileInputStream

public static Connection getConnection5() throws Exception {

//		InputStream is = TestConnection.class.getClassLoader().getResourceAsStream("E:/eclipse/workspace/东软/src/top/linruchang/jdbc/jdbc.properties");

		// 1. 读取连接数据库 配置信息 从文件中
		InputStream is = new FileInputStream("E:/eclipse/workspace/东软/src/top/linruchang/jdbc/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);

		return conn;
	}

二. 关闭资源

public static void closeResource(Connection conn, Statement statement, ResultSet rs) {
		
		// 1. 关闭结果集
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 2. 关闭sql语句声明
		try {
			if (statement != null)
				statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 3. 关闭数据库连接
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/46562.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!