一、JDBC是什么
JDBC:java database connectivity
二、JDBC本質
JDBC 是sun公司提供的一套接口
接口都有調用者和實現者,面向接口調用,面向接口寫實現類,都屬於面向接口編程
為什么要面向接口編程:
為了解耦合,降低程序的耦合度,提高程序的擴展力。
比如:多態就是典型的面向抽象編程
Animal a = new Dog();
三、JDBC開發前的准備
先從對應的官網中下載對應的驅動jar包,配置到IDEA中
配置的截圖
四、JDBC開發六步
1.注冊驅動:告訴Java程序將要連接的是哪個品牌的數據庫
2.獲取連接:表示jvm和數據庫之間的進程通道打開了,這屬於進程之間的通信,使用之后一定要關閉
3.獲取數據庫操作對象(執行sql語句的對象)
4.執行sql語句
5.處理查詢結果集:只有第四步執行的是select語句時才有第五步
6.釋放資源:java程序和數據庫是進程之間的通信,使用之后一定要關閉
// 示例1:sql語句是insert,所有沒有上述第五步處理查詢結果集
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTest01 {
public static void main(String[] args) {
//1.注冊驅動,這里目的要把class文件加載一下,后續還有另外一種方式實現
java.sql.Driver driver = null;
// 寫在這里是因為下邊finally要用到,所以聲明為全局的
Connection con = null;
Statement state = null;
try {
driver = new Driver(); //多態,父類型引用指向子類對象;java.sql.Driver是接口,jdbc中的Driver是實現類
DriverManager.registerDriver(driver);
//2.獲取連接
String url = "jdbc:mysql://127.0.0.1/firstbase";
String userName = "root";
String password = "123456";
con = DriverManager.getConnection(url,userName,password);
System.out.println("數據庫連接對象是:" + con);
//3.獲取數據庫操作對象(專門執行sql語句的對象)
state = con.createStatement();
//4.執行sql語句
String sql = "insert into dept(deptno, dname, loc) values(60, 'SALES', 'NEWYORK')";
int count = state.executeUpdate(sql);
System.out.println(count == 1 ? "保存成功" : "保存失敗");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//5.釋放資源,為了保證資源一定釋放,在finally語句中關閉資源,並且遵循從小到大依次關閉,分別try catch
if (state != null){
try {
state.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null){
try{
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
// 示例2:delete 和 update
import java.sql.*;
public class JdbcTest02 {
public static void main(String[] args) {
Driver driver = null;
Connection con = null;
Statement state = null;
try {
//1.注冊驅動
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//2.獲取連接
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/firstbase","root","123456");
//3.獲取數據庫執行對象
state = con.createStatement();
//4.執行sql語句;jdbc中sql語句不需要寫分號
// String sql = "delete from dept where deptno = 50 "; //刪除語句
String sql = "update dept set dname = 'zhangsan' where deptno = 10 "; // 修改語句
int count = state.executeUpdate(sql);
System.out.println(count == 1 ? "刪除成功" : "刪除失敗");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (state != null){
try {
state.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
//示例3:注冊驅動的另一種常用的寫法
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcTest03 {
public static void main(String[] args) {
//1.注冊驅動的另外一種方式(比較常用)
try {
//這里不需要接收返回值,因為只需要這個加載的動作,這個在源碼中有靜態代碼塊,只需要加載就執行了
//這種方式常用是因為接收的是一個字符串,可以寫到配置文件.properities中
Class.forName("com.mysql.jdbc.Driver");
//2.獲取連接
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/firstbase","root","123456");
System.out.println(con); //這里打印出con對象,表示獲取連接成功
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
//實例4:使用資源綁定器綁定屬性文件
import java.util.ResourceBundle;
import java.sql.*;
public class JdbcTest04 {
public static void main(String[] args) {
//使用資源綁定器綁定屬性配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String jdbc = bundle.getString("jdc");
String url = bundle.getString("url");
String userName = bundle.getString("userName");
String password = bundle.getString("password");
Connection con = null;
Statement state = null;
try {
//1.注冊驅動
Class.forName(jdbc);
//2.獲取連接
con = DriverManager.getConnection(url,userName,password);
//3.獲取數據庫操作對象
state = con.createStatement();
//4.執行sql語句
String sql = "insert into dept(deptno, dname, loc) values(60, 'SALES', 'NEWYORK')";
int count = state.executeUpdate(sql);
System.out.println(count == 1 ? "保存成功" : "保存失敗");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (state != null){
try {
state.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
// 實例5:處理查詢結果集
import java.sql.*;
public class JdbcTest05 {
public static void main(String[] args) {
Connection con = null;
Statement state = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/firstbase", "root", "123456");
state = con.createStatement();
String sql = "select * from dept";
rs = state.executeQuery(sql); //這里rs就是查詢結果集
while(rs.next()) { //rs.next()返回布爾類型,如果rs一下行有數據返回true,否則返回false
String deptno = rs.getString("deptno"); //rs.getString()參數可以是數字1,2,3表示第幾列,或者是傳字段名
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.println(deptno + "\t" + dname + "\t" + loc);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
if (rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (state != null){
try {
state.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}