day28 JDBC(过于抽象-列举部分代码)

Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 1 加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 2 创建数据库连接对象
			conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/_06_", "root", "root");
			// 3 语句传输对象
			stmt = conn.createStatement();
			String sql = "insert into test_jdbc (id,name,money) values (4,'小小',999.9)";
			// sql = "update test_jdbc set money=money+1000 where id=1 ";
			// sql = "delete from test_jdbc where id = 1";
			// 如果是查询,就返回true,不是就返回false,价值不大,所以用的不多,添加,删除,更新都可以用这个方法
			// stmt.execute(sql);
			// 返回值是int,返回影响了几条数据(更改了几条/删除了几条/添加了几条),添加,删除,更新都可以用这个方法
			int count = stmt.executeUpdate(sql);
			System.out.println("影响了 " + count + " 条数据");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭资源,从上到下依次关闭,后打开的先关闭
				if (rs != null) {
					rs.close();
				}
				if (stmt != null) {
					stmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

PreparedStatement

添加或者更新的时候,尽量使用 PreparedStatement ,而不是使用Statement
Statement 和 PreparedStatement 的区别

Statement用于执行静态SQL语句,在执行的时候,必须指定一个事先准备好的SQL语句,并且相对不安全,会有SQL注入的风险

PreparedStatement是预编译的SQL语句对象,sql语句被预编译并保存在对象中, 被封装的sql语句中可以使用动态包含的参数 ? ,
在执行的时候,可以为?传递参数

使用PreparedStatement对象执行sql的时候,sql被数据库进行预编译和预解析,然后被放到缓冲区,
每当执行同一个PreparedStatement对象时,他就会被解析一次,但不会被再次编译 可以重复使用,可以减少编译次数,提高数据库性能
并且能够避免SQL注入,相对安全(把’ 单引号 使用 \ 转义,避免SQL注入 )

封装工具类

创建链接这些可以这样进行优化
	public static Connection getConnection() throws ClassNotFoundException,
			SQLException {
		String username = "root";
		String password = "root";
		String url = "jdbc:mysql://127.0.0.1:3306/_06_";

		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = DriverManager.getConnection(url, username,
				password);

		return connection;
	}

关闭资源这些可以这样进行优化
因为Connection和Statement/PreparedStatement以及ResultSet都实现了AutoCloseable接口
所以我们可以直接写AutoCloseable

	public static void close(AutoCloseable obj) {
		if (obj != null) {
			try {
				obj.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

Batch多语句操作

Statement实现

	Connection conn = null;
		Statement stmt = null;
		try {
			conn = DBUtil.getConnection();
			stmt = conn.createStatement();
			stmt.addBatch("insert into test_jdbc (id,name,money) values(21,'stmt多条测试1',99.12)");
			stmt.addBatch("insert into test_jdbc (id,name,money) values(22,'stmt多条测试2',99.22)");
			stmt.addBatch("insert into test_jdbc (id,name,money) values(23,'stmt多条测试3',99.32)");
			stmt.addBatch("insert into test_jdbc (id,name,money) values(24,'stmt多条测试4',99.42)");
			stmt.executeBatch();
			System.out.println("执行成功");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(stmt);
			DBUtil.close(conn);
		}

PreparedStatement实现

Connection conn = null;
		PreparedStatement prst = null;
		try {
			conn = DBUtil.getConnection();
			String sql = "insert into test_jdbc (id,name,money) values(?,?,?)";
			prst = conn.prepareStatement(sql);
			prst.setInt(1, 31);
			prst.setString(2, "prst多条测试1");
			prst.setDouble(3, 11.1);
			prst.addBatch();
			prst.setInt(1, 32);
			prst.setString(2, "prst多条测试2");
			prst.setDouble(3, 21.1);
			prst.addBatch();
			prst.setInt(1, 33);
			prst.setString(2, "prst多条测试3");
			prst.setDouble(3, 31.1);
			prst.addBatch();
			prst.executeBatch();
			System.out.println("执行成功");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(prst);
			DBUtil.close(conn);
		}

事务机制管理

Transaction事务机制管理
默认情况下,是执行一条SQL语句就保存一次,那么比如我需要 有三条数据同时成功同时失败,这个时候就需要开启事务机制了
如果开启事务机制,执行中发生问题,会回滚到没有操作之前,相当于什么也没有发生过

	Connection conn = null;
		PreparedStatement prst = null;
		Statement stmt = null;
		try {
			conn = DBUtil.getConnection();
			// 取消自动提交
			conn.setAutoCommit(false);
			String sql = "insert into test_jdbc (id,name,money) values(?,?,?)";
			prst = conn.prepareStatement(sql);
			prst.setInt(1, 31);
			prst.setString(2, "事务测试1");
			prst.setDouble(3, 11.1);
			prst.addBatch();
			prst.setInt(1, 32);
			// 这里故意写成1,让报错
			prst.setString(1, "事务测试2");
			prst.setDouble(3, 21.1);
			prst.addBatch();
			prst.setInt(1, 33);
			prst.setString(2, "事务测试3");
			prst.setDouble(3, 31.1);
			prst.addBatch();
			prst.executeBatch();
			// 提交
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				// 事务回滚
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		
		} finally {
			DBUtil.close(stmt);
			DBUtil.close(prst);
			DBUtil.close(conn);
		}

SQL注入

所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。具体来说,它是利用现有应用程序,将(恶意的)SQL命令注入到后台数据 库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。

Properties优化硬代码

dbutil

public static Connection getConnection() throws ClassNotFoundException,
			SQLException, IOException {

		Properties properties = new Properties();
		// getClassLoader : 获取类加载器
		// getResourceAsStream : 把资源转换为流
		// 这里 定位到src.所以可以直接写文件名,而流是定位到项目
		properties.load(DBUtil.class.getClassLoader().getResourceAsStream(
				"jdbc.properties"));
		// properties 本质就是一个map,可以通过key获取value
		// username=root 读取到之后,以 = 分隔为数组,把username作为key,root作为value保存
		// 所以我们就可以通过username 来获取root
		String driver = properties.getProperty("driver");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		String url = properties.getProperty("url");

		Class.forName(driver);
		Connection connection = DriverManager.getConnection(url, username,
				password);

		return connection;
	}

优化更改后
PropertiesUtil

public class PropertiesUtil {
	private static Properties jdbcProperties = null;

	private PropertiesUtil() {
	}

	public static Properties getProperties() {
		try {
			if (jdbcProperties == null) {
				jdbcProperties = new Properties();
				jdbcProperties.load(PropertiesUtil.class.getClassLoader()
						.getResourceAsStream("jdbc.properties"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jdbcProperties;
	}
}

DButil

public static Connection getConnection() throws ClassNotFoundException,
			SQLException, IOException {

		Properties properties = PropertiesUtil.getProperties();

		String driver = properties.getProperty("driver");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		String url = properties.getProperty("url");

		Class.forName(driver);
		Connection connection = DriverManager.getConnection(url, username,
				password);

		return connection;
	}

连接池

数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;
释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。
这项技术能明显提高对数据库操作的性能
优点 :
1.资源复用 : 数据库连接得到重用,避免了频繁创建释放链接引起的大量性能开销
在减少系统消耗的基础上,也增进了系统运行环境的平稳性
2.更快的系统响应速度 : 数据库连接池在初始化过程中,往往就已经创建了若干个数据库连接对象放到池中备用
这时,连接的初始化工作已完成,对于业务请求处理而言,直接利用现有的可用连接,避免了数据库连接初始化和释放过程的时间,从而缩减了系统整体的响应时间
3.统一的连接管理,避免数据库连接遗漏 : 在较为完备数据库连接池中,可以根据预先的连接占用超时设定,强制回收占用连接,从而避免了常规数据库连接操作中可能出现的资源泄露情况
导入jar包
连接池工具类

public class BasicDataSourceUtil {
	private BasicDataSource bds;
	private static BasicDataSourceUtil bdsu;

	/**
	 * 把初始化数据库链接的代码,放到了 单例模式的构造方法中
	 * 
	 * 因为当前类是单例的,所以构造方法只能被执行一次,那么链接池对象也就成了单例
	 */
	private BasicDataSourceUtil() {
		bds = new BasicDataSource();
		Properties properties = PropertiesUtil.getProperties();
		bds.setDriverClassName(properties.getProperty("driver"));
		bds.setUrl(properties.getProperty("url"));
		bds.setUsername(properties.getProperty("username"));
		bds.setPassword(properties.getProperty("password"));
	}

	/**
	 * 主要为了解决高并发,所以需要线程安全的单例模式
	 * 
	 * @return
	 */
	public static BasicDataSourceUtil getInstance() {
		if (bdsu == null) {
			synchronized (BasicDataSourceUtil.class) {
				if (bdsu == null) {
					bdsu = new BasicDataSourceUtil();
				}
			}
		}
		return bdsu;
	}

	/**
	 * 提供一个获取链接对象的方法
	 * 
	 * @return
	 */
	public BasicDataSource getBasicDataSource() {
		return bds;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值