一个类引发的回忆
没事查看很早以前的备份文件,发现7年前写的一个连库类,想起了当年的点点滴滴,原来代码也是有生命的。
package
com.itgenius.netoa.admin;
import java.sql. * ;
import javax.sql. * ;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming. * ;
import java.util.List;
public class EJBDAO implements ApplicationUtil {
private Connection conn = null ;
private Statement st = null ;
private PreparedStatement pst = null ;
private CallableStatement cs = null ;
private ResultSet rs = null ;
private DataSource ds = null ;
private boolean isCorrect = false ;
public EJBDAO() {
}
public void getConnection(){
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup(dsJndi);
conn = ds.getConnection();
} catch (NamingException ex) {
ex.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
}
public void getConnection(String dburl,String dbdriver,String dbusername,String dbpasswd){
}
public void getStatement(){
try {
getConnection();
st = conn.createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// 得到预备状态通道
public void getPStatement(String sql){
try {
getConnection();
pst = conn.prepareStatement(sql);
} catch (SQLException ex) {
}
}
// 执行查询得到结果集
public ResultSet getResultSet(String sql){
try {
getStatement();
rs = st.executeQuery(sql);
} catch (SQLException ex) {
} finally {
return rs;
}
}
// 给预备状态通道中的sql的变量付值,然后执行
public boolean executePstatement(List list){
isCorrect = false ;
try {
for ( int i = 0 ; i < list.size(); i ++ ){
pst.setString((i + 1 ), list.get(i).toString());
}
pst.executeUpdate();
isCorrect = true ;
} catch (SQLException ex) {
} finally {
return isCorrect;
}
}
// 执行新增、修改、删除
public boolean doUpdate(String sql){
isCorrect = false ;
try {
getStatement();
st.executeUpdate(sql);
isCorrect = true ;
} catch (SQLException ex){
ex.printStackTrace();
} finally {
return isCorrect;
}
}
// 执行存储过程
public boolean doProcure(String pname,List list){
isCorrect = false ;
try {
getConnection();
cs = conn.prepareCall( " {call " + pname + " } " );
for ( int i = 0 ; i < list.size(); i ++ )
{
cs.setString((i + 1 ), list.get(i).toString());
}
cs.execute();
isCorrect = true ;
} catch (SQLException ex) {
} finally {
return isCorrect;
}
}
public void addBatch(List list){
try {
for ( int i = 0 ;i < list.size();i ++ ){
st.addBatch(list.get(i).toString());
}
} catch (Exception e){
}
}
public boolean ExecuteBatch(){
isCorrect = false ;
try {
st.executeBatch();
isCorrect = true ;
} catch (Exception e){
e.printStackTrace();
} finally {
Close();
return isCorrect;
}
}
// 关闭连接
public void Close(){
try {
if (rs != null )
{
rs.close();
}
if (st != null ){
st.close();
}
if (pst != null )
{
pst.close();
}
if (cs != null )
cs.close();
if (conn != null )
conn.close();
} catch (SQLException ex) {
}
}
}
import java.sql. * ;
import javax.sql. * ;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming. * ;
import java.util.List;
public class EJBDAO implements ApplicationUtil {
private Connection conn = null ;
private Statement st = null ;
private PreparedStatement pst = null ;
private CallableStatement cs = null ;
private ResultSet rs = null ;
private DataSource ds = null ;
private boolean isCorrect = false ;
public EJBDAO() {
}
public void getConnection(){
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup(dsJndi);
conn = ds.getConnection();
} catch (NamingException ex) {
ex.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
}
public void getConnection(String dburl,String dbdriver,String dbusername,String dbpasswd){
}
public void getStatement(){
try {
getConnection();
st = conn.createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// 得到预备状态通道
public void getPStatement(String sql){
try {
getConnection();
pst = conn.prepareStatement(sql);
} catch (SQLException ex) {
}
}
// 执行查询得到结果集
public ResultSet getResultSet(String sql){
try {
getStatement();
rs = st.executeQuery(sql);
} catch (SQLException ex) {
} finally {
return rs;
}
}
// 给预备状态通道中的sql的变量付值,然后执行
public boolean executePstatement(List list){
isCorrect = false ;
try {
for ( int i = 0 ; i < list.size(); i ++ ){
pst.setString((i + 1 ), list.get(i).toString());
}
pst.executeUpdate();
isCorrect = true ;
} catch (SQLException ex) {
} finally {
return isCorrect;
}
}
// 执行新增、修改、删除
public boolean doUpdate(String sql){
isCorrect = false ;
try {
getStatement();
st.executeUpdate(sql);
isCorrect = true ;
} catch (SQLException ex){
ex.printStackTrace();
} finally {
return isCorrect;
}
}
// 执行存储过程
public boolean doProcure(String pname,List list){
isCorrect = false ;
try {
getConnection();
cs = conn.prepareCall( " {call " + pname + " } " );
for ( int i = 0 ; i < list.size(); i ++ )
{
cs.setString((i + 1 ), list.get(i).toString());
}
cs.execute();
isCorrect = true ;
} catch (SQLException ex) {
} finally {
return isCorrect;
}
}
public void addBatch(List list){
try {
for ( int i = 0 ;i < list.size();i ++ ){
st.addBatch(list.get(i).toString());
}
} catch (Exception e){
}
}
public boolean ExecuteBatch(){
isCorrect = false ;
try {
st.executeBatch();
isCorrect = true ;
} catch (Exception e){
e.printStackTrace();
} finally {
Close();
return isCorrect;
}
}
// 关闭连接
public void Close(){
try {
if (rs != null )
{
rs.close();
}
if (st != null ){
st.close();
}
if (pst != null )
{
pst.close();
}
if (cs != null )
cs.close();
if (conn != null )
conn.close();
} catch (SQLException ex) {
}
}
}