posts - 431,  comments - 344,  trackbacks - 0

          Database

          import Java.sql.Connection;

          import Java.sql.ResultSet;

          import Java.sql.Statement;

          import Java.sql.SQLException;

          import javax.naming.Context;

          import javax.naming.InitialContext;

          import javax.sql.DataSource;

           

          public class Database

          {

          private Connection m_conn=null;

          private Statement m_stmt=null;

          private boolean isAutoCommit;

           

          public Database() throws Exception

          {

              try

              {

               Context initCtx=new InitialContext();

               Context envCtx=(Context)initCtx.lookup(“java:comp/env”);

               DataSource ds=(DataSource)envCtx.lookup(“jdbc/SqlServer”);

                m_conn=ds.getConnection();

                initCtx.close();

                envCtx.close();

          }

          catch(Exception ex)

          {

                 ex..printStackTrace();

                 System.out.println(“Create Connection Error!”);

                 throw ex;

          }

          }

           

          public void close()  throws SQLException

          {

               try

               {

                     if(m_stmt !=null) m_stmt.close();

                     if(m_conn !=null) m_conn.close();

          }

          catch(SQLException ex)

          {

               ex.printStackTrace();

               System.out.println(“Close Connection Error!”);

               throw ex;

          }

          }

           

          public ResultSet executeQuery1(String sql) throws SQLException

          {

                 try

          {

              m_stmt=m_conn.CreateStatement();

              return m_stmt.executeQuery(sql);

          }

          catch(SQLException ex)

          {

               ex.printStackTrace();

               System.out.println(“Execute SQL:’”+sql+”’ Error!’”);

               System.out.println(ex.toString());

               System.out.println(ex.getMessage());

               throw ex;

          }

          }

           

          public ResultSet executeQuery2(String sql) throws SQLException

          {

                  try

          {

              m_stmt=m_conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

          ResultSet.CONCUR_READ_ONLY);

                              return m_stmt..executeQuery(sql);

          }

          catch(SQLException ex)

          {

               ex.printStackTrace();

               System.out.println(“Execute SQL:’”+sql+”’ Error!’”);

               System.out.println(ex.toString());

               System.out.println(ex.getMessage());

               throw ex;

          }

          }

           

          public int executeUpdate(String sql) throws SQLException

          {

                try

          {

                Statement stmt=m_conn.createStatment();

                int rt=stmt.executeUpdate(sql);

                stmt.close();

                return rt;

          }

          catch(SQLException ex)

          {

               ex.printStackTrace();

               System.out.println(“Execute SQL:’”+sql+”’ Error!’”);

               System.out.println(ex.toString());

               System.out.println(ex.getMessage());

               throw ex;

          }

          }

          // 開始新事務

          public void beginTrans() throws SQLException    

          {

                 try

          {

               isAutoCommit=m_conn.getAutoCommit(); // 獲得當前自動提交狀態

               m_conn.setAutoCommit(false);// 禁止自動提交

          }

          catch(SQLException ex)

          {

               ex.printStackTrace();

               System.out.println("BeginTrans Error!");

               throw ex;

          }

          }

          // 保存任何更改并結束當前事務。它也可能啟動新事務

          public void commitTrans() throws SQLException 

          commit() 提交從上一次提交 / 回滾操作后的更改,使之成為永久的更改,并釋放 Connection 當前保持的任何數據庫鎖。 只有當禁止自動提交時可以使用該方法。

          {

                 try

          {

                               m_conn.commit(); 

               m_conn.setAutoCommit(isAutoCommit);

          }

          catch(SQLException ex)

          {

               ex.printStackTrace();

               System.out.println("CommitTrans Error!");

               throw ex;

          }

          }

          // 取消當前事務中所作的任何更改并結束事務。它也可能啟動新事務。

          public void rollbackTrans() throws SQLException

          {

                 try

          {

               m_conn.rollback();

               m_conn.setAutoCommit(isAutoCommit);

          }

          catch(SQLException ex)

          {

                ex.printStackTrace();

               System.out.println("RollbackTrans Error!");

               throw ex;

          }

          }

          }

          posted on 2006-10-18 13:31 周銳 閱讀(859) 評論(0)  編輯  收藏 所屬分類: JavaSQL Server
          主站蜘蛛池模板: 博罗县| 墨玉县| 泸西县| 安丘市| 金堂县| 临高县| 海阳市| 浦东新区| 黎城县| 乌鲁木齐县| 乌苏市| 乐平市| 三门峡市| 侯马市| 铜山县| 灵璧县| 应城市| 黎川县| 霍山县| 龙泉市| 凤阳县| 通辽市| 四平市| 甘孜| 靖州| 扶绥县| 化德县| 德阳市| 威海市| 公主岭市| 法库县| 宜川县| 灵宝市| 望都县| 安西县| 阳城县| 将乐县| 云安县| 任丘市| 禄劝| 龙口市|