www.pudn.com > town-1[1].0.4.rar > ExecuteStatement.java
package com.workingdogs.town; import java.io.*; import java.sql.*; /* Town, a Java JDBC abstraction layer Copyright (C) 1999 Serge Knystautas, Jon Stevens This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * A very simple class to execute SQL that does not return data. While we recommend using * the TableDataSet object for inserts, deletes, and often for updates, we recognize there will * be some SQL such as stored procedures or batch inserts, deletes, and updates that should * be handled inside the database for performance reason. During object construction, this object will automatically execute the SQL statement provided.Here is an example to delete all records from table_name
new ExecuteStatement (connectionString, "DELETE * from table_name");The object automatically interacts with the database connection pool to use a connection and then return it to the pool. *
* * @author Serge Knystautas sergek@lokitech.com * @version 1.0 */ public class ExecuteStatement extends DBConnection { String sqlstatement = null; /** * Executes a given statement on the given connection. This object immediately becomes useless * after construction... it's meant as a one time execution. * @param connString java.lang.String * @param tableName java.lang.String * @exception com.lokitech.town.DataSetException The exception description. * @exception com.lokitech.town.ConnectionException The exception description. */ public ExecuteStatement(String driver, String connString, String sqlstatement) throws DataSetException, ConnectionException { super(driver, connString); this.sqlstatement = sqlstatement; execute (); close (); } /** * Executes a given statement on the given connection. This object immediately becomes useless * after construction... it's meant as a one time execution. * @param connString java.lang.String * @param tableName java.lang.String * @exception com.lokitech.town.DataSetException The exception description. * @exception com.lokitech.town.ConnectionException The exception description. */ public ExecuteStatement(String driver, String connString, String username, String password, String sqlstatement) throws DataSetException, ConnectionException { super(driver, connString, username, password); this.sqlstatement = sqlstatement; execute (); close (); } /** * Executes the SQL statement */ private void execute () throws ConnectionException { try { if (stmt == null) { stmt = connection().createStatement(); stmt.execute (sqlstatement); } } catch (SQLException e) { if (stmt != null) { try { stmt.close(); } catch (SQLException sqle) { throw new ConnectionException (sqle); } } //throw new SQLException (e.getMessage()); throw new ConnectionException (e); } } }