백그라운드 dao BaseDao
2432 단어 seda
package cn.service.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 。
*/
public class BaseDao {
private static String driver ="oracle.jdbc.driver.OracleDriver";//
private static String url = "jdbc:oracle:thin:@localhost:1521:oracle11";// URL
private static String user = "newegg"; //
private static String password = "newegg"; //
/**
* 。
*/
public Connection getConnection() {
Connection conn = null;//
//
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();//
}
return conn;//
}
/**
* 。
* @param conn
* @param stmt Statement
* @param rs
*/
public void closeAll(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
// ,
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Statement ,
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// ,
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 、 、 SQL
* @param sql
* @param pars
* @return int
*/
public int executeUpdate(String sql, Object[] pars) {
int rowCount =0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(sql);
if(pars!=null){
for(int i=0;i<pars.length;i++){
pstmt.setObject(i+1, pars[i]);
}
}
rowCount = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, null);
}
return rowCount;
}
}