package com.icool.common.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql. Statement;
/**
*
* @author ZH_Q
* * @version 1.0
*/
public class GetConn {
private Connection conn= Local Database
public GetConn() {
this.Clfn = "oracle.jdbc.driver.OracleDriver";
this.dmName = "jdbc:oracle:thin:@localhost. 1521:orcl";
this.usPwd = "q792002998";
this.usName = "system";
}
/**
* @return Database Connection Object
*/
public Connection getConn() {
try
{
Class.forName(Clfn);
conn = DriverManager.getConnection(dmName,usName,usPwd );
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* @return The result set queried based on the SQL statement
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException {
conn = getConn();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
/**
* @return affects the number of rows of data
* @throws SQLException
*/
public int executeUpdate(String sql) throws SQLException {
Statement stmt = null;
int i = 0;
getConn();
stmt = conn. createStatement();
i = stmt.executeUpdate(sql);
return i;
}
/**
* @return Returns a precompiled object based on the SQL statement
* @throws SQLException
*/
public PreparedStatement PreparedStatement(String sql) throws SQLException {
PreparedStatement pstmt = null;
getConn();
pstmt= conn.prepareStatement(sql);
return pstmt;
}
/**
* @param Close the database connection
* @ throws DataBaseExpection
*/
public void close(){
if(conn!=null) {
try
{
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* @param Sets whether or not to autocommit
* @throws SQLException
*/
public void setAutoCommit(boolean b) throws SQLException {
getConn();
conn.setAutoCommit(b);
}
public void commit() throws SQLException {
getConn();
conn.commit();
}
public void rollback() throws SQLException {< /p>
getConn();
conn.rollback();
}
}
}