天天看點

JAVA單排日記-2020/3/14-JDBC-使用步驟詳解

public class DemoJDBC {
    public static void main(String[] args) throws Exception {
        //1.導入驅動jar包
        //2.注冊驅動
        Class.forName("com.mysql.jdbc.Driver");
        //3.擷取資料庫連接配接對象(連接配接到資料庫)
        Connection conn = DriverManager.getConnection("jdbc:masql://localhost:3306/db05", "root", "password");
        //4.定義sql語句
        String sql = "update account set balance = 500 where id =1";
        //5.擷取執行sql的對象
        Statement stmt = conn.createStatement();
        //6.執行sql
        int count = stmt.executeUpdate(sql);
        //7.處理結果
        System.out.println(count);
        //8.釋放資源
        stmt.close();
        conn.close();
    }
}
           

其中的對象:

  • DriverManager

    :驅動管理對象
  • Connection

    :資料庫連接配接對象
  • Statement

    :執行sql的對象
  • ResultSet

    :結果集對象
  • PreparedStatement

    :執行sql的對象

對象詳解:

1.

DriverManager

:驅動管理對象
  • 功能
  1. 注冊驅動:告訴程式該使用哪一個資料庫驅動jar包
  2. 擷取資料庫連接配接
url:

jdbc:mysql://ip:端口号/資料庫名稱

例:jdbc:mysql://localhost:3306/db05

如果是本地localhost:3306可以省略

jdbc:mysql:///db05

2.

Connection

:資料庫連接配接對象
  • 功能
  1. 擷取執行 sql 的對象
createStatement
PreparedStatement
           
  1. 管理事務
開啟事務:

setAutoCommit(false)

送出事務:

commit()

復原事務:

rollback()

3.

Statement

:執行sql的對象
  • 功能:執行sql
boolean execute(sql); 可以執行任意的sq1,了解
           
int executeUpdate(sql); 執行DML(insert,update,delete),DDL(create,alter,drop)
           
傳回值代表影響的行數
ResultSet executeQuery(sql) 執行DQL(select)
           
  • 練習
  • account表添加一條記錄
  • account表修改記錄
  • account表删除一條記錄
    JAVA單排日記-2020/3/14-JDBC-使用步驟詳解
package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

public class DemoAccount {
    public static void main(String[] args)  {
        Statement stamt=null;
        Connection conn=null;
        try {
            //1.導入驅動jar包
            //2.注冊驅動
            Class.forName("com.mysql.jdbc.Driver");
            //3.擷取資料庫連接配接對象(連接配接到資料庫)
            conn = DriverManager.getConnection("jdbc:mysql:///db05","root","root");
            //4.定義sql語句
            String sql01="insert into account(name,balance) values ('wangwu',1200)";
            String sql02="update account set balance = 1000 where name = 'zhangsan'";
            String sql03="delete from account where id=10";
            //5.擷取執行sql的對象
            stamt = conn.createStatement();
            //6.執行sql
            int[] arr=new int[3];
            arr[0] = stamt.executeUpdate(sql01);
            arr[1] = stamt.executeUpdate(sql02);
            arr[2] = stamt.executeUpdate(sql03);
            //7.處理結果
            System.out.println(Arrays.toString(arr));
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally { //8.釋放資源
            if (stamt!=null){
                try {
                    stamt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
           
JAVA單排日記-2020/3/14-JDBC-使用步驟詳解
4.

ResultSet

:結果集對象
  • 功能:将結果封裝,友善使用方法調用其中資料
  • 方法:
  • next()

    :遊标向下移動一行,從表頂部移到第一行資料
  • getxxx(參數)

    :擷取結果集中xxx類型的資料,
xxx:代表資料類型如:

int getInt()

string getstring()

參數:

1.int:代表列的編号,從1開始如:getstring(1)

2.string:代表列名稱。如:getDouble(“balance")

  • 練習:
    JAVA單排日記-2020/3/14-JDBC-使用步驟詳解
package JDBC;

import java.sql.*;
import java.util.Arrays;

public class DemoSetReuslt {
    public static void main(String[] args)  {
        Statement stamt=null;
        Connection conn=null;
        try {
            //1.導入驅動jar包
            //2.注冊驅動
            Class.forName("com.mysql.jdbc.Driver");
            //3.擷取資料庫連接配接對象(連接配接到資料庫)
            conn = DriverManager.getConnection("jdbc:mysql:///db05","root","root");
            //4.定義sql語句
            String sql = "select * from account";
            //5.擷取執行sql的對象
            stamt = conn.createStatement();
            //6.執行sql
            ResultSet res = stamt.executeQuery(sql);
            //7.處理結果
            res.next();//遊标下移到第一行
            String name = res.getString(2);//擷取第一行第二列
            int n = res.getInt("balance");//擷取第一行,列名為balance
            System.out.println("姓名:"+name+" 工資:"+n);

            res.next();//遊标下移到第2行
            String name2 = res.getString(2);//擷取第2行第二列
            int n2 = res.getInt(3);//擷取第2行,列名為balance
            System.out.println("姓名:"+name2+" 工資:"+n2);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally { //8.釋放資源
            if (stamt!=null){
                try {
                    stamt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
           
JAVA單排日記-2020/3/14-JDBC-使用步驟詳解
  • 注意:循環周遊
  1. 遊标向下移動一行
  2. 判斷是否有資料(有資料:

    next() =true

  3. 擷取資料
package JDBC;

import java.sql.*;
import java.util.Arrays;

public class DemoSetReuslt {
    public static void main(String[] args)  {
        Statement stamt=null;
        Connection conn=null;
        try {
            //1.導入驅動jar包
            //2.注冊驅動
            Class.forName("com.mysql.jdbc.Driver");
            //3.擷取資料庫連接配接對象(連接配接到資料庫)
            conn = DriverManager.getConnection("jdbc:mysql:///db05","root","root");
            //4.定義sql語句
            String sql = "select * from account";
            //5.擷取執行sql的對象
            stamt = conn.createStatement();
            //6.執行sql
            ResultSet res = stamt.executeQuery(sql);
            //7.處理結果
            while (res.next()){
                String name = res.getString(2);
                int n = res.getInt("balance");
                System.out.println("姓名:"+name+" 工資:"+n);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally { //8.釋放資源
            if (stamt!=null){
                try {
                    stamt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}