package com.xian.jdbc;
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),容縣企業(yè)網(wǎng)站建設(shè),容縣品牌網(wǎng)站建設(shè),網(wǎng)站定制,容縣網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,容縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
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數(shù)據(jù)庫
* @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;
}
/**
* 關(guān)閉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();
}
/**
* 查詢無參數(shù)的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;
}
/**
* 查詢有參數(shù)的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; } }
分享標(biāo)題:oracle的JDBC連接
本文來源:http://weahome.cn/article/pscgdc.html