JDBC訪問數據庫方法

JDBC訪問各種類型數據庫的前提:
由於不同數據庫產品的實現方式通信協議不一樣,所以各數據庫廠商以jar包(數據庫驅動)的形式提供實現類,jar包含有已經寫好的類和接口,只需要將其引入項目中就可直接使用。

jar包的引入:

  1. 首先要使用什麼數據庫就下載什麼數據庫的jar包,以MySQL舉例,直接進入MySQL官網下載即可https://dev.mysql.com/downloads/connector/j/
  2. 將下載好的jar包複製到項目文件夾中
  3. 在eclipse中右鍵點擊jar包選擇Build Path中的Add to Build Path就可以將其引入項目中了
    在這裏插入圖片描述

訪問流程:

  1. 註冊驅動:使用Class.forName()加載上一步引入的jar包中的驅動類,要用try包住。
    若出現報錯

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

表明數據庫驅動com.mysql.jdbc.Driver’已經被棄用了,應當使用新的驅動com.mysql.cj.jdbc.Driver’

try {
	Class.forName("com.mysql.cj.jdbc.Driver") ;//註冊驅動
} 
catch (ClassNotFoundException e) {}
  1. 獲取連接:DriverManager用於管理數據庫驅動,建立與數據庫的連接;Connection用於表示與數據庫的連接。
    格式:DriverManager.getConnection(“jdbc:mysql://IP地址:端口/數據庫名字”, 用戶名, 密碼) ,同樣要用try包住
    MySQL默認端口爲3306,若服務器在本機可用localhost或127.0.01
    java連接時如果出現異常

The server time zone value ‘�й���׼ʱ��’ is unrecogni

是因爲使用了Mysql Connector/J 6.x以上的版本,然後就報了時區的錯誤。加入參數serverTimezone=UTC就好了。

try {	//獲取連接
	Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8", "userName", "password")
} 
catch (SQLException e) {}
  1. 執行操作語句:Statement用於操作sql語句,首先要獲取Statement
Statement stmt = con.createStatement() ;

然後調用executeUpdate()方法,sql語句用字符串的形式作爲參數,該方法是int類型的,會返回一個受影響的行數相當於執行完插入語句返回的語句

String sql = "insert into t_user(username,password,age) values('aaa', '789', 22);" ;
stmt.executeUpdate(sql) ;

在這裏插入圖片描述
若使用sql語言出現中文亂碼,則要在連接數據庫的時候再添加參數useUnicode=true&characterEncoding=utf8
4. 處理結果:接上一步的executeUpdate()方法會返回一個值,可將其輸出查看結果
5. 關閉資源:訪問完數據庫之後不能一直佔用通道,需要將其關閉,關閉的順序要與打開的順序相反,先關Statement,再關Connection,也要用try包住

if(stmt != null) {	//先關Statement
	try {
		stmt.close() ;
	} 
	catch (SQLException e) {}
}
if(con != null) {	//再關Connection
	try {
		con.close() ;
	} 
	catch (SQLException e) {}
}

基本流程例子:

package mysql訪問;
import java.sql.*;
public class 操作流程 {
	public static void main(String[] args) {
		String driverClassName = "com.mysql.cj.jdbc.Driver" ;
		String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8" ;
		String userName = "root" ;
		String password = "不能給你們知道" ;
		Connection con = null ;
		Statement stmt = null ;
		try {
			//註冊驅動
			Class.forName(driverClassName) ;	
			//獲取連接
			con = DriverManager.getConnection(url, userName, password ) ;	
			//獲取Statement
			stmt = con.createStatement() ;
			//執行sql語句
			String sql = "insert into t_user(username,password,age) values('小明', '789', 22);" ;
			int num = stmt.executeUpdate(sql) ;
			//處理結果
			System.out.println(num) ;
		} 
		catch (ClassNotFoundException e) {System.out.println(e) ;}
		catch (SQLException e) {System.out.println(e) ;}
		finally {	//關閉資源
			if(stmt != null) {	//先關Statement
				try {
					stmt.close() ;
				} 
				catch (SQLException e) {}
			}
			if(con != null) {	//再關Connection
				try {
					con.close() ;
				} 
				catch (SQLException e) {}
			}
		}
	}
}

總結:

  • JDBC相關的類和接口都在java.sql中
作用
DriverManager 管理數據庫驅動
Connection 表示與數據庫的連接
Statement 執行SQL語
方法 作用
Class.forName() 加載類,可註冊驅動
DriverManager.getConnection() 建立與數據庫的連接
con.createStatement() 獲取Statement
stmt.executeUpdate() 進行sql操作
stmt.close() 關閉Statement
con.close() 關閉Connection
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章