Java链接Oracle数据库 博客分类: JavaOracle oraclejavajdbc
程序员文章站
2024-03-05 21:52:07
...
Java链接Oracle数据库
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class OracleDAO { private String ip = null; private String port = null; private String database = null; private String user = null; private String password = null; private Connection con = null; /** * * @param ip * @param port * @param database * @param user * @param password */ public OracleDAO(String ip, String port, String database, String user, String password) { this.ip = ip; this.port = port; this.database = database; this.user = user; this.password = password; } /** * * @return Connection */ public Connection getOracleConnection() { int time = 0; String url = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + database + ""; ; try { for (; (++time < 3) && (con == null || con.isClosed());) { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection(url, user, password); System.out.println("Get Oracle database connection OK."); } } catch (Exception e) { System.err.println("Cannot get Oracle database connection."); e.printStackTrace(); } return con; } /** * * @param con * Connection * @return */ public boolean closeOracleResource(Connection con) { return closeOracleResource(con, null, null); } /** * * @param ps * Statement * @return */ public boolean closeOracleResource(Statement ps) { return closeOracleResource(null, ps, null); } /** * * @param rs * ResultSet * @return */ public boolean closeOracleResource(ResultSet rs) { return closeOracleResource(null, null, rs); } /** * * @param con * Connection * @param ps * Statement * @return */ public boolean closeOracleResource(Connection con, Statement ps) { return closeOracleResource(con, ps, null); } /** * * @param ps * Statement * @param rs * ResultSet * @return */ public boolean closeOracleResource(Statement ps, ResultSet rs) { return closeOracleResource(null, ps, rs); } /** * * @param con * Connection * @param rs * ResultSet * @return */ public boolean closeOracleResource(Connection con, ResultSet rs) { return closeOracleResource(con, null, rs); } /** * * @param con * Connection * @param ps * Statement * @param rs * ResultSet * @return */ public boolean closeOracleResource(Connection con, Statement ps, ResultSet rs) { try { if (con != null) con.close(); if (ps != null) ps.close(); if (rs != null) rs.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }