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. SNMP协议以及著名的MIB详解
  2. codeforces 742E (二分图着色)
  3. tensorflow 学习(一)
  4. 你或许不了解的C++函数调用(1)
  5. 让Sqlite脱离VC++ Runtime独立执行
  6. toggle的使用心得
  7. Android自定义控件之TextView
  8. wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)
  9. ERR Unsupported CONFIG parameter: notify-keyspace-events; nested exception is redis.clients.jedis.exceptions.JedisDataException
  10. MySQL5.7下面,误操作导致的drop table db1.tb1; 的恢复方法:
  11. 【面试】我是如何在面试别人Spring事务时“套路”对方的
  12. python中网络编程
  13. svn 安装及更新web库
  14. CXF develop Webserice Tuturial
  15. PowerDesigner 缺省值 引号 问题
  16. linux 用户配制文件 用户增加及删除 以及用户属于的更改
  17. mac终端常用命令
  18. 开包即食的教程带你浅尝最新开源的C# Web引擎Blazor
  19. 动画隐藏UITabBarController与UINavigationController
  20. Android -- 触摸Area对焦区域(更新)

热门文章

  1. 33 | 无实例无真相:基于LoadRunner实现企业级服务器端性能测试的实践(下)
  2. Log2Net日志查询网站代码解析
  3. Codeforces Gym101503E:XOR-omania(构造+思维)
  4. JAVA超级简单的爬虫例子(1)
  5. GitHub使用整理——关于上传Keil工程一些注意的点
  6. spark 源码分析之十六 -- Spark内存存储剖析
  7. python爬虫笔记之爬取足球比赛赛程
  8. UNICODE环境下读写txt文件操作
  9. ybc云计算思维
  10. 网络框架,互联网的组成,OSI七层协议,抽象层