欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDBC util 工具类

程序员文章站 2022-05-19 12:05:34
...

 

package cj;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public final class JdbcUtil {
	private static String url = "jdbc:oracle:thin:@192.168.1.102:1521:orcl";
	private static String password = "cj";
	private static String user = "cj";

	private JdbcUtil() {
	}

	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}



	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, "cj", "cj");
	}

	public static void free(ResultSet rs, Statement st, Connection con) {
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (st != null)
					st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (con != null)
					try {
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
			}
		}
	}

}

 

相关标签: Oracle