日期:2014-05-16  浏览次数:20559 次

jdbc 简单工具类的使用
package Jdbcday02;



import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;



public class ConnectionUtils {



	private static String url;

	private static String driver;

	private static String username;

	private static String password;



	static {

		Properties props = new Properties();

		try {

			props.load(ConnectionUtils.class.getClassLoader()

					.getResourceAsStream(

							"Jdbcday02/db_oracle.properties"));

		} catch (IOException e) {

		}

		if (props != null) {

			url = props.getProperty("url");

			driver = props.getProperty("driver");

			username = props.getProperty("username");

			password = props.getProperty("password");

			try {

				Class.forName(driver);

			} catch (ClassNotFoundException e) {

			}

		}

	}



	public static Connection openConnection() throws SQLException {

		return DriverManager.getConnection(url, username, password);

	}

	public static void closeConnection(Connection con) {

		try {

			if (con != null) {

				con.close();

			}

		} catch (SQLException e) {

		}

	}



	public static void closeStatement(Statement stmt) {

		try {

			if (stmt != null) {

				stmt.close();

			}

		} catch (SQLException e) {

		}

	}

	public static void closeStatement(PreparedStatement pstmt) {

		try {

			if (pstmt != null) {

				pstmt.close();

			}

		} catch (SQLException e) {

		}

	}

	public static void closeResultSet(ResultSet rs) {

		try {

			if (rs != null) {

				rs.close();

			}

		} catch (SQLException e) {

		}

	}



}