最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2015年03月12日 16:12:26   投稿:hebedich  
這篇文章主要介紹了java連接Mysql數(shù)據(jù)庫的工具類,非常的實(shí)用,推薦給大家,需要的朋友可以參考下

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

復(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ù)庫
 * @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能有所幫助。

相關(guān)文章

  • 關(guān)于java開發(fā)的性能問題總結(jié)(必看)

    關(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í)例

    這篇文章主要介紹了JAVA得到數(shù)組中最大值和最小值的簡單實(shí)例,需要的朋友可以參考下
    2014-08-08
  • jd-easyflow中inclusive的用法示例小結(jié)

    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í)

    這篇文章主要介紹了使用Spring boot標(biāo)記一個(gè)方法過時(shí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 淺談Java基于Consul創(chuàng)建分布式鎖

    淺談Java基于Consul創(chuàng)建分布式鎖

    這篇文章主要介紹了淺談基于Consul創(chuàng)建分布式鎖,Consul是HashiCorp公司推出的開源工具,用于實(shí)現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置Consul是分布式的、高可用的、可橫向擴(kuò)展的,需要的朋友可以參考下
    2023-07-07
  • SpringBoot動(dòng)態(tài)定時(shí)功能實(shí)現(xiàn)方案詳解

    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
  • SpringBoot?讀取yml文件的多種方式匯總

    SpringBoot?讀取yml文件的多種方式匯總

    這篇文章主要介紹了SpringBoot讀取yml文件的幾種方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • 淺析Java ClassName.this中類名.this關(guān)鍵字的理解

    淺析Java ClassName.this中類名.this關(guān)鍵字的理解

    Java ClassName.this中類名.this關(guān)鍵字 的理解大家都了解多少,有不太了解的朋友可以參考下本文一起學(xué)習(xí)學(xué)習(xí)
    2016-05-05
  • Java遞歸實(shí)現(xiàn)迷宮游戲

    Java遞歸實(shí)現(xiàn)迷宮游戲

    這篇文章主要介紹了如何利用Java遞歸方法實(shí)現(xiàn)迷宮游戲,下面文章會(huì)詳細(xì)的從為問題描述開始,清晰的解題思路以及詳細(xì)的代碼實(shí)現(xiàn),具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-12-12
  • java io讀取文件操作代碼實(shí)例

    java io讀取文件操作代碼實(shí)例

    這篇文章主要介紹了java io讀取文件操作代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評論

满城县| 英吉沙县| 调兵山市| 平舆县| 吴旗县| 平利县| 滦南县| 东至县| 阿拉善盟| 施秉县| 浮山县| 互助| 鄂托克旗| 岫岩| 临高县| 中西区| 宝鸡市| 商水县| 舞阳县| 大庆市| 千阳县| 翁牛特旗| 峨眉山市| 龙泉市| 贺州市| 桃源县| 七台河市| 儋州市| 铜川市| 当涂县| 永城市| 普安县| 博客| 项城市| 武隆县| 黔南| 应用必备| 宁晋县| 中宁县| 黔东| 永宁县|