在加载时静态代码块可以自动执行代码块中的代码
事务
如果一个包含多个业务操作,被事务管理,那么这些操作同时成功或者同时失败
事务的三大操作:
1、开启事务:start transaction
2、回滚:rollback
3、提交:commit
MySQL数据库中事务默认自动提交
每执行一条SQL语句都会自动提交,把数据持久化保存在表中,如果不是自动提交事务的话,每次操作数据只是临时的操作。当重新打开数据库时之前的操作的数据会丢失
查看事务的默认提交方式:SELECT @@autocommit; – 1 代表自动提交 0 代表手动提交
事务的四大特征(面试可能会考)
1、原子性:是不可分割的最小操作单位,要么同时成功,要么同时失败
2、持久性:当事务提交或回滚后,数据库 会持久化的保存数据
3、隔离性:多个事务之间,相互独立
4、一致性:事务操作前后,数据总量不变
JDBC:
什么是JDBC?JDBC本质,其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。我们只需要使用不同的jar包中的对象进行操作即可
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBC {
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/learning?useSSL=false", "root", "123456");
Statement statement = con.createStatement();
int i = statement.executeUpdate("update Course set t_id = 06 where c_id = 01");
con.close();
}
}
//以上就是通过JDBC对数据库的进行一个修改操作
1、DriverManager
功能:注册驱动:告诉程序该使用哪一个数据库驱动jar
Class.forName("com.mysql.jdbc.Driver")
获取数据库连接:通过getConnection(url,user,password)方法
2、Connection
连接数据的桥梁对象,数据库连接对象
功能:获取sql语句
Statement createStatement()
PreparedStatement prepareStatement(String sql)
二者的区别在于后者可以防止漏洞注入
管理事务:开启事务,setAutoCommit(boolean autoCommit) :调用该方法设置参数为false,即开启事务
提交事务:commit()
回滚事务:rollback()
3、Statement
Statement是用于执行静态sql的执行sql对象,还有一个就是prepareStatement用于执行预编译sql对象,
静态sql就是sql语句不变的,预编译就是sql是动态的,带有?符号的。
4、ResultSet
该对象是在executeQuery(String sql)方法执行查询sql方法执行查询sql语句的时候会返回结果集对象
import java.sql.*;
import java.sql.*;
//把数据呈现在控制台上
public class Demo3 {
public static void main(String[] args) {
Connection con=null;
ResultSet resultSet=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/learning?useSSL=false", "root", "123456");
Statement statement = con.createStatement();
resultSet = statement.executeQuery("select * from Course");
//sout("---------------------------------")
while (resultSet.next()){
int c_id = resultSet.getInt("c_id");
String c_name = resultSet.getString("c_name");
int t_id = resultSet.getInt("t_id");
System.out.println(c_id+"\t"+c_name+"\t"+t_id);
}
//sout("---------------------------------")
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if (con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
抽取JDBC工具类:JDBCUtils
我们发现之前写的代码会有一些问题,每一次在进行操作数据的时候都需要重复的注册驱动,获取连接。我们可以直接抽取一个工具类帮助我们来完成这些操作,简化代码!
/*
* 获取连接
* 释放资源
* */
import java.sql.*;
public class DBUtils {
//1、获取连接
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/learning?useSSL=false", "root", "123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
//2、释放资源
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
if (resultSet == null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
读取配置文件方式
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/learning?useSSL=false
username=root
password=123456
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtils2 {
//获取driver = com.mysql.jdbc.Driver
private static final Properties properties = new Properties();//存储配置文件的map
static {
//获得一个字节流
InputStream is = DBUtils2.class.getResourceAsStream("/db.properties");//properties的文件名
try {
properties.load(is);//通过流,将配置文件内容加载到properties集合
Class.forName(properties.getProperty("driver"));//通过键拿值
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取url = jdbc:mysql://localhost:3306/learning
//username = root
//password = li1579026891
public static Connection getConnection(){
Connection connection = null;
try {
connection = DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("username"), properties.getProperty("password"));
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//关闭
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
if (resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
//测试
public class Demo4 {
public static void main(String[] args) {
Connection connection = DBUtils2.getConnection();
System.out.println(connection);
}
}
数据库连接池
其实就是一个容器(集合),存放数据库连接(connection)的容器。当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器。
好处:节约资源、用户访问高效