JDBC
PrearedStatement对象
- PrearedStatement对象可以防止SQL注入,并且效率更高!
新增
package com.feng.lesson03;
import com.feng.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestInsert {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = JdbcUtils.getConnection();//联接数据库
//区别
//使用 ? 占位符
String sql = "INSERT INTO users(id,`name`,`password`,`email`,`birthday`)VALUES(?,?,?,?,?)";
statement = connection.prepareStatement(sql);//预编译SQL,先写SQL,然后不执行
//传入值
statement.setInt(1,6);
statement.setString(2,"wwf");
statement.setString(3,"123456");
statement.setString(4,"1324515@qq.com");
//setDate()里面需要一个sql.Date类型(数据库的日期类),要将java.util.Date类型转换为sql.Date类型
statement.setDate(5,new Date(new java.util.Date().getTime()));
//调用方法插入值
int i = statement.executeUpdate();
if (i>0){
System.out.println("插入成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if (statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
删除
package com.feng.lesson03;
import com.feng.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestDelete {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = JdbcUtils.getConnection();
String sql = "DELETE FROM users WHERE id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1,6);
int i = statement.executeUpdate();
if (i>0){
System.out.println("删除成功!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
更新
package com.feng.lesson03;
import com.feng.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestUpdate {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
//联接数据库
connection = JdbcUtils.getConnection();
//定义修改的sql语句
String sql = "UPDATE users SET `name`=?,`password`=? WHERE id=?";
//获取对像
statement = connection.prepareStatement(sql);
//数据
statement.setString(1,"safda");
statement.setString(2,"1008611");
statement.setInt(3,5);
//修改
int i = statement.executeUpdate();
if (i>0){
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally { //关闭
if (statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
查询
package com.feng.lesson03;
import com.feng.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestSelect {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
String sql = "SELECT * FROM users WHERE `name` = ? AND `password` = ?";
statement = connection.prepareStatement(sql);
statement.setString(1,"lisi");
statement.setString(2,"123456");
resultSet = statement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
System.out.println(resultSet.getString("password"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
防止SQL注入
package com.feng.lesson03;
import com.feng.lesson02.utils.JdbcUtils;
import java.sql.*;
public class SQL注入 {
public static void main(String[] args) {
login("haha","123456");
//login("'or '1=1","'or'1=1");//通过拼接字符串改变,sql语句
}
//登录业务
public static void login(String username,String password){
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;//查询
try {
connection = JdbcUtils.getConnection();
String sql = "SELECT * FROM users WHERE `name` = ? AND `password` = ?";
//PreparedStatement 防止SQL注入的本质,把传递进来的参数当作字符
//假设其中存在转义字符,就会直接转义,列如'' 就会直接转义,
statement = connection.prepareStatement(sql);
statement.setString(1,"lisi");
statement.setString(2,"123456");
resultSet = statement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
System.out.println(resultSet.getString("password"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.release(connection,statement,resultSet);
}
}
}
使用IDEA连接数据库
- 要在IDEA里先导入mysql-connector-java-5.1.47.jar的包,然后再连接。
- 连接成功后,可以选择数据库
- 双击数据库,可查看数据库中的内容。
- 更新,修改数据。
- 编写SQL语句
- 连接失败 ,查看原因。
事务
- 要么都成功要么都失败。
ACID原则
原子性:要么全都完成,要么都不完成。
一致性:总数不变。
隔离性:多个进程互不干扰。
持久性:一旦提交不可逆,持久化到数据库了。
隔离性的问题:
脏读:一个事务读取了另一个没有提交的事务。
不可重复读:在同一个事务内,重复的读取表中的数据,表中的数据发生了改变。
虚读(幻读):再一个事务内,读取到了别人插入的数据,导致前后读出来的结果不一致。
代码实现
- 开启事务:connection.setAutoCommit(false);
- 一组事务执行完毕,提交事务。
- 可以再catch语句中显示的定义 回滚语句,但它本身默认失败就是会自动回滚的。
package com.feng.lesson04;
import com.feng.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestTransaction1 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = JdbcUtils.getConnection();
//关闭自动提交,自动开启事务
connection.setAutoCommit(false);//开启事务
String sql = "update account set money = money-100 where name = 'A' ";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
String sql2 = "update account set money = money+100 where name = 'B' ";
statement = connection.prepareStatement(sql2);
statement.executeUpdate();
//提交事务
connection.commit();
System.out.println("提交成功");
} catch (SQLException throwables) {
//如果失败,是默认回滚的,可以不用显示的写
try {
connection.rollback();//如果失败就回滚事务
} catch (SQLException e) {
e.printStackTrace();
}
throwables.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
数据库连接池
-
数据库从连接 到 执行完毕 再到 释放。连接 到 释放 十分的浪费系统资源,频繁的这样做,是开销很大的。
-
池化技术:预先准备一些资源,有需要就连接预先准备好的资源,用完了再放回池中,也不用创建,也不用释放。
- 常用连接数 是10个
- 最小连接数就 是10个
- 最大连接数:15 业务最高承载上限。
- 超过15 个业务,就排队等待。
- 等待超时:等的太久了。
-
编写连接池,只需要实现 DataSource 接口
开源数据源实现(拿来即用)
DBCP
C3P0
Druid:阿里巴巴的
使用里这些数据库连接池之后,我们就不需要在项目开发中再编写连接数据库的代码了!
DBCP
需要用到的jar包
commons-dbcp-1.4.jar commons-pool-1.6.jar
C3P0
c3p0-0.9.5.5.jar mchange-commons-java-0.2.19.jar
结论
无论使用什么数据源,本质还是一样的,DataSource接口不会变,方法就不会变。