java連接Mysql數(shù)據(jù)庫的工具類
一個(gè)封裝好的鏈接Mysql數(shù)據(jù)庫的工具類,可以方便的獲取Connection對象關(guān)閉Statement、ResultSet、Statment對象等等
package myUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 鏈接mysql數(shù)據(jù)庫
* @author weichk
*/
public class MysqlDbManager {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/openfire";
private static final String USER = "root";
private static final String PASSWORD = "123456";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("加載Mysql數(shù)據(jù)庫驅(qū)動(dòng)失??!");
}
}
/**
* 獲取Connection
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("獲取數(shù)據(jù)庫連接失敗!");
throw e;
}
return conn;
}
/**
* 關(guān)閉ResultSet
* @param rs
*/
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 關(guān)閉Statement
* @param stmt
*/
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
* 關(guān)閉ResultSet、Statement
* @param rs
* @param stmt
*/
public static void closeStatement(ResultSet rs, Statement stmt) {
closeResultSet(rs);
closeStatement(stmt);
}
/**
* 關(guān)閉PreparedStatement
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(PreparedStatement pstmt) throws SQLException
{
pstmt.close();
}
/**
* 關(guān)閉ResultSet、PreparedStatement
* @param rs
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(ResultSet rs, PreparedStatement pstmt) throws SQLException
{
rs.close();
pstmt.close();
}
/**
* 關(guān)閉ResultSet、Statement、Connection
* @param rs
* @param stmt
* @param con
*/
public static void closeConnection(ResultSet rs, Statement stmt, Connection con) {
closeResultSet(rs);
closeStatement(stmt);
closeConnection(con);
}
/**
* 關(guān)閉Statement、Connection
* @param stmt
* @param con
*/
public static void closeConnection(Statement stmt, Connection con) {
closeStatement(stmt);
closeConnection(con);
}
/**
* 關(guān)閉Connection
* @param con
*/
public static void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
以上就是本文的全部內(nèi)容了,希望對大家熟練掌握java能有所幫助。
- Java連接MySql的詳細(xì)介紹
- java連接mysql數(shù)據(jù)庫亂碼的解決方法
- Java連接MYSQL數(shù)據(jù)庫的實(shí)現(xiàn)步驟
- java連接mysql數(shù)據(jù)庫詳細(xì)步驟解析
- java連接MySQl數(shù)據(jù)庫實(shí)例代碼
- java連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)代碼
- JavaWeb連接數(shù)據(jù)庫MySQL的操作技巧
- javaweb中mysql數(shù)據(jù)庫連接步驟方法及其實(shí)例
- java連接mysql數(shù)據(jù)庫的方法
- Java+MySQL前后端連接新手小白教程
相關(guān)文章
關(guān)于java開發(fā)的性能問題總結(jié)(必看)
下面小編就為大家?guī)硪黄P(guān)于java開發(fā)的性能問題總結(jié)(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
JAVA得到數(shù)組中最大值和最小值的簡單實(shí)例
這篇文章主要介紹了JAVA得到數(shù)組中最大值和最小值的簡單實(shí)例,需要的朋友可以參考下2014-08-08
jd-easyflow中inclusive的用法示例小結(jié)
文章介紹了在jd-easyflow中使用inclusive進(jìn)行條件分支配置的方法,當(dāng)conditionType設(shè)置為inclusive時(shí),所有條件分支都會(huì)被評估,而不僅僅是一個(gè)條件滿足就終止,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-11-11
使用Spring boot標(biāo)記一個(gè)方法過時(shí)
這篇文章主要介紹了使用Spring boot標(biāo)記一個(gè)方法過時(shí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
SpringBoot動(dòng)態(tài)定時(shí)功能實(shí)現(xiàn)方案詳解
在SpringBoot項(xiàng)目中簡單使用定時(shí)任務(wù),不過由于要借助cron表達(dá)式且都提前定義好放在配置文件里,不能在項(xiàng)目運(yùn)行中動(dòng)態(tài)修改任務(wù)執(zhí)行時(shí)間,實(shí)在不太靈活。現(xiàn)在我們就來實(shí)現(xiàn)可以動(dòng)態(tài)修改cron表達(dá)式的定時(shí)任務(wù),感興趣的可以了解一下2022-11-11
淺析Java ClassName.this中類名.this關(guān)鍵字的理解
Java ClassName.this中類名.this關(guān)鍵字 的理解大家都了解多少,有不太了解的朋友可以參考下本文一起學(xué)習(xí)學(xué)習(xí)2016-05-05

