《JAVA:從入門到精通》part 23

二十六、數(shù)據(jù)庫操作基礎(chǔ)

  • 數(shù)據(jù)庫系統(tǒng)概論在此先不過多敘述了,主要是學(xué)習(xí)用JAVA操作數(shù)據(jù)庫。

1. JDBC概述

  • JDBC是一種可用于執(zhí)行SQL語句的Java API(應(yīng)用程序設(shè)計(jì)接口),是連接數(shù)據(jù)庫和JAVA應(yīng)用程序的紐帶。

  • 為什么要使用JDBC呢?JAVA可以利用JDBC驅(qū)動程序訪問數(shù)據(jù)庫。通過JDBC技術(shù)可以完成與數(shù)據(jù)庫建立連接、向數(shù)據(jù)庫發(fā)送SQL語句、處理數(shù)據(jù)庫返回的結(jié)果。


    JDBC的作用
  • 使用JDBC操作數(shù)據(jù)庫分為五個(gè)步驟:加載JDBC驅(qū)動程序、連接數(shù)據(jù)庫、發(fā)送SQL語言、處理結(jié)果集和關(guān)閉數(shù)據(jù)庫。


    使用JDBC操作數(shù)據(jù)庫的步驟

2. 連接數(shù)據(jù)庫

  • 要訪問數(shù)據(jù)庫,首先要加載數(shù)據(jù)庫的驅(qū)動程序(只需要在第一次訪問數(shù)據(jù)庫時(shí)加載一次),然后每次訪問數(shù)據(jù)庫時(shí)創(chuàng)建一個(gè)Connection對象,接著執(zhí)行操作數(shù)據(jù)庫的SQL語句,最后在完成數(shù)據(jù)庫操作后銷毀前面創(chuàng)建的Connection對象,釋放與數(shù)據(jù)庫的連接。


    連接數(shù)據(jù)庫

下面編寫一個(gè)程序連接數(shù)據(jù)庫:

  • 連接數(shù)據(jù)庫:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class lianjiesjk
{
    public static void main(String[] args)
    {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/database";
            String username="root";
            String password="123456";
            Connection con= DriverManager.getConnection(url,username,password);
            System.out.println(con);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

運(yùn)行結(jié)果:

  • 如圖所示,數(shù)據(jù)庫連接成功。

3. JAVA對數(shù)據(jù)庫的基本操作(增、刪、查、改)

  • 首先在mysql中建立一個(gè)數(shù)據(jù)表:


首先對數(shù)據(jù)庫進(jìn)行查詢操作:

import java.sql.*;

public class chaxun
{
    public static void main(String[] args )
    {
        Connection con1=null;//鏈接接口
        Statement stmt1=null;//發(fā)送sql接口
        ResultSet rs1=null;//結(jié)果集接口
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con1= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/database","root","123456");
            stmt1=con1.createStatement();
            rs1=stmt1.executeQuery("select * from student1");
            while (rs1.next())//下一行數(shù)據(jù)
            {
                int ID=rs1.getInt("ID");//獲取ID這一列的值
                String name=rs1.getString(2);//獲取第二列的值
                String sex=rs1.getString("sex");
                String birthday=rs1.getString(4);
                System.out.println("編號:"+ID+"  姓名:"+name+"  性別:"+sex+"  生日:"+birthday);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs1!=null)
            {
                try {
                    rs1.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt1!=null)
            {
                try {
                    stmt1.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con1!=null)
            {
                try {
                    con1.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

運(yùn)行結(jié)果:
查詢結(jié)果

  • 由運(yùn)行結(jié)果可以看到,查詢得到的數(shù)據(jù)跟創(chuàng)建表的數(shù)據(jù)一樣,實(shí)現(xiàn)了對數(shù)據(jù)庫的查詢操作。

對數(shù)據(jù)庫實(shí)行增、刪、改操作

import java.sql.*;

public class chaxun
{
    public static void main(String[] args )
    {
        Connection con1=null;//鏈接接口
        Statement stmt1=null;//發(fā)送sql接口
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con1= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/database","root","123456");
            stmt1=con1.createStatement();
            //插入數(shù)據(jù)
            String sq11="insert into student1(ID,name,sex,birthday) values('5','tom','男','20001122')";
            int result1=stmt1.executeUpdate(sq11);
            System.out.println("插入了"+result1+"行數(shù)據(jù)");

            //修改數(shù)據(jù)
            String sql2="update student1 set birthday = '19920404' where ID = 2";
            int result2=stmt1.executeUpdate(sql2);
            System.out.println("修改了"+result2+"行數(shù)據(jù)");

            //刪除數(shù)據(jù)
            String sql3="delete from student1 where ID=1";
            int result3=stmt1.executeUpdate(sql3);
            System.out.println("刪除了"+result3+"行數(shù)據(jù)");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

運(yùn)行結(jié)果:
操作情況

操作后的數(shù)據(jù)表
  • 由運(yùn)行結(jié)果可以看到,程序運(yùn)行之后,刪除了第一行的數(shù)據(jù),修改了小華的生日數(shù)據(jù),并且添加了一行新的數(shù)據(jù),至于為什么tom得性別是問號,那是由于在建立數(shù)據(jù)表的時(shí)候,選擇的編碼模式與IDEA的編碼模式不一樣導(dǎo)致的亂碼問題。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容