oracle的JDBC连接
admin
2023-05-10 15:21:39
0

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

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;

   }

}


相关内容

热门资讯

沈伯洋不认“抗中保台”?徐巧芯... 海峡导报综合报道 民进党13日正式征召民进党民代沈伯洋参选台北市长,蓝绿对决态势至此成形。对此,国民...
宝马线上股东大会“技术故障”背... 【文/观察者网 张家栋 编辑/高莘】当地时间5月13日,据德国《世界报》报道,宝马集团年度股东大会因...
线上预约+线下收购,十部门联合... 2026年夏粮旺季收购即将全面启动。近日,国家发展改革委、国家粮食和物资储备局等十部门联合发出通知,...
赖清德再度缺席弹劾案审查会,国... 台民意机构于5月14日继续召开针对台湾地区领导人赖清德弹劾案的第二次审查会,赖清德与13日一样未出席...
伊朗学者:特朗普访华或影响美伊... 美国总统特朗普访问中国之际,美伊停火谈判和霍尔木兹海峡危机仍未解决。伊朗学者法拉吉扎德在接受凤凰卫视...
日本版星链军事侦察网启用 日本防卫政务官若林洋平今天(5月14日)在日本国会参议院内阁委员会上称,为确保作为“反击能力”手段的...
外交部:美方务必慎之又慎处理台... 5月14日,外交部发言人郭嘉昆主持例行记者会。有记者就台湾问题提问,郭嘉昆表示,习近平主席与特朗普总...
世卫组织:全球卫生进展失衡 急... 世界卫生组织13日发布的《2026年世界卫生统计》报告显示,全球卫生领域近年来虽取得一定进展,但进展...
储量2356.87亿立方米!我... 【大河财立方消息】据中国石化,中国石化四川资阳东峰页岩气田2356.87亿立方米探明地质储量通过自然...
英国拟将中国敬业集团旗下的英钢... 有记者问:近日有英国媒体报道称,英国政府将通过相关立法,将中国敬业集团旗下的英国钢铁公司国有化。请问...