servletjsp一些应用程序

源代码在线查看: transactionbean.java

软件大小: 442 K
上传用户: awake2
关键词: servletjsp 应用程序
下载地址: 免注册下载 普通下载 VIP

相关代码

				package coreservlets.beans;
				
				import java.io.*;
				import java.sql.*;
				import java.util.*;
				import coreservlets.*;
				
				/** Bean for performing JDBC transactions. After specifying the
				 *  the connection, submit a block of SQL statements as a
				 *  single transaction by calling execute. If an SQLException
				 *  occurs, any prior statements are automatically rolled back.
				 *  
				 *  Taken from Core Servlets and JavaServer Pages 2nd Edition
				 *  from Prentice Hall and Sun Microsystems Press,
				 *  http://www.coreservlets.com/.
				 *  © 2003 Marty Hall and Larry Brown.
				 *  May be freely used or adapted.
				 */
				
				public class TransactionBean {
				  private Connection connection;
				
				  public void setConnection(Connection connection) {
				    this.connection = connection;
				  }
				
				  public void setConnection(String driver, String url,
				                            String username, String password) {
				    setConnection(ConnectionInfoBean.getConnection(
				                     driver, url, username, password));
				  }
				
				  public Connection getConnection() {
				    return(connection);
				  }
				
				  public void execute(List list) throws SQLException {
				     execute((String[])list.toArray(new String[list.size()]));
				  }
				
				  public void execute(String transaction)
				      throws SQLException {
				    execute(new String[] { transaction });
				  }
				
				  /** Execute a block of SQL statements as a single
				   *  transaction.  If an SQLException occurs, a rollback
				   *  is attempted and the exception is thrown.
				   */
				
				    public void execute(String[] transaction)
				      throws SQLException {
				    if (connection == null) {
				      throw new SQLException("No connection available.");
				    }
				    boolean autoCommit = connection.getAutoCommit();
				    try {
				      connection.setAutoCommit(false);
				      Statement statement = connection.createStatement();
				      for(int i=0; i				        statement.execute(transaction[i]);
				      }
				      statement.close();
				    } catch(SQLException sqle) {
				      connection.rollback();
				      throw sqle;
				    } finally {
				      connection.commit();
				      connection.setAutoCommit(autoCommit);
				    }
				  }
				
				  public void close() {
				    if (connection != null) {
				      try {
				        connection.close();
				      } catch(SQLException sqle) {
				        System.err.println(
				          "Failed to close connection: " + sqle);
				      } finally {
				        connection = null;
				      }
				    }
				  }
				}       			

相关资源