1.JDBC基本介绍
1.概述

2.JDBC原理图

2.JDBC快速入门
1.JDBC API

2.JDBC程序编写步骤

3.环境配置
1.创建src/lib文件夹,放入jar包

2.加入到项目中

3.配置代码提示

4.代码实例

package jdbc_;
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Jdbc01 {
public static void main(String[] args) throws SQLException {
Driver driver = new Driver();
String url= "jdbc:mysql://localhost:3306/hsp_db02";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "root");
Connection connect = driver.connect(url, properties);
String sql = "insert into actor values(null, '刘德华', '男', '1970-1-1', '110')";
Statement statement = connect.createStatement();
int i = statement.executeUpdate(sql);
System.out.println(i > 0 ? "成功" : "失败");
statement.close();
connect.close();
}
}
3.数据库五种连接方式(推荐使用4、5)
1.代码实例
package jdbc_;
import com.mysql.cj.jdbc.Driver;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcConnect {
public static void main(String[] args) throws SQLException {
Driver driver = new Driver();
String url= "jdbc:mysql://localhost:3306/hsp_db02";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "root");
Connection connect = driver.connect(url, properties);
System.out.println("方式一:" + connect);
connect.close();
}
@Test
public void method02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url= "jdbc:mysql://localhost:3306/hsp_db02";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "root");
Connection connect = driver.connect(url, properties);
System.out.println("方式二:" + connect);
connect.close();
}
@Test
public void method03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url= "jdbc:mysql://localhost:3306/hsp_db02";
String user= "root";
String password= "root";
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式三:" + connection);
connection.close();
}
@Test
public void method04() throws ClassNotFoundException, SQLException {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
String url= "jdbc:mysql://localhost:3306/hsp_db02";
String user= "root";
String password= "root";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式四:" + connection);
connection.close();
}
@Test
public void method05() throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
Class<?> aClass = Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式五:" + connection);
connection.close();
}
}
2.细节说明
1.第四种方式是自动加载驱动原因
- Driver类加载的时候有个静态代码块
- 这个静态代码块会自动创建一个驱动实例并且注册

2.可不可以不写类加载语句?
- 答案是可以的
- 原因是在mysql的驱动4以后是自动加载的
- mysql4以后自带配置文件会自动加载驱动并注册

3.方式五的连接方式(建议使用)

1.src下(或者任意位置)创建配置文件(自定义名字)
src\mysql.properties
user=root
password=root
url=jdbc:mysql://localhost:3306/hsp_db02
driver=com.mysql.cj.jdbc.Driver
注意这个文件不能加空格
2.获取连接
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
Class<?> aClass = Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
4.课堂练习
题目

答案
package jdbc_;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class ConnectExercise {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String driver = properties.getProperty("driver");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class<?> aClass = Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "delete from actor where id = 1";
Statement statement = connection.createStatement();
int i = statement.executeUpdate(sql);
System.out.println(i > 0 ? "成功" : "失败");
statement.close();
connection.close();
}
}
4.ResultSet查询
1.基本介绍


2.ResultSet查询表中所有数据
package jdbc_;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class ResultSet_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String driver = properties.getProperty("driver");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class<?> aClass = Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "select * from actor ";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int anInt = resultSet.getInt(1);
String string = resultSet.getString(2);
String string1 = resultSet.getString(3);
Date date = resultSet.getDate(4);
String string2 = resultSet.getString(5);
System.out.println(anInt + " " + string + " " + string1 + " " + date + " " + string2);
}
resultSet.close();
statement.close();
connection.close();
}
}

3.SQL注入

5.预处理
1.基本介绍


2.预处理查询
package jdbc_;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class PreparedStatement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String driver = properties.getProperty("driver");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class<?> aClass = Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "select id, name from actor where id = ? and name = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 4);
preparedStatement.setString(2, "孙显圣");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int anInt = resultSet.getInt(1);
String string = resultSet.getString(2);
System.out.println(anInt + " " + string);
}
resultSet.close();
preparedStatement.close();
connection.close();
}
}
3.预处理DML
package jdbc_;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class PreparedStatement_01 {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String driver = properties.getProperty("driver");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class<?> aClass = Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "update actor set name = ? where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "孙显圣");
preparedStatement.setInt(2, 2);
int i = preparedStatement.executeUpdate();
System.out.println(i > 0 ? "成功" : "失败");
preparedStatement.close();
connection.close();
}
}
4.课堂练习

package jdbc_;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class PreStatementExercise {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String driver = properties.getProperty("driver");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "select * from actor";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int anInt = resultSet.getInt(1);
String string = resultSet.getString(2);
String string1 = resultSet.getString(3);
Date date = resultSet.getDate(4);
String string2 = resultSet.getString(5);
System.out.println(anInt + " " + string + " " + string1 + " " + date + " " + string2);
}
resultSet.close();
preparedStatement.close();
connection.close();
}
}
6.JDBC_API


7.JDBCUtils

1.JDBCUtils工具类代码
package utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String user;
private static String password;
private static String url;
private static String driver;
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\mysql.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
user = properties.getProperty("user");
password = properties.getProperty("password");
url = properties.getProperty("url");
driver = properties.getProperty("driver");
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
2.JDBCUtils_DML演示
package jdbc_;
import org.junit.jupiter.api.Test;
import utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCUtils_Use {
@Test
public void testDML() {
Connection connection = JDBCUtils.getConnection();
String sql = "update actor set name = ? where id = ?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "刘德华");
preparedStatement.setInt(2, 6);
int i = preparedStatement.executeUpdate();
System.out.println(i > 0 ? "成功" : "失败");
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, preparedStatement, connection);
}
}
}
3.JDBCUtils_select演示
package jdbc_;
import org.junit.jupiter.api.Test;
import utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtils_Use {
@Test
public void testSelect() {
Connection connection = JDBCUtils.getConnection();
String sql = "select name, phone from actor where id = ?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 8);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String string = resultSet.getString("name");
String string1 = resultSet.getString("phone");
System.out.println(string + " " + string1);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(resultSet, preparedStatement, connection);
}
}
}
8.事务
1.基本介绍

2.案例——使用事务处理银行转账
1.创建表account
CREATE TABLE account(
id INT PRIMARY key auto_increment,
name VARCHAR(32) NOT NULL DEFAULT'',
blance DOUBLE NOT NULL DEFAULT 0
)
INSERT INTO account VALUES(null, '马云', 3000);
INSERT INTO account VALUES(null, '马化腾', 10000);
SELECT * FROM account
2.编写代码
package jdbc_;
import utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Transaciton {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatement1 = null;
try {
connection = JDBCUtils.getConnection();
connection.setAutoCommit(false);
String sql1 = "update account set blance = blance - 100 where name = ?";
String sql2 = "update account set blance = blance + 100 where name = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setString(1, "马云");
preparedStatement1 = connection.prepareStatement(sql2);
preparedStatement1.setString(1, "马化腾");
preparedStatement.executeUpdate();
preparedStatement1.executeUpdate();
connection.commit();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, preparedStatement, connection);
JDBCUtils.close(null, preparedStatement1, null);
}
}
}