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; } }
文章标题:oracle的JDBC连接
标题网址:http://kswsj.cn/article/pscgdc.html