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

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

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

一個(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能有所幫助。

相關(guān)文章

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

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

    下面小編就為大家?guī)?lái)一篇關(guān)于java開(kāi)發(fā)的性能問(wèn)題總結(jié)(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • JAVA得到數(shù)組中最大值和最小值的簡(jiǎn)單實(shí)例

    JAVA得到數(shù)組中最大值和最小值的簡(jiǎn)單實(shí)例

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

    jd-easyflow中inclusive的用法示例小結(jié)

    文章介紹了在jd-easyflow中使用inclusive進(jìn)行條件分支配置的方法,當(dāng)conditionType設(shè)置為inclusive時(shí),所有條件分支都會(huì)被評(píng)估,而不僅僅是一個(gè)條件滿足就終止,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • 使用Spring boot標(biāo)記一個(gè)方法過(guò)時(shí)

    使用Spring boot標(biāo)記一個(gè)方法過(guò)時(shí)

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

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

    這篇文章主要介紹了淺談基于Consul創(chuàng)建分布式鎖,Consul是HashiCorp公司推出的開(kāi)源工具,用于實(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)目中簡(jiǎn)單使用定時(shí)任務(wù),不過(guò)由于要借助cron表達(dá)式且都提前定義好放在配置文件里,不能在項(xiàng)目運(yùn)行中動(dòng)態(tài)修改任務(wù)執(zhí)行時(shí)間,實(shí)在不太靈活?,F(xiàn)在我們就來(lái)實(shí)現(xiàn)可以動(dòng)態(tài)修改cron表達(dá)式的定時(shí)任務(wù),感興趣的可以了解一下
    2022-11-11
  • SpringBoot?讀取yml文件的多種方式匯總

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

    這篇文章主要介紹了SpringBoot讀取yml文件的幾種方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(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ì)的從為問(wèn)題描述開(kāi)始,清晰的解題思路以及詳細(xì)的代碼實(shí)現(xiàn),具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-12-12
  • java io讀取文件操作代碼實(shí)例

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

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

最新評(píng)論

郓城县| 黎城县| 庄河市| 日喀则市| 北海市| 惠安县| 蓬莱市| 菏泽市| 合肥市| 马山县| 会东县| 湘西| 桦甸市| 沛县| 武川县| 乡宁县| 石泉县| 青海省| 呼玛县| 房产| 翁源县| 廉江市| 枣强县| 灌阳县| 长春市| 措美县| 蒙山县| 集安市| 嘉禾县| 济南市| 洛浦县| 兴安县| 阿尔山市| 华宁县| 五华县| 富阳市| 杭锦后旗| 桐梓县| 广西| 天长市| 宁南县|