08-JDBC

JDBC是一套标准,是Java与各大数据库厂商共同定制的一套接口. 这套接口由各大数据库厂商进行了实现.

1、使用步骤

 1. 引入jar文件.
 2. 加载数据库驱动 (JavaSE项目中可以省略 , JavaWeb项目必须编写此步骤)
		Class.forName("com.mysql.jdbc.Driver");
 3. 通过驱动管理器, 获取JDBC连接对象.
		Connection conn = DriverManager.getConnection("数据库连接地址","帐号","密码");
	// 数据库连接地址格式: 主协议:子协议://ip地址:端口号/数据库名称
	// mysql的连接地址: jdbc:mysql://localhost:3306/库名
	// oracle的连接地址: jdbc:oracle:thin:@localhost:1521:ORCL
 4. 通过连接对象, 创建SQL执行对象 (SQL执行环境)
		Statement state = conn.createStatement();
 5. 通过SQL执行对象 ,执行SQL语句.
		state.execute(String sql语句);
 6. 释放资源JDBC中常用的类型与方法
		state.close();
		conn.close();

2、JDBC中常用的类型和方法

 1. DriverManager : 驱动管理器
	获取数据库连接:
		static Connection getConnection(String 数据库地址,String 账号 ,String 密码)
		
 2. Connection : 数据库连接对象
	创建SQL执行对象: 
		Statement createStatement();
		
 3. Statement : SQL执行对象
	执行SQL语句(查询语句返回true, 其它语句返回false)
		boolean execute(String sql);
	执行DML语句(INSERT UPDATE DELETE) 和 DDL语句(create alter drop)
		(返回int值, 表示语句对数据库表格的影响行数 !)
		(通常我们认为 返回值>0 表示执行成功.)
		int executeUpdate(String sql);
	执行DQL语句 (select)
		ResultSet executeQuery(String sql);
		
 4. ResultSet : 结果集对象 (指的是一个select语句的查询结果)
	1. 控制游标移动的常用方法:
		boolean next() ****
		作用: 控制游标向下一行移动.
		返回值: 移动成功返回true , 下一行不存在移动失败, 返回false
		
	2. 获取游标指向行的字段值的常用方法:
		XXX getXXX(String 列名) ***
		根据字段名, 得到此字段的值
		XXX getXXX(int 字段的索引) *
		根据字段的索引, 得到字段的值 , 索引从1开始

3、使用预编译解决SQL注入问题

内部实现原理:
	1. 将未拼接参数的SQL语句, 作为SQL指令, 先传递给数据库 进行编译.
	2. 再将参数传递给数据库, 此时传递的参数不会再作为指令执行, 只会被当作文本存在.

操作流程与Statement基本一致:
 1. 如何得到一个PreparedStatement 对象
		PreparedStatement state = conn.prepareStatement("预编译的SQL语句");
 2. 预编译的SQL语句如何编写
		需要填充参数的位置, 使用?代替即可! 例如:
		select id from xzk_user where username=? and password=?
 3. 参数如何填充
		state.setXXX(int index,XXX value);
		setXXX中XXX指的是数据类型,
		参数1: index : SQL语句中?的索引值 , 从1开始
		参数2: value : 填充的参数值.
 4. 如何执行填充完毕参数的SQL
		boolean execute();
		int executeUpdate();
		ResultSet executeQuery();

4、批处理

将多条语句, 放到一起批量处理 .
批处理的原理: 将多条SQL语句, 转换为一个SQL指令. 显著的提高大量SQL语句执行时的数据库性能.

Statement对象使用流程:
 1. 得到Statement对象
		Statement state = conn.createStatement();
 2. 将一条SQL语句, 加入到批处理中.
		state.addBatch(String sql);
 3. 执行批处理
		state.executeBatch();
 4. 清空批处理
		state.clearBatch();

PreparedStatement对象使用流程:
 1. 得到PreparedStatement对象
		PreparedStatement state = conn.prepareStatement("预编译的SQL");
 2. 填充预编译的参数
		state.setXXX(1,填充参数);
 3. 将一条填充完毕参数的SQL, 加入到批处理中.
		state.addBatch();
 4. 执行批处理
		state.executeBatch();
 5. 清空批处理
		state.clearBatch();

5、Properties配置文件

Properties类 是Java中的Map集合的实现类.
	.properties文件 用于通过文件描述一组键值对!
	.properties文件 ,可以快速的转换为Properties类的对象.

文件中内容的格式:
	文件内容都是字符串 , 键与值之间通过等号连接 , 多个键值对之间换行分割.
	例如:
		url=xxx
		user=xxx
		password=xxx
如何将文件 转换为 集合:
步骤:
 1. 创建Properties对象
		Properties ppt = new Properties();
 2. 创建一个字节输入流 , 指向.properties文件
		InputStream is = new FileInputStream("文件地址");
 3. 将字节输入流, 传递给properties对象, 进行加载.
		ppt.load(is);

6、连接池

连接池用于缓存连接!
当我们需要使用连接时, 可以不用再创建连接 ! 可以直接从连接池中获取连接.
当连接池中存在空闲连接时, 会将空闲连接给到程序使用.
当连接池中不存在空闲连接时, 且连接池未满时 , 则创建连接提供给程序使用 ,并在程序使用完毕后,缓存连接.
当连接池中不存在空闲连接时, 且连接池已满时 , 则排队等候空闲连接的出现.
注意:
	使用连接池中的连接对象操作数据库时, 操作完毕依然需要释放连接(调用close()).
	连接池中的连接在设计时, 使用了动态代理设计模式+装饰者设计模式 . 我们调用它的close方法,
	代理没有关闭这个连接, 而是将连接重新放入了池中.
6.1、DBCP连接池
 1. 引入相关的jar文件
		dbcp.jar
		poll.jar
 2. 将配置文件引入
 3. 将配置文件, 转换为Properties对象
		Properties ppt = new Properties();
		ppt.load(配置文件的输入流);
 4. 通过连接池的工厂类(BasicDataSourceFactory)的创建连接池的方法(createDataSource())
		DataSource ds = BasicDataSourceFactory.createDataSource(ppt);
 5. 从连接池中 获取连接对象
		Connection conn = ds.getConnection();

工具类:
public class DBCPUtil{
	private static DataSource data = null;
	static {
		InputStream is =DBCPUtil.class.getClassLoader().getResourceAsStream("dbcp.properties");
		Properties ppt = new Properties();
		try {
			ppt.load(is);
			data = BasicDataSourceFactory.createDataSource(ppt);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/
	**
	* 用于从DBCP连接池中 获取一个连接
	* @return DBCP连接池中的一个连接对象.
	*/
	public static Connection getConnection() {
		try {
			return data.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	} 
	/
	**
	* 用于释放连接 , 执行环境 , 结果集 等资源
	* @param conn 要释放的连接资源
	* @param state 要释放的执行环境资源
	* @param result 要释放的结果集资源
	*/
	public static void close(Connection conn,Statement state,ResultSet result) {
		if(result != null) {
			try {
				result.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} 
		if(state != null) {
			try {
				state.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
6.2、Druid连接池
 1. 引入相关的jar文件
		druid-1.0.9.jar
 2. 将配置文件引入
 3. 将配置文件, 转换为Properties对象
		Properties ppt = new Properties();
		ppt.load(配置文件的输入流);
 4. 通过连接池的工厂类(DruidDataSourceFactory)的创建连接池的方法(createDataSource())
		DataSource ds = DruidDataSourceFactory.createDataSource(ppt);
 5. 从连接池中 获取连接对象
		Connection conn = ds.getConnection();

工具类:
public class DruidUtil{
	private static DataSource data = null;
	static {
		InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties");
		Properties ppt = new Properties();
		try {
			ppt.load(is);
			data = DruidDataSourceFactory.createDataSource(ppt);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/
	**
	* 用于从DBCP连接池中 获取一个连接
	* @return DBCP连接池中的一个连接对象.
	*/
	public static Connection getConnection() {
		try {
			return data.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	} 
	/
	**
	* 用于释放连接 , 执行环境 , 结果集 等资源
	* @param conn 要释放的连接资源
	* @param state 要释放的执行环境资源
	* @param result 要释放的结果集资源
	*/
	public static void close(Connection conn,Statement state,ResultSet result) {
		if(result != null) {
			try {
			result.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} 
		if(state != null) {
			try {
				state.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} 
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/rookie__zhou/article/details/111185800
08