JDBC连接ORACLE工具类

package com.lwp.util;

import java.sql.*;

public class DBUtil {

 public static Connection getConnection() {
  Connection conn = null;
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
   String username = "oracle";
   String password = "oracle";
   conn = DriverManager.getConnection(dbUrl, username, password);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }

 public static void close(PreparedStatement pstmt) {
  if (pstmt != null) {
   try {
    pstmt.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }

 public static void close(Connection conn) {
  if (conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }

 public static void close(ResultSet rs) {
  if (rs != null) {
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }

 public static void commit(Connection conn) {
  if (conn != null) {
   try {
    conn.commit();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }

 public static void rollback(Connection conn) {
  if (conn != null) {
   try {
    conn.rollback();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }

 public static void setAutoCommit(Connection conn, boolean autoCommit) {
  if (conn != null) {
   try {
    conn.setAutoCommit(autoCommit);
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }

 public static void main(String[] args) {
  DBUtil.getConnection();
  System.out.println("---------数据库连接成功!---------");
 }
}

你可能感兴趣的:(JAVAEE)