溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JDBC連接MySQL并實(shí)現(xiàn)模糊查詢的方法是什么

發(fā)布時(shí)間:2022-01-04 10:27:06 來(lái)源:億速云 閱讀:192 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“JDBC連接MySQL并實(shí)現(xiàn)模糊查詢的方法是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“JDBC連接MySQL并實(shí)現(xiàn)模糊查詢的方法是什么”吧!

場(chǎng)景:

在學(xué)習(xí)JDBC的語(yǔ)言中,每次都執(zhí)行通用的幾步:即注冊(cè)驅(qū)動(dòng),獲取連接,創(chuàng)建操作,處理結(jié)果,釋放資源 過(guò)于復(fù)雜,因此不妨將上述步驟封裝成工具類,只對(duì)外提供方法!

描述:

這是不使用工具類的封裝寫(xiě)出來(lái)的代碼,比較冗余復(fù)雜

package com.zdx.JDBC;
 
import java.sql.*;
 
public class JAVA1129_5 {
    public static void main(String[] args) {
        //設(shè)置空對(duì)象,注冊(cè)驅(qū)動(dòng),獲取連接,創(chuàng)建操作,處理結(jié)果集,釋放資源
        String url = "jdbc:mysql://127.0.0.1:3306/hello";
        String username = "root";
        String password = "rota";
 
        String SQL = "insert into stu values(1,'zdx','nbnc'),(2,'cyc','qwq');";
//        String SQL1 = "update stu set sname ='xzq',major='bask' where sno = '1';";
        String SQL1="select * from stu";
        Connection connection = null;
        Statement statement = null;
        ResultSet resultset = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            statement = connection.createStatement();
            int cnt = statement.executeUpdate(SQL);
            if (cnt != 0) {
                System.out.println("執(zhí)行成功");
            }
            ResultSet result = statement.executeQuery(SQL1);
            while (result.next()) {
                //隨著光標(biāo)移動(dòng)對(duì)操作對(duì)象進(jìn)行操作
                System.out.println("nbnb");
            }
 
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 
        //釋放資源,必須在最后加上finally語(yǔ)句塊執(zhí)行
        finally {
                if (resultset != null) {
                    try {
                        resultset.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
        }
    }

解決方案:

首先類內(nèi)的構(gòu)造方法加私有修飾符,模仿Sun公司工具類,如Arrays類 和 Collection 。

其次注冊(cè)驅(qū)動(dòng),利用靜態(tài)代碼塊內(nèi)只注冊(cè)一次進(jìn)行注冊(cè)驅(qū)動(dòng)

然后獲取數(shù)據(jù)庫(kù)連接,返回?cái)?shù)據(jù)庫(kù)連接對(duì)象的方法內(nèi)有異常,不能catch,需要向外扔。

最后封裝一個(gè)關(guān)閉的方法。

注意由于封裝工具類,且對(duì)外只提供方法因此都封裝成類方法(即static修飾)

package com.zdx.JDBC;
 
import java.sql.*;
 
//2021.11.2920點(diǎn)03分 對(duì)數(shù)據(jù)庫(kù)的工具類進(jìn)行封裝
public class  DBUtil{
    private DBUtil(){}
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }//利用靜態(tài)代碼塊在類加載時(shí)只加載一次的特性注冊(cè)驅(qū)動(dòng)。
 
    //獲取連接的方法
    public static Connection getConnection (String url,String user,String password)throws SQLException{
       return   DriverManager.getConnection(url,user,password);//這里注意驅(qū)動(dòng)管理類內(nèi)調(diào)用的獲取連接方法返回對(duì)象就是connection對(duì)象。
    }
 
    //關(guān)閉資源
    //按照順序,結(jié)果集,數(shù)據(jù)庫(kù)操作對(duì)象,連接對(duì)象!
    public static void close(Connection connection,Statement ps,ResultSet resultSet){
 
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
 
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
 
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
 
}

 對(duì)工具類進(jìn)行調(diào)用實(shí)現(xiàn)模糊查詢:

package com.zdx.JDBC;
 
import java.sql.*;
 
public class Main {
 
    public static void main(String[] args) {
 
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        String url = "jdbc:mysql://127.0.0.1:3306/hello";
        String user = "root";
        String password = "rota";
        //獲取連接
        try {
            connection = DBUtil.getConnection(url, user, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //獲取預(yù)編譯的數(shù)據(jù)庫(kù)操作對(duì)象
        String SQL = "select sname from stu where sname like ?";
        try {
            ps = connection.prepareStatement(SQL);
            ps.setString(1, "_y%");
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString("sname"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
 
 
        //釋放資源
        finally {
            DBUtil.close(connection, ps, resultSet);
        }
    }
}

感謝各位的閱讀,以上就是“JDBC連接MySQL并實(shí)現(xiàn)模糊查詢的方法是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)JDBC連接MySQL并實(shí)現(xiàn)模糊查詢的方法是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI