package com.maya.hanqi.jdbctest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcConnectUtil { //工具类 private static final String USERNAME = "test"; private static final String PASSWORD = "test"; private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe"; private static final String DRIVERCLASSNAME = "oracle.jdbc.OracleDriver"; public static Connection getConnection(){ Connection conn = null; try { Class.forName(DRIVERCLASSNAME); conn = DriverManager.getConnection(URL,USERNAME,PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void destroy(Connection conn,Statement sm,ResultSet rs){ if (conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } if (sm!=null) { try { sm.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } if (rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } } public static void main(String[] args) { System.out.println(getConnection()); } }