国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java連接Mysql數(shù)據(jù)庫(kù)的工具類

2019-11-26 15:16:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一個(gè)封裝好的鏈接Mysql數(shù)據(jù)庫(kù)的工具類,可以方便的獲取Connection對(duì)象關(guān)閉Statement、ResultSet、Statment對(duì)象等等

復(fù)制代碼 代碼如下:

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ù)庫(kù)
 * @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ù)庫(kù)驅(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ù)庫(kù)連接失敗!");
            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能有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 道孚县| 高密市| 喀什市| 嫩江县| 塔城市| 博白县| 利津县| 高邑县| 泌阳县| 株洲县| 土默特左旗| 临泉县| 郸城县| 尼勒克县| 潍坊市| 全南县| 昭觉县| 长沙市| 岗巴县| 阳曲县| 临沭县| 宁安市| 平山县| 弥勒县| 武夷山市| 阜新市| 陇西县| 长葛市| 秦皇岛市| 汨罗市| 达州市| 临西县| 郎溪县| 蓬溪县| 顺义区| 蒙城县| 石首市| 东山县| 宁波市| 平舆县| 谷城县|