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 周锐 阅读(847) 评论(0)  编辑  收藏 所属分类: JavaSQL Server

只有注册用户登录后才能发表评论。


网站导航: