欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

JDBC系列:(2.5)創(chuàng)建JDBCUtils工具類

1、建立db.properties文件

成都創(chuàng)新互聯(lián)公司主要從事做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)大理州,十多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

url=jdbc:MySQL://localhost:3306/testdb
user=root
password=root
driverClass=com.mysql.jdbc.Driver

2、JDBC工具類:JDBCUtil.java

package com.rk.db.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC的工具類
 * @author RK
 *
 */
public class JDBCUtil
{
	private static final String url;
	private static final String user;
	private static final String password;
	private static final String driverClass;
	
	/**
	 * 靜態(tài)代碼塊中(只加載一次)
	 */
	static
	{		
		try
		{
			//讀取db.properties文件
			InputStream inStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
			
			Properties props = new Properties();
			//加載文件
			props.load(inStream);
			//讀取信息
			url = props.getProperty("url");
			user = props.getProperty("user");
			password = props.getProperty("password");
			driverClass = props.getProperty("driverClass");
			
			//注冊驅(qū)動程序
			Class.forName(driverClass);
		}
		catch (IOException e)
		{
			System.out.println("讀取數(shù)據(jù)庫配置文件出錯");
			throw new RuntimeException(e);
		}
		catch (ClassNotFoundException e)
		{
			System.out.println("數(shù)據(jù)庫驅(qū)程程序注冊出錯");
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 獲取數(shù)據(jù)庫的連接
	 * @return 數(shù)據(jù)庫連接
	 */
	public static Connection getConnection()
	{
		try
		{
			return DriverManager.getConnection(url,user,password);
		}
		catch (SQLException e)
		{
			System.out.println("獲取數(shù)據(jù)庫連接出錯");
			throw new RuntimeException(e);
		}		
	}
	
	/**
	 * 關(guān)閉Connection、Statement和ResultSet
	 * @param conn 數(shù)據(jù)庫連接
	 * @param stmt	執(zhí)行SQL語句的命令
	 * @param rs 結(jié)果集
	 */
	public static void close(Connection conn,Statement stmt,ResultSet rs)
	{
		closeQuietly(rs);
		closeQuietly(stmt);
		closeQuietly(conn);
	}
	
	/**
	 * 安靜的關(guān)閉數(shù)據(jù)庫資源
	 * @param ac 實現(xiàn)了AutoCloseable接口的對象
	 */
	public static void closeQuietly(AutoCloseable ac)
	{
		if(ac != null)
		{
			try
			{
				ac.close();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}

網(wǎng)頁名稱:JDBC系列:(2.5)創(chuàng)建JDBCUtils工具類
文章轉(zhuǎn)載:http://chinadenli.net/article26/ihohcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)網(wǎng)站改版面包屑導(dǎo)航外貿(mào)網(wǎng)站建設(shè)域名注冊手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計