抽取数据库连接池

JDBCUtil工具类(一级):

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCUtil {
	{
		try {
			Class.forName(DRI);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static final String DRI = "com.mysql.jdbc.Driver";
	public static final String URL = "jdbc:mysql://localhost:3306/jiejie";
	public static final String USERNAME = "root";
	public static final String PASSWORD = "root";

//	获取连接方法
	public static Connection getCollection() throws Exception {
		Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		return connection;
	}

//	释放资源
	public static void release(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception {
		if (rs != null) {
			rs.close();
			rs = null;
			System.out.println("ResultSet已经关闭");
		}
		if (ps != null) {
			ps.close();
			ps = null;
		}
		if (conn != null) {
			conn.close();
			conn = null;
		}

	}

}

JDBCUtil工具类(升级版):

package com.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class JDBCUtil {
	{
		try {
			// 加载 类
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	static {
		Properties properties = new Properties();
		try {
//			加载properties中的数据
			properties.load(new FileInputStream("hhh.properties"));
			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			username = properties.getProperty("username");
			password = properties.getProperty("password");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String driver;
	public static String url;
	public static String username;
	public static String password;

//	获取连接方法
	public static Connection getCollection() throws Exception {
		Connection connection = DriverManager.getConnection(url, username, password);
		return connection;
	}

//	释放资源
	public static void release(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception {
		if (rs != null) {
			rs.close();
			rs = null;
			System.out.println("ResultSet已经关闭");
		}
		if (ps != null) {
			ps.close();
			ps = null;
		}
		if (conn != null) {
			conn.close();
			conn = null;
		}

	}

}

hhh.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jiejie
username=root
password=root

测试类:

package com.test;

import java.sql.Connection;
import java.sql.ResultSet;

public class Test3 {
	public static void main(String[] args) {
		try {
			Connection connection = JDBCUtil.getCollection();
			ResultSet i = connection.prepareStatement("select * from u_p").executeQuery();
			while (i.next()) {
				int a = i.getInt("id");
				String name = i.getString("username");
				String pw = i.getString("pw");
				System.out.println(a + "----" + name + "-----" + pw);
			}
			JDBCUtil.release(i, null, connection);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值