package com.check.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private final String DRIVER="oracle.jdbc.driver.OracleDriver";
private final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
private final String USER="service";
private final String PWD="orcl";
private Connection conn=null;
private PreparedStatement pstm=null;
private ResultSet rs=null;
public Connection getConnection(){
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,USER,PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public int executeUpdate(String sql,Object[] params){
int ret=0;
conn=this.getConnection();
try {
pstm=conn.prepareStatement(sql);
this.setParams(pstm, params);
ret=pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
public int executeUpdate(String sql){
return executeUpdate(sql,null);
}
public ResultSet executeQuery(String sql,Object[] params){
conn=this.getConnection();
try {
pstm=conn.prepareStatement(sql);
this.setParams(pstm, params);
rs=pstm.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}//因为rs还要使用,所以不能close()
return rs;
}
public ResultSet executeQuery(String sql){
return executeQuery(sql,null);
}
private void setParams(PreparedStatement pstm,Object[] params) throws SQLException{
if(params!=null){
for(int i=0;i<params.length;i++){
pstm.setObject(i+1, params[i]);
}
}
}
public void closeStatement(){
try {
if(pstm!=null) pstm.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeAll(){
try {
if(rs!=null) rs.close();
if(pstm!=null) pstm.close();
if(conn!=null) conn.close();//取消引用
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private final String DRIVER="oracle.jdbc.driver.OracleDriver";
private final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
private final String USER="service";
private final String PWD="orcl";
private Connection conn=null;
private PreparedStatement pstm=null;
private ResultSet rs=null;
public Connection getConnection(){
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,USER,PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public int executeUpdate(String sql,Object[] params){
int ret=0;
conn=this.getConnection();
try {
pstm=conn.prepareStatement(sql);
this.setParams(pstm, params);
ret=pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
public int executeUpdate(String sql){
return executeUpdate(sql,null);
}
public ResultSet executeQuery(String sql,Object[] params){
conn=this.getConnection();
try {
pstm=conn.prepareStatement(sql);
this.setParams(pstm, params);
rs=pstm.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}//因为rs还要使用,所以不能close()
return rs;
}
public ResultSet executeQuery(String sql){
return executeQuery(sql,null);
}
private void setParams(PreparedStatement pstm,Object[] params) throws SQLException{
if(params!=null){
for(int i=0;i<params.length;i++){
pstm.setObject(i+1, params[i]);
}
}
}
public void closeStatement(){
try {
if(pstm!=null) pstm.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeAll(){
try {
if(rs!=null) rs.close();
if(pstm!=null) pstm.close();
if(conn!=null) conn.close();//取消引用
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}