/** * 驱动加载字符串 */ private String className = "oracle.jdbc.driver.OracleDriver"; /** * 数据库连接地址 */ private String url="jdbc:oracle:thin:@localhost:1521:orcl"; /** * 登录名 */ private String user = "system"; /** * 密码 */ private String password="orcl"; /** * 数据库连接对象 */ protected Connection connection; /** * sql语句执行命令对象 */ protected PreparedStatement preparedStatement; /** * 结果集对象 */ protected ResultSet resultSet; /** * 获得数库连接 * @return 数据库连接对象 */ protected Connection openConnection(){ try { //加载驱动 Class.forName(className); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //获得数据库连接 try { this.connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return this.connection; } protected void closeConnection(){ if(this.resultSet!=null){ try { //关闭结果集 this.resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(this.preparedStatement!=null){ //关闭sql执行对象 try { this.preparedStatement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(this.connection!=null){ //关闭连接 try { this.connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
java jdbc连接oracle数据库_Java中使用JDBC连接oracle数据库
最新推荐文章于 2025-07-17 18:21:36 发布