java連接mysql數(shù)據(jù)庫學(xué)習(xí)示例
package sns.team6.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 鏈接數(shù)據(jù)庫的工具類
*
* @author 徐銳
*
*/
public class DBHelper {
// mysql驅(qū)動(dòng)路徑
private static final String driver = "com.mysql.jdbc.Driver";
// 數(shù)據(jù)庫的連接路徑
private static final String url = "jdbc:mysql://localhost:3306/snsteam6";
/**
* 連接數(shù)據(jù)庫的對(duì)象
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, "root", "root");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 關(guān)閉打開的資源
*
* @param conn
* @param pst
* @param rst
*/
public static void closeInfo(Connection conn, PreparedStatement pst,
ResultSet rst) {
try {
if (rst != null) {
rst.close();
rst = null;
}
if (pst != null) {
pst.close();
pst = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取結(jié)果集對(duì)象
*
* @param sql
* @param params
* @return
*/
public static ResultSet resultSet(String sql, Object[] params) {
// 數(shù)據(jù)庫的鏈接對(duì)象
Connection conn = null;
// 數(shù)據(jù)庫的操作對(duì)象
PreparedStatement pst = null;
// 結(jié)果對(duì)象
ResultSet rst = null;
try {
// 數(shù)據(jù)庫的鏈接對(duì)象
conn = DBHelper.getConnection();
// 數(shù)據(jù)庫的操作對(duì)象
pst = conn.prepareStatement(sql);
// 判斷是否有參數(shù)
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
// 給操作對(duì)象賦值
pst.setObject(i + 1, params[i]);
}
}
// 獲取結(jié)果對(duì)象
rst = pst.executeQuery();
} catch (SQLException e) {
rst = null;
e.printStackTrace();
}
return rst;
}
/**
* 獲取添加的結(jié)果對(duì)象
*
* @param sql
* @param params
* @return
*/
public static boolean result(String sql, Object[] params) {
boolean flag = false;
// 數(shù)據(jù)庫的鏈接對(duì)象
Connection conn = null;
// 數(shù)據(jù)庫的操作對(duì)象
PreparedStatement pst = null;
try {
// 數(shù)據(jù)庫的鏈接對(duì)象
conn = DBHelper.getConnection();
// 數(shù)據(jù)庫的操作對(duì)象
pst = conn.prepareStatement(sql);
// 判斷是否有參數(shù)
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
// 給操作對(duì)象賦值
pst.setObject(i + 1, params[i]);
}
}
// 獲取結(jié)果對(duì)象,是int類型,表示執(zhí)行成功的行數(shù)
int row = pst.executeUpdate();
if (row > 0) {
// 如果大于0,表示執(zhí)行成功
flag = true;
}
} catch (SQLException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
}
- java連接mysql數(shù)據(jù)庫亂碼的解決方法
- Java連接MYSQL數(shù)據(jù)庫的實(shí)現(xiàn)步驟
- Java實(shí)現(xiàn)獲得MySQL數(shù)據(jù)庫中所有表的記錄總數(shù)可行方法
- java連接mysql數(shù)據(jù)庫詳細(xì)步驟解析
- 通過java備份恢復(fù)mysql數(shù)據(jù)庫的實(shí)現(xiàn)代碼
- java連接MySQl數(shù)據(jù)庫實(shí)例代碼
- java使用jdbc連接數(shù)據(jù)庫工具類和jdbc連接mysql數(shù)據(jù)示例
- java連接Mysql數(shù)據(jù)庫的工具類
- Java 通過JDBC連接Mysql數(shù)據(jù)庫
- Java連接MYSQL數(shù)據(jù)庫的詳細(xì)步驟
相關(guān)文章
SpringBoot監(jiān)控模塊Actuator的用法詳解
Spring?Boot?Actuator?是?Spring?Boot?自帶的一個(gè)功能模塊,提供了一組已經(jīng)開箱即用的生產(chǎn)環(huán)境下常用的特性和服務(wù),比如應(yīng)用程序的健康檢查、信息暴露、度量收集、日志記錄等,本文將給大家詳細(xì)SpringBoot監(jiān)控模塊Actuator的用法2023-06-06
詳解SpringMVC學(xué)習(xí)系列之國(guó)際化
這篇文章主要介紹了詳解SpringMVC學(xué)習(xí)系列之國(guó)際化,詳細(xì)的介紹了關(guān)于瀏覽器,Session,Cookie,URL請(qǐng)求的國(guó)際化的實(shí)現(xiàn),有興趣的可以了解一下2017-07-07
一文詳解Spring任務(wù)執(zhí)行和調(diào)度(小結(jié))
這篇文章主要介紹了一文詳解Spring任務(wù)執(zhí)行和調(diào)度(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Java多線程、進(jìn)度條實(shí)現(xiàn)賽馬實(shí)驗(yàn)的示例代碼
這篇文章主要介紹了Java多線程、進(jìn)度條實(shí)現(xiàn)賽馬實(shí)驗(yàn)的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot使用Editor.md構(gòu)建Markdown富文本編輯器示例
這篇文章主要介紹了SpringBoot使用Editor.md構(gòu)建Markdown富文本編輯器示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
SpringBoot整合thymeleaf 報(bào)錯(cuò)的解決方案
這篇文章主要介紹了SpringBoot整合thymeleaf 報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java基于rest assured實(shí)現(xiàn)接口測(cè)試過程解析
這篇文章主要介紹了Java基于rest assured實(shí)現(xiàn)接口測(cè)試過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java中List轉(zhuǎn)Map的幾種具體實(shí)現(xiàn)方式和特點(diǎn)
這篇文章主要介紹了幾種常用的List轉(zhuǎn)Map的方式,包括使用for循環(huán)遍歷、Java8StreamAPI、ApacheCommonsCollections和GoogleGuava,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01

