首页 > 其他分享 >JJdbcUtils

JJdbcUtils

时间:2023-03-05 15:35:34浏览次数:35  
标签:java JJdbcUtils pops static catch import public

package com.st.ustils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**

  • 资源的连接和关闭
  • @author 30693

*/
public class JJdbcUtils {
public static String driverclass;
public static String url;
public static String username;
public static String password;

static {
	// 1.创建配资文件对象
	Properties pops = new Properties();
	// 2. 创建流对象
	try {
		Reader re = new FileReader("src/db.properties");
		// 3.加载流对象
		pops.load(re);
		// 4.通过key -- value得到value
		driverclass = pops.getProperty("driver");
		url = pops.getProperty("url");
		username = pops.getProperty("username");
		password = pops.getProperty("password");
		Class.forName(driverclass);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

public static Connection getConn() throws SQLException {
	return DriverManager.getConnection(url, username, password);
}

public static void close(ResultSet rs, Statement se, Connection conn) {
	try {
		if (rs != null) {
			rs.close();
		}
		if (se != null) {
			se.close();
		}
		if (conn != null) {
			conn.close();
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}

标签:java,JJdbcUtils,pops,static,catch,import,public
From: https://www.cnblogs.com/Breeze-lele/p/17180642.html

相关文章