JDBC相关知识总结

96 阅读2分钟

获取JDBC链接的若干方式

测试类名称ConnectionTest.java

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

public class ConnectionTest {
	
	//方式一:
	@Test
	public void testConnection1() throws SQLException{
		
		//获取Driver的实现类对象
		Driver driver = new com.mysql.jdbc.Driver();
		
		String url = "jdbc:mysql://localhost:3308/student";
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "123456");
		
		Connection conn = driver.connect(url,info);
		
		System.out.println(conn);
	}
	//方式二:对方式一的迭代:在如下的程序中不出现第三方的api,使得程序具有更好的可移植性
	@Test
	public void testConnection2() throws Exception{
		//1、获取Driver实现类对象:使用反射
		Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();
		
		//2、提供要连接的数据库
		String url = "jdbc:mysql://localhost:3308/student";
		
		//3、提供连接需要的用户名和密码
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "123456");
		
		//4、获取连接
		Connection conn = driver.connect(url,info);
		System.out.println(conn);
	}
	
	//方式三:使用DriverManager替换Driver
	@Test
	public void testConnection3()throws Exception{
		//1、获取Driver的实现类对象
		Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();
		
		//2、提供另外三个连接的基本信息:
		String url="jdbc:mysql://localhost:3308/student";
		String user="root";
		String password="123456";
		
		//注册驱动
		DriverManager.registerDriver(driver);
		
		//获取连接
		Connection conn = DriverManager.getConnection(url,user,password);
		System.out.println(conn);
	}
	
	//方式四:优化方式三。可以只是加载驱动,而不用显示的注册驱动了。
	@Test
	public void testConnection4()throws Exception{
			
		//1、提供另外三个连接的基本信息:
		String url="jdbc:mysql://localhost:3308/student";
		String user="root";
		String password="123456";
			
		//2、加载驱动
		Class.forName("com.mysql.jdbc.Driver");
			
		//3、获取连接
		Connection conn = DriverManager.getConnection(url,user,password);
		System.out.println(conn);
	}
	
	//方式五:将数据库连接需要的基本信息声明在配置文件中
	/**
	 * 好处:实现了数据与代码的分离,实现了解耦;
	 * 		如果需要修改配置文件信息,可以避免程序重新打包。
	 * */
	@Test
	public void testConnection5()throws Exception{
		//1、读取配置文件中的基本信息
		InputStream is =ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
		Properties pros =new Properties();
		pros.load(is);
		String user = pros.getProperty("user");
		String url = pros.getProperty("url");
		String password = pros.getProperty("password");
		//2、加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//3、获取链接
		Connection conn = DriverManager.getConnection(url,user,password);
		System.out.println(conn);
	}
	
	//在java.sql包中有3个接口分别定义了对数据库的调用的不同方式:
	/**
	 * Statement:用于执行静态SQL语句并返回它所生成结果的对象。(不常用了)
	 * PreparedStatement:SQL语句被预编译并存储在此对象中,可以使用此对象多次
	 * 					  高效地执行该语句。
	 * CallableStatement:用于执行SQL存储过程。
	 * 
	 * Statement弊端:需要拼写sql语句,并且存在SQL注入的问题。
	 * sql注入:用户名和密码输入错误也可登陆成功
	 * sql注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户
	 * 输入数据中注入非法的SQL语句段或命令,从而利用系统的SQL引擎完成恶意
	 * 行为的做法。
	 * 解决方案:用PreparedStatement取代Statement
	 * */
	 //(具体使用之后再说)
}

转载文章:blog.csdn.net/weixin_4470…

学习资料来源于:尚硅谷