JDBC connection of oracle
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 {
/ * *
* connect to ora database
* @ return con
* @ throws ClassNotFoundException
* @ throws SQLException
* @ author Jia Xiaoxian
, /
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
}
/ * *
* close the ora connection channel
* @ return
* @ throws SQLException
* @ author Jia Xiaoxian
, /
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 ()
}
/ * *
* query Sql with no parameters
* @ return Result
* @ throws Exception
* @ author Jia Xiaoxian
, /
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
}
/ * *
* query Sql with parameters
* @ return Result
* @ throws Exception
* @ author Jia Xiaoxian
, /
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 iTuno Bandi)