JDBC連接MySQL并實(shí)現(xiàn)模糊查詢
場(chǎng)景:
在學(xué)習(xí)JDBC的語言中,每次都執(zhí)行通用的幾步:即注冊(cè)驅(qū)動(dòng),獲取連接,創(chuàng)建操作,處理結(jié)果,釋放資源 過于復(fù)雜,因此不妨將上述步驟封裝成工具類,只對(duì)外提供方法!
描述:
這是不使用工具類的封裝寫出來的代碼,比較冗余復(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語句塊執(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ù)庫連接,返回?cái)?shù)據(jù)庫連接對(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ù)庫的工具類進(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ù)庫操作對(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ù)庫操作對(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);
}
}
}到此這篇關(guān)于JDBC連接MySQL并實(shí)現(xiàn)模糊查詢的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA多線程與并發(fā)學(xué)習(xí)總結(jié)分析
以下是對(duì)小編對(duì)JAVA多線程與并發(fā)的學(xué)習(xí)進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下2013-08-08
kettle指定jdk8的路徑啟動(dòng)的實(shí)現(xiàn)
本文主要介紹了指定jdk8的路徑啟動(dòng)的實(shí)現(xiàn),通過修改kettle的配置文件來指定使用jdk8版本啟動(dòng),具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
Java與Spring?boot后端項(xiàng)目Bug超全總結(jié)
Spring Boot是一個(gè)開源的 Java 開發(fā)框架,它的目的是簡(jiǎn)化Spring應(yīng)用程序的開發(fā)和部署,下面這篇文章主要給大家介紹了關(guān)于Java與Spring?boot后端項(xiàng)目Bug的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Spring創(chuàng)建Bean完成后執(zhí)行指定代碼的幾種實(shí)現(xiàn)方式
在實(shí)際開發(fā)中經(jīng)常會(huì)遇到在spring容器加載完某個(gè)bean之后,需要執(zhí)行一些業(yè)務(wù)代碼的場(chǎng)景,本文給大家介紹Spring創(chuàng)建Bean完成后執(zhí)行指定代碼的幾種實(shí)現(xiàn)方式,感興趣的朋友一起看看吧2024-01-01
java使用lambda表達(dá)式對(duì)List集合進(jìn)行操作技巧(JDK1.8)
這篇文章主要介紹了java使用lambda表達(dá)式對(duì)List集合進(jìn)行操作技巧適用jdk1.8,感興趣的朋友跟著小編一起看看實(shí)現(xiàn)代碼吧2018-06-06

