jdbc 链接方式 DBUtil获得链接的三种写法

本文介绍了在非JavaWeb项目和JavaWeb项目中使用JDBC链接数据库的两种常见方法。对于非JavaWeb项目,详细说明了如何通过database.properties配置文件初始化数据库连接。而在JavaWeb项目中,利用Tomcat的数据连接池,不再依赖properties文件,而是通过context.xml配置,并强调了在Tomcat的lib目录添加数据库驱动jar包的重要性。
摘要由CSDN通过智能技术生成

普通项目(非javaweb项目),项目文件路径如图

database.properties

driver=oracle.jdbc.driver.OracleDriver
username=neu
password=oracle
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:ORACLE

DBUtil

package com.duoduo.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

public class DBUtil {
	private static Connection connection;
	private static PreparedStatement pstmt;
	private static ResultSet resultSet;
	private static String password;
	private static String username;
	private static String driver;
	private static String url;
	static {
		try {
			init();
			register();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/***
	 * 注册驱动 *
	 **/
	private static void register() throws ClassNotFoundException {
		Class.forName(driver);
	}

	/***
	 * 初始化配置 *
	 **/
	private static void init() throws IOException {
		Properties properties = new Properties();
		FileInputStream in = new FileInputStream(
				"properties/database.properties");
		properties.load(in);
		in.close();
		driver = properties.getProperty("driver");
		username = properties.getProperty("username");
		password = properties.getProperty("password");
		url = properties.getProperty("url");
		Logger logger = Logger.getLogger("com.duoduo.jdbc");
		logger.info(driver);
		logger.info(username);
		logger.info(password);
		logger.info(url);
	}

	/***
	 * 获得连接 *
	 **/
	public static Connection getConnection2() throws SQLException {
		// 确保资源已经释放
		clear();
		connection = DriverManager.getConnection(url, username, password);
		return connection;
	}

	/***
	 * 获得连接 *
	 **/
	public static void getConnection() throws SQLException {
		// 确保资源已经释放
		clear();
		connection = DriverManager.getConnection(url, username, password);
		// return connection;
	}

	/***
	 * 开始事务 *
	 **/
	public static void startTrans(Connection conn) throws SQLException {
		if (conn != null) {
			conn.setAutoCommit(false);
		}
	}

	public static void startTrans() throws SQLException {
		if (connection != null) {
			connection.setAutoCommit(false);
		}
	}

	/***
	 * 结束事务 *
	 * 
	 * @throws SQLException
	 **/
	public static void endTrans(Connection conn) throws SQLException {
		if (conn != null) {
			conn.commit();
		}
	}

	public static void endTrans() throws SQLException {
		if (connection != null) {
			connection.commit();
		}
	}

	/***
	 * 回滚事务 *
	 * 
	 * @throws SQLException
	 **/
	public static void rollbackTrans(Connection conn) throws SQLException {
		if (conn != null) {
			conn.rollback();
		}
	}

	public static void rollbackTrans() throws SQLException {
		if (connection != null) {
			connection.rollback();
		}
	}

	/***
	 * 清空资源 *
	 * 
	 * @throws SQLException
	 **/
	public static void clear(Connection conn) throws SQLException {
		if (conn != null) {
			conn.close();
		}
	}

	/***
	 * 清空资源 *
	 **/
	public static void clear(Connection conn, PreparedStatement ps)
			throws SQLException {
		if (ps != null) {
			ps.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	/***
	 * 清空资源 *
	 **/
	public static void clear(Connection conn, PreparedStatement ps, ResultSet rs)
			throws SQLException {
		if (rs != null) {
			rs.close();
		}
		if (ps != null) {
			ps.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	public static void clear() throws SQLException {
		if (resultSet != null) {
			resultSet.close();
			resultSet = null;
		}
		if (pstmt != null) {
			pstmt.close();
			pstmt = null;
		}
		if (connection != null) {
			connection.close();
			connection = null;
		}
	}

	private static void setParamsToPreparedStatement(PreparedStatement pstmt,
			List<Object> params) throws SQLException {
		int index = 1;
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				pstmt.setObject(index++, params.get(i));
			}
		}
	}

	public static Map<String, Object> findSimpleResult(String sql,
			List<Object> params) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		// index:占位符地址
		pstmt = connection.prepareStatement(sql);
		setParamsToPreparedStatement(pstmt, params);
		resultSet = pstmt.executeQuery();

		ResultSetMetaData metaData = resultSet.getMetaData();
		int col_len = metaData.getColumnCount();
		while (resultSet.next()) {
			for (int i = 0; i < col_len; i++) {
				String cols_name = metaData.getColumnName(i + 1);
				Object cols_value = resultSet.getObject(cols_name);
				if (cols_value == null) {
					
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值