Java的數據庫編程:JDBC

1.數據庫編程

MySQL是一個CS結構的系統。
在這裏插入圖片描述
我們這個數據庫編程呢,就是通過自己寫代碼實現一個MySQL客戶端,同樣是通過網絡和服務器進行交互。

客戶端不是很容易實現的。數據庫會給我們提供一組API(application programming interface),提供一組函數/類/方法,讓用戶去直接使用,來方便我們實現。

不同的數據庫提供的API都是差距比較大的,所以爲了解決這個問題,Java中引入JDBC,事Java自帶的一組數據庫操作的API,相當於涵蓋了各種數據操作的操作方式,把不同的數據庫的API統一到了一起。

不同類型的API可以通過適配器轉換爲JDBC統一的API,不用的數據庫需要提供不同的驅動程序去搭配到JDBC中。

再Java中驅動程序是一個獨立的jar包(很多.class文件的壓縮)

數據庫連接Connection

編程實現客戶端過程:
1.創建DataSource對象(準備工作)
2.基於DataSource對象,創建Connection對象,和數據庫建立連接(打開了客戶端,輸入了密碼,連接成功)
3.PrepareStatement對象拼裝具體的SQL語句(客戶端輸入SQL的過程)
4.拼裝好SQL後,需要執行SQL(客戶端按下回車,SQL就被髮到服務器了)
5.查看服務器返回結果(客戶端顯示結果)
6.關閉連接,釋放資源(退出客戶端)

JDBC提供了兩套API:
一種是通過DriverManager(驅動管理類)的靜態方法獲取

一種是通過DataSource(數據源)對象獲取。實際應用中會使用DataSource對象。

// 加載JDBC驅動程序 Class.forName(“com.mysql.jdbc.Driver”);
// 創建數據庫連接 Connection connection = DriverManager.getConnection(url);
DataSource ds = new MysqlDataSource();
((MysqlDataSource) ds).setUrl(“jdbc:mysql://localhost:3306/test”);
((MysqlDataSource) ds).setUser(“root”);
((MysqlDataSource) ds).setPassword(“root”);
Connection connection = ds.getConnection();

JDBC實現增刪改查

1》插入

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestJDBC {
    public static void main(String[] args) throws SQLException {
        //創建dataSource對象
        DataSource dataSource = new MysqlDataSource();
       //對dataSource進行一些配置:URL,User,Password需要向下轉型
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");

        //建立連接
        //connection對象生命週期較短,每個請求都需創建一個新的connection
        Connection connection = dataSource.getConnection();
        //拼裝sql語句PrepareStatement
        //以插入爲例
        int id = 1;
        String name = "曹操";
        int classId = 10;
        String sql = "insert into student values(?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        //1,2,3相當於剛纔問號的下標,從1開始
        statement.setInt(1,id);
        statement.setString(2,name);
        statement.setInt(3,classId);
        //執行sql
        //insert用executeUpdate
        //select 使用executeQuery來執行
        //返回值爲操作修改了多少行
        int ret = statement.executeUpdate();
        System.out.println("ret: "+ ret);
        //關閉釋放相關資源  後創建的被先釋放
        statement.close();
        connection.close();

    }
}

在這裏插入圖片描述
2》查詢

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class javaJDBCSelect {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");
        Connection connection = dataSource.getConnection();
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //ResultSet新的類,表示查找結果集
        ResultSet resultSet =  statement.executeQuery();
        //遍歷結果集(相當於一張表,裏面有很多行,每行又是一條記錄)
        //next()一方面判斷是否存在下一行,如果存在就獲取這一行
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int classId = resultSet.getInt("classId");
            System.out.println("id:" +id+"name:"+name+"classId: "+classId);

        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}

在這裏插入圖片描述
在這裏插入圖片描述
3》刪除

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCDelete {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入要刪除學生的姓名: ");
        String name = scanner.next();
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");
        Connection connection = dataSource.getConnection();
        String sql = "delete from student where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        int ret = statement.executeUpdate();
        if (ret == 1) {
            System.out.println("刪除成功");
        }else {
            System.out.println("刪除失敗");
        }
        statement.close();
        connection.close();
    }
}

在這裏插入圖片描述
在這裏插入圖片描述
4》更新

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCUpdate {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入要修改學生姓名id:");
        int id = scanner.nextInt();
        System.out.println("請輸入要修改學生的姓名: ");
        String name = scanner.next();
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");
        Connection connection = dataSource.getConnection();
        String sql = "update student set name =? where id =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);
        int ret = statement.executeUpdate();
        if (ret == 1) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失敗");
        }
        statement.close();
        connection.close();

    }
}

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章