Kimi's NutShell

          我荒廢的今日,正是昨日殞身之人祈求的明日

          BlogJava 新隨筆 管理
            141 Posts :: 0 Stories :: 75 Comments :: 0 Trackbacks

          package classmate;
          import java.sql.*;

          import javax.sql.DataSource;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          public class DataStore {
          ?
          ?DataSource dataSource = null;
          ?private static Connection conn = null;
          ?private Statement stmt = null;
          ?private boolean hadErrors = false;
          ?
          ?//construct
          ?public DataStore(DataSource dataSource) throws Exception{
          ??this.dataSource = dataSource;
          ??conn = dataSource.getConnection();
          ??stmt = conn.createStatement();
          ?}
          ?
          ?//invoke transaction
          ?public void beginTransaction() throws SQLException{
          ??conn.setAutoCommit(false);
          ?}
          ?
          ?//perform transaction
          ?public void commitTransaction() throws SQLException{
          ??//hadErrors = false
          ??if(!hadErrors){
          ???conn.commit();
          ??}
          ??else{? //hadErrors = true
          ???conn.rollback();
          ???hadErrors = false;
          ??}
          ??hadErrors = false;
          ??conn.setAutoCommit(true);
          ?}
          ?
          ?//mark error
          ?public void ErrorOccur(){
          ??hadErrors = true;
          ?}
          ?
          ?//execute insert,delete,update
          ?synchronized public void execute(String sql) throws SQLException{
          ??if(stmt != null)
          ???stmt.executeUpdate(sql);
          ??else{
          ???Log log = LogFactory.getLog("mylog");
          ???log.error("fail to execute sql");
          ??}
          ?}
          ?
          ?//execute select
          ?synchronized public ResultSet read(String sql) throws SQLException{
          ??if(stmt != null){
          ???ResultSet tmp = null;
          ???tmp = stmt.executeQuery(sql);
          ???return tmp;
          ??}else{
          ???return null;
          ??}
          ?}
          ?
          ?// count of result
          ?synchronized public int readCount(String sql) throws SQLException{
          ??int nCount = 0;
          ??try{
          ???if(stmt != null){
          ????ResultSet tmp = null;
          ????tmp = stmt.executeQuery(sql);
          ????if(tmp != null && tmp.next()){
          ?????nCount = tmp.getInt(1);
          ????}else{
          ?????nCount = 0;
          ????}
          ???}
          ??}catch(SQLException e){
          ???nCount = 0;
          ??}
          ??return nCount;
          ?}
          ?
          ?// close db
          ?synchronized public void stop(){
          ??try{
          ???if(conn != null){
          ????conn.close();
          ????stmt.close();
          ???}
          ??}catch(Exception e){
          ???Log log = LogFactory.getLog("mylog");
          ???log.error("fail to close db- DataStore.java");
          ??}finally{
          ???conn = null;
          ??}
          ?}
          }

          還在研究,貌似DCL的問題還存在。

          posted on 2007-10-24 16:35 Kimi 閱讀(483) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 拉孜县| 临夏市| 道真| 临猗县| 六枝特区| 开封县| 九龙坡区| 壶关县| 石楼县| 龙口市| 井冈山市| 阳西县| 益阳市| 新干县| 鄂托克前旗| 恩平市| 宜兰县| 全州县| 宜丰县| 扎鲁特旗| 奎屯市| 宣汉县| 宜兴市| 湾仔区| 邓州市| 顺平县| 甘泉县| 新郑市| 葵青区| 余姚市| 嘉峪关市| 穆棱市| 乐业县| 礼泉县| 天祝| 邢台县| 遵化市| 望奎县| 通州市| 巴林右旗| 衡山县|