JDBC
1. JDBC介紹
Java Database Connectivity
Java規定的數據庫連接接口,SUN公司提供的,爲了簡化程序員操作數據庫的過程。
SUN公司要求數據庫提供商,按照JDBC API接口規範,完成對應Java程序的數據連接操作,規範Jar包,並且提供對應的操作方法。
JDBC接口中核心的內容
java.sql.*
javax.sql.*
2. JDBC連接數據庫所需的必要條件
cmd > mysql -hlocalhost -uroot -p123456
1. 明確連接使用的數據庫是MySQL數據庫
2. 明確當前數據庫的主機地址,IP地址,域名,localhost表示本地
3. -uroot 用戶名
4. -p123456 對應用戶名密碼
JDBC連接數據庫也是需要這四個條件的
1. 確定連接的數據庫所在網絡地址和對應操作哪一個數據庫
這裏使用一個符合JDBC規範的URL
URL jdbc:mysql://localhost:3306/nzgp2001
2. 用戶名 user root
3. 密碼 password 123456
URL
含義
jdbc目前操作數據庫的主協議
mysql子協議
localhost 數據庫服務器所在的網路偶地址
3306 數據庫默認端口號
nzgp2001 當前URL連接操作對應數據庫是哪一個
JDBC是第三方提供的內容
獲取對應的Jar
mysql-connector-java-5.1.47.jar
從官網獲取對應的Jar包
mvnrepository Maven查詢網址
3. JDBC連接MySQL數據庫
3.1 操作流程
1. 導入Jar包
在項目根目錄創建lib目錄,放入對應jar包,引入依賴
2. 加載驅動
Java程序只是規定了接口規範,但是沒有實現
數據庫連接需要使用JDBC對應驅動
3. 準備必要參數連接數據庫
4. 獲取數據庫連接
5. 關閉資源
3.2 數據庫連接演示代碼
package com. qfedu. a_jdbc;
import java. sql. Connection;
import java. sql. DriverManager;
import java. sql. SQLException;
public class Demo1 {
public static void main ( String[ ] args) throws ClassNotFoundException, SQLException {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true" ;
String user = "root" ;
String password = "123456" ;
Connection connection = DriverManager. getConnection ( url, user, password) ;
System. out. println ( connection) ;
connection. close ( ) ;
}
}
3.3 數據庫驅動加載過程
public class Driver extends NonRegisteringDriver implements java. sql. Driver {
static {
try {
java. sql. DriverManager. registerDriver ( new Driver ( ) ) ;
} catch ( SQLException E) {
throw new RuntimeException ( "Can't register driver!" ) ;
}
}
public Driver ( ) throws SQLException {
}
}
4. JDBC核心API[能記住最好,記不住拉倒]
class java. sql. DriverManager 驅動管理類
-- | static java. sql. Connection getConnection ( String url, String user, String password) ;
interface java. sql. Connection 數據庫連接接口
-- | java. sql. Statement createStatement ( ) ;
-- | java. sql. PreparedStatement prepareStatement ( String sql) ;
interface java. sql. Statement 數據庫SQL語句搬運工對象
-- | int executeUpdate ( String sql) ;
-- | java. sql. ResultSet executeQuery ( String sql) ;
interface java. sql. PreparedStatement 數據庫SQL語句【預處理】搬運工對象
PreparedStatement extends java. sql. Statement
-- | int executeUpdate ( ) ;
-- | java. sql. ResultSet executeQuery ( ) ;
-- | setXXX ( int index, XXX value)
interface java. sql. ResultSet 數據庫結果集接口
-- | XXX getXXX ( int columnIndex) ;
-- | XXX getXXX ( String fieldName) ;
-- | boolean next ( ) ;
5. Statement操作SQL語句
5.1 Statement插入SQL數據操作
@Test
public void testInsert ( ) {
Statement statement = null;
Connection connection = null;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true" ;
String user = "root" ;
String password = "123456" ;
connection = DriverManager. getConnection ( url, user, password) ;
statement = connection. createStatement ( ) ;
String sql = "insert into nzgp2001.user(userName, password) value ('老黑', '123456')" ;
int affectedRows = statement. executeUpdate ( sql) ;
System. out. println ( "affectedRows:" + affectedRows) ;
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( statement != null) {
statement. close ( ) ;
}
if ( connection != null) {
connection. close ( ) ;
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
5.2 Statement修改SQL數據操作
@Test
public void testUpdate ( ) {
Statement statement = null;
Connection connection = null;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true" ;
String user = "root" ;
String password = "123456" ;
connection = DriverManager. getConnection ( url, user, password) ;
statement = connection. createStatement ( ) ;
String sql = "update nzgp2001.user set userName ='航海中路彭于晏' where id = 1" ;
int affectedRows = statement. executeUpdate ( sql) ;
System. out. println ( "affectedRows:" + affectedRows) ;
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( statement != null) {
statement. close ( ) ;
}
if ( connection != null) {
connection. close ( ) ;
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
5.3 Statement刪除SQL數據操作
@Test
public void testDelete ( ) {
Statement statement = null;
Connection connection = null;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true" ;
String user = "root" ;
String password = "123456" ;
connection = DriverManager. getConnection ( url, user, password) ;
statement = connection. createStatement ( ) ;
String sql = "delete from user where id > 2" ;
int affectedRows = statement. executeUpdate ( sql) ;
System. out. println ( "affectedRows:" + affectedRows) ;
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( statement != null) {
statement. close ( ) ;
}
if ( connection != null) {
connection. close ( ) ;
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}