目錄
一. 具體操作如下
1.註冊驅動
二.實操
JDBC(Java DataBase Connectivity)java 數據庫連接,是 JavaEE 平台下的技術規範,其定義了在 Java 語言中連接數據,執行 SQL 語句的標準,可以為多種關係數據庫提供統一訪問。
一. 具體操作如下
1.註冊驅動
Class.forName("com.mysql.cj.jdbc.Driver");
2.獲取連接
DriverManager(驅動管理類)功能如下: 1. 註冊驅動 2. 獲取數據庫連接
法1: 註冊與給定的驅動程序 DriverManager 。 public static void registerDriver(Driver driver) throws SQLException
式2:根據url、數據庫登錄的户名、密碼獲取個數據庫的連接對象。
public static Connection getConnection(String url, String user, Stri ng password)
參數説明:
1.url : 連接路徑
語法:jdbc:mysql://ip地址(域名):端號/數據庫名稱參數鍵值對1&參數鍵值對2…
示例:jdbc:mysql://127.0.0.1:3306/db1
1).如果連接的是本機mysql服務器,並且mysql服務默認端是3306,則url可以簡寫為:jdbc:mysql:///數據庫名稱參數鍵值對
(2)JDBC配置 useSSL=false 使户賬號密碼進連接,useSSL=true:般通過證書或者令牌進安全驗證。
(3)JDBC配置useTimezone=true和serverTimezone=GMT%2B8的的是為了解決時區 設置問題,確保Java應程序與MySQL數據庫之間的時間同步。
2.user :户名
3.password :密碼
4.Connection 數據庫連接對象
功能: 1. 獲取執 SQL 的對象 2. 管理
String url = "jdbc:mysql://127.0.0.1:3306/數據庫名? charset=utf8mb4&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8";
String user = "數據庫賬户";
String password = "數據庫密碼";
Connection conn = DriverManager.getConnection(url, user, password);
3.定義sql
String sql = "sql語句";
4.獲取sql對象
Connetction類中重要的成員法包括:
1. 普通執SQL對象: Statement createStatement()法
2. 創建PreparedStatement類的實例: PreparedStatement prepareStatement(sql)法 預編譯SQL的執SQL對象, 通過這種式獲取的 PreparedStatement SQL語句執對象是我們 會重點要進講解的,它可以防SQL注。
Statement sta = conn.createStatement();
二.實操
1.DriverManager
import java.sql.*;
import java.text.MessageFormat;
public class DEMO1_DriverManager {
public static void main(String[] args) {
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
//1.加載數據庫廠商提供的驅動
Class.forName("com.mysql.cj.jdbc.Driver");//指定路徑
//2.獲取數據庫的連接 固定寫法 IP+端口號 數據庫 字符集編碼
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java114?characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false",
"root","hhj048482");//通過實現類來以獲取數據庫連接Connnection是Java中的類
//3.創建Statement對象
statement = connection.createStatement();
//4.定義SQL語句
String sql="select id,name,sno,age,gender,enroll_date,class_id from student";
//5.執行SQL語句
resultSet = statement.executeQuery(sql);//執行查詢
//statement.executeUpdate();//執行更新執行
//6.遍歷結果集,獲取數據行
while (resultSet.next()) {
//獲取ID列的值
long aLong = resultSet.getLong(1);
//resultSet.getLong("id");
String string = resultSet.getString(2);
String string1 = resultSet.getString(3);
int anInt = resultSet.getInt(4);
Date date = resultSet.getDate(5);
long aLong1 = resultSet.getLong(7);
System.out.println(MessageFormat.format("學生編號={0},姓名={1},學號{2},年齡{3},性別={4},入學時間={5},班級編號={6}"
,aLong,string,string1,anInt,date,aLong1));
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
//釋放結果集對象
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.DataSource
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtiL {
//數據源
private static DataSource dataSource=null;
//數據庫連接串
private static final String URL="jdbc:mysql://127.0.0.1:3306/java114?characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false";
//用户名
private static final String USER="root";
//密碼
private static final String PASSWORD="hhj048482";
//當類加載到JVM的時候,執行數據源的初始化
static {
MysqlDataSource mysqlDataSource=new MysqlDataSource();
mysqlDataSource.setURL(URL);
mysqlDataSource.setUser(USER);
mysqlDataSource.setPassword(PASSWORD);
dataSource = mysqlDataSource;
}
//構造方法私有化,防止new這個對象
private DBUtiL(){}
/**
* 獲取數據庫的連接
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void close (ResultSet resultSet, Statement statement, Connection connection) {
// 釋放結果集對象
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 釋放Statement
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 關閉數據庫連接
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}