如果idea連接資料庫失敗存在時區問題 , 在url路徑后面拼接一個引數?serverTimezone=GMT%2B8

初識jdbc
package com.starcpdk;
import java.sql.*;
public class Jdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1. 加載驅動
Class.forName("com.mysql.jdbc.Driver");
// 2. 用戶資訊和url
String url = "jdbc:mysql://localhost:3306/jdbc?userUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String password = "root";
// 3. 連接成功 , 資料庫物件
Connection connection = DriverManager.getConnection(url, username, password);
// 4. 執行sql的物件
Statement statement = connection.createStatement();
// 5. 執行sql物件 去執行sql , 可能存在結果 , 查看回傳的結果
String sql = "select * from user";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
}
// 6. 釋放連接
resultSet.close();
statement.close();
connection.close();
}
}
JDBC工具類簡單封裝
package com.starcpdk.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String driver = null;
private static String url = null;
private static String password = null;
private static String username = null;
static {
try {
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
// 1. 加載驅動 只加載一次
Class.forName(driver);
}catch (Exception e){
e.printStackTrace();
}
}
// 2. 獲取連接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url , username , password);
}
// 3.釋放連接資源
public static void release(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();
}
}
}
}
使用
package com.starcpdk;
import com.starcpdk.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertTest {
public static void main(String[] args) throws SQLException {
Connection connection = null;
Statement statement = null;
try {
connection = JDBCUtils.getConnection();
statement = connection.createStatement();
String sql = "insert into user(id , name , password)values(6 , 'zs' , '123456')";
int i = statement.executeUpdate(sql);
if (i > 0){
System.out.println("插入成功");
}
}catch (Exception e){
e.printStackTrace();
}finally {
JDBCUtils.release(connection , statement , null);
}
}
}
sql注入 傳入不合法的字串 ,導致sql被拼接 , 導致資料庫資料的泄露 , 因此不安全
防止sql注入的方式
package com.starcpdk;
import com.starcpdk.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PreparedQuery {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getConnection();
String sql = "select * from user where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1 , 3);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.release(connection , preparedStatement , resultSet);
}
}
}
事務ACID原則
原子性:要么全部完成 , 要么都不完成
一致性:總數不變
隔離性:多個行程互不干擾
持久性:一單提交 , 不可逆 , 持久化到資料庫
隔離性問題:
臟讀:一個事務讀取了另一個沒有提交的事務
不可重復讀:在同一個事務內 , 重復讀取表中的資料 , 表資料發生了改變
幻讀(虛讀):在一個事務內 , 讀取到了別人插入的資料 , 導致前后讀出來的結果不一致
package com.starcpdk;
import com.starcpdk.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class testTransaction {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = JDBCUtils.getConnection();
// 關閉資料庫的自動提交功能 , 自動開啟事務
connection.setAutoCommit(false);
// 寫sql
String sql1 = "update account set money = money-100 where name = 'A'";
statement = connection.prepareStatement(sql1);
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 e) {
try {
connection.rollback(); // 如果失敗則 , 回滾事務
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally {
JDBCUtils.release(connection , statement , rs);
}
}
}
資料庫連接池
資料庫連接 — 執行完畢 — 釋放資源
這個程序是非常浪費時間的
池化技術:預先準備一些資源 , 過來連接預先準備好的
DBCP工具類封裝
package com.starcpdk.utils;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBCPUtils {
private static DataSource dataSource = null;
static {
try {
InputStream is = com.starcpdk.utils.JDBCUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
Properties properties = new Properties();
properties.load(is);
// 創建資料源 工廠模式 -- 創建物件
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
// 2. 獲取連接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
// 3.釋放連接資源
public static void release(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();
}
}
}
}
dbcpconfig.properties組態檔內容
# 連接設定
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=true&?serverTimezone=GMT%2B8
username = root
password = root
# 初始化連接
initialSize = 20
#最大連接數
maxActive=50
# 最大空閑連接數
maxIdle=20
# 最小空閑連接數
minIdle=5
# 超時等待時間 , 以毫秒數為單位 ,
maxWait=60000
#JDBC驅動建立連接時附帶的連接屬性屬性的格式必須為這樣:[屬性名=property;]
#注意:“user” 與 “password” 兩個屬性會被明確地傳遞,因此這里不需要包含他們,
connectionProperties=useUnicode=true;characterEncoding=utf8
#指定由連接池所創建的連接的自動提交(auto-commit)狀態,
defaultAutoCommit=true
#driver default 指定由連接池所創建的連接的事務級別(TransactionIsolation),
#可用值為下列之一:(詳情可見javadoc,)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
C3P0
package com.starcpdk.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class C3P0Utils {
private static ComboPooledDataSource dataSource = null;
static {
try {
// 創建資料源
dataSource = new ComboPooledDataSource("MYSQL"); // 組態檔寫法
// 代碼實作方式
// dataSource = new ComboPooledDataSource();
// dataSource.setDriverClass();
// dataSource.setUser();
// dataSource.setPassword();
// dataSource.setJdbcUrl();
} catch (Exception e) {
e.printStackTrace();
}
}
// 2. 獲取連接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
// 3.釋放連接資源
public static void release(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();
}
}
}
}
c3p0-config.xml組態檔
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<!-- 如果要研究某個xml中可以設定哪些屬性,找相關類的 屬性 或者setXxx()-->
<property name="user">root</property>
<property name="password">root</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC</property>
<property name="checkoutTimeout">30000</property>
</default-config>
<named-config name="MYSQL">
<property name="user">root</property>
<property name="password">root</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC</property>
<property name="checkoutTimeout">20000</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</named-config>
</c3p0-config>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/159607.html
標籤:其他
