java連接Mysql數(shù)據(jù)庫的工具類
一個(gè)封裝好的鏈接Mysql數(shù)據(jù)庫的工具類,可以方便的獲取Connection對(duì)象關(guān)閉Statement、ResultSet、Statment對(duì)象等等
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());
}
}
}
}
以上就是本文的全部?jī)?nèi)容了,希望對(duì)大家熟練掌握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)文章
SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互相關(guān)介紹全面總結(jié)
Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),這篇文章主要介紹了SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互2022-10-10java 注解annotation的使用以及反射如何獲取注解
這篇文章主要介紹了java 注解annotation的使用以及反射如何獲取注解的相關(guān)資料,需要的朋友可以參考下2017-01-01在SpringBoot環(huán)境中使用Mockito進(jìn)行單元測(cè)試的示例詳解
Mockito特別適用于在Spring Boot環(huán)境中進(jìn)行單元測(cè)試,因?yàn)樗軌蜉p松模擬Spring應(yīng)用中的服務(wù)、存儲(chǔ)庫、客戶端和其他組件,通過使用Mockito,開發(fā)者可以模擬外部依賴,從而使單元測(cè)試更加獨(dú)立和可靠,本文給大家介紹了在Spring Boot環(huán)境中使用Mockito進(jìn)行單元測(cè)試2024-01-01SpringMVC如何自定義響應(yīng)的HTTP狀態(tài)碼
這篇文章主要介紹了SpringMVC如何自定義響應(yīng)的HTTP狀態(tài)碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11