JDBC连接Oracle数据库详解

来源:互联网 发布:家用太阳能系统 知乎 编辑:程序博客网 时间:2024/06/13 05:44

创建数据连接、关闭数据连接等步骤分开来写,方便重复使用。

package tobaccoTrans.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.Statement;import java.sql.ResultSet;import java.sql.SQLException;/** * 数据库连接关闭操作类 * @author DWJ */public class ConnectionUtil {public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";public static final String URL = "jdbc:oracle:thin:@10.60.2.8:1521:yydb";public static final String USER = "monitor";public static final String PWD = "monitor";/** * JDBC连接数据库 * @return Connection con */protected static Connection getConnection() {Connection con = null;try {Class.forName(DRIVER); // 加载JDBC驱动con = DriverManager.getConnection(URL, USER,PWD); // 创建数据库连接} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}/** * 关闭 ResultSet */public static void closeResultSet(ResultSet rs) {if (rs != null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/** * 关闭 PreparedStatement */public static void closePstmt(PreparedStatement pstmt) {if (pstmt != null) {try {pstmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/** * 关闭 Statement */public static void closeStatement(Statement stmt) {if (stmt != null) {try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/** * 关闭 Connection */public static void closeConn(Connection conn) {if (conn != null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}


 

原创粉丝点击