日期:2014-05-16 浏览次数:20556 次
JDCB连接ORACLE数据库代码
?
?
package com.syxp.dns.common; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.log4j.Logger; /** * 数据库连接类 * @author 常宝龙 * */ public class JDBCConnection { //驱动名 private static String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver"; //连接路径 private static String ORACLE_URL = "jdbc:oracle:thin:@192.168.1.189:1521:orcl"; //用户名 private static String ORACLE_USERNAME = "iams"; //密码 private static String ORACLE_PASSWORD = "Syxp1111"; //连接参数 private static Connection conn = null; //日志信息 private static Logger logger = Logger.getLogger(JDBCConnection.class); /** * 获取数据库连接 * @return * @throws ClassNotFoundException * @throws SQLException */ public Connection getConnection() throws ClassNotFoundException,SQLException{ //加载驱动 Class.forName(ORACLE_DRIVER); //得到连接 conn = DriverManager.getConnection(ORACLE_URL, ORACLE_USERNAME, ORACLE_PASSWORD); logger.info("成功连接Oracle数据库"); return conn; } /** * 关闭数据库连接 * @param rs * @param pstmt * @param conn */ public void close(ResultSet rs , PreparedStatement pstmt , Connection conn) throws SQLException{ if(rs!=null){ rs.close(); } if(pstmt!=null){ pstmt.close(); } if(conn!=null&&!conn.isClosed()){ conn.close(); } logger.info("成功关闭资源"); } /** * 测试方法 * @param args */ public static void main(String[] args) { JDBCConnection jdbcConnection = new JDBCConnection(); try { jdbcConnection.getConnection(); ResultSet rs = null; PreparedStatement pstmt = null; jdbcConnection.close(rs, pstmt, conn); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }