沒事查看很早以前的備份文件,發現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)?{
??????}
??}
}