溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

oracle的JDBC連接

發布時間:2020-07-27 04:02:08 來源:網絡 閱讀:508 作者:jiazhipeng12 欄目:關系型數據庫

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;

   }

}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女