JDBC連接MySQL并實(shí)現(xiàn)模糊查詢
場景:
在學(xué)習(xí)JDBC的語言中,每次都執(zhí)行通用的幾步:即注冊驅(qū)動,獲取連接,創(chuàng)建操作,處理結(jié)果,釋放資源 過于復(fù)雜,因此不妨將上述步驟封裝成工具類,只對外提供方法!
描述:
這是不使用工具類的封裝寫出來的代碼,比較冗余復(fù)雜
package com.zdx.JDBC;
import java.sql.*;
public class JAVA1129_5 {
public static void main(String[] args) {
//設(shè)置空對象,注冊驅(qū)動,獲取連接,創(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)移動對操作對象進(jìn)行操作
System.out.println("nbnb");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//釋放資源,必須在最后加上finally語句塊執(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 。
其次注冊驅(qū)動,利用靜態(tài)代碼塊內(nèi)只注冊一次進(jìn)行注冊驅(qū)動
然后獲取數(shù)據(jù)庫連接,返回?cái)?shù)據(jù)庫連接對象的方法內(nèi)有異常,不能catch,需要向外扔。
最后封裝一個關(guān)閉的方法。
注意由于封裝工具類,且對外只提供方法因此都封裝成類方法(即static修飾)
package com.zdx.JDBC;
import java.sql.*;
//2021.11.2920點(diǎn)03分 對數(shù)據(jù)庫的工具類進(jìn)行封裝
public class DBUtil{
private DBUtil(){}
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}//利用靜態(tài)代碼塊在類加載時(shí)只加載一次的特性注冊驅(qū)動。
//獲取連接的方法
public static Connection getConnection (String url,String user,String password)throws SQLException{
return DriverManager.getConnection(url,user,password);//這里注意驅(qū)動管理類內(nèi)調(diào)用的獲取連接方法返回對象就是connection對象。
}
//關(guān)閉資源
//按照順序,結(jié)果集,數(shù)據(jù)庫操作對象,連接對象!
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();
}
}
}
}對工具類進(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ù)庫操作對象
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);
}
}
}到此這篇關(guān)于JDBC連接MySQL并實(shí)現(xiàn)模糊查詢的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot自定義配置實(shí)現(xiàn)IDE自動提示功能
這篇文章主要介紹了Spring Boot自定義配置實(shí)現(xiàn)IDE自動提示功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Java時(shí)間處理第三方包Joda?Time使用詳解
這篇文章主要為大家介紹了Java時(shí)間處理第三方包Joda?Time使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
完全解析Java編程中finally語句的執(zhí)行原理
這篇文章主要深度介紹了Java編程中finally語句的執(zhí)行原理,細(xì)致講解了finally在異常處理中的流程控制作用,需要的朋友可以參考下2015-11-11
Java Swing JSlider滑塊的實(shí)現(xiàn)示例
這篇文章主要介紹了Java Swing JSlider滑塊的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java中Lambda表達(dá)式和函數(shù)式接口的使用和特性
Java Lambda表達(dá)式是一種函數(shù)式編程的特性,可簡化匿名內(nèi)部類的寫法,與函數(shù)式接口搭配使用,實(shí)現(xiàn)代碼簡潔、可讀性高、易于維護(hù)的特點(diǎn),適用于集合操作、多線程編程等場景2023-04-04

