oracle的JDBC连接

发布时间:2020-07-27 04:02:08 作者:jiazhipeng12
来源:网络 阅读:487

package com.xian.jdbc;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;


import javax.servlet.jsp.jstl.sql.Result;

import javax.servlet.jsp.jstl.sql.ResultSupport;


public class OracleConnection {

/**

* 连接ora数据库

* @return con

* @throws ClassNotFoundException

* @throws SQLException

* @author 贾小仙

*/

   public static Connection getOracleConnection() throws ClassNotFoundException,SQLException{

  String driver="oracle.jdbc.driver.OracleDriver";

  String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";

  Class.forName(driver);

  Connection con=DriverManager.getConnection(url, "scott","7758521");

  return con;

   }

   /**

* 关闭ora连接通道

* @return 

* @throws SQLException

* @author 贾小仙

*/

   public void freeResources(ResultSet resultSet,PreparedStatement pStatement,Connection con) throws SQLException{

  if(resultSet.isClosed()==false)

  resultSet.close();

  if(pStatement.isClosed()==false)

  pStatement.close();

  if(con.isClosed()==false)

  con.close();

   }

   /**

  * 查询无参数的Sql

  * @return Result

  * @throws Exception

  * @author 贾小仙

  */

   public Result runSelectSql(String sql){

  Connection con=null;

  PreparedStatement pStatement=null;

  ResultSet resultSet=null;

  Result result=null;

  try {

con=getOracleConnection();

pStatement=con.prepareStatement(sql);

resultSet=pStatement.executeQuery();

result=ResultSupport.toResult(resultSet);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

freeResources(resultSet, pStatement, con);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

  return result;

   }

   /**

* 查询有参数的Sql

* @return Result

* @throws Exception

* @author 贾小仙

*/

   public Result runSelectSql(String sql,Object[] params){

  Connection con=null;

  PreparedStatement pStatement=null;

  Result result=null;

  ResultSet resultSet=null;

  try {

con=getOracleConnection();

pStatement=con.prepareStatement(sql);

for(int i=0;i<params.length;i++){

pStatement.setObject(i+1, params[i]);

}

resultSet=pStatement.executeQuery();

result=ResultSupport.toResult(resultSet);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

freeResources(resultSet, pStatement, con);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

  return result;

   }

}


推荐阅读:
  1. 使用JDBC获取Oracle连接时报错
  2. JAVA JDBC连接oracle 测试代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

oracle package return

上一篇:VR实现电影里超帅气的上弹匣

下一篇:Exchange 2016部署实施案例篇-07.Exchange日常运维检查

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》