Web Hosting Java, JSP, Tomcat 6, J2EE, Servlets, Struts, Jboss
Web hosting packages - JavaSercer Pages 17.3.3 The Action The final database
January 27, 2007 on 10:27 am | In Java | No Comments JavaSercer Pages 17.3.3 The
Hint: This post is supported by Gama php5 hosting services
JavaSercer Pages 17.3.2 The Action A set of (Cms hosting)
January 27, 2007 on 4:29 am | In Java | No Comments JavaSercer Pages 17.3.2 The
Hint: This post is supported by Gama php5 hosting services
JavaSercer Pages Example 17.18. The QueryTag’s execute( ) (Christian web host)
January 26, 2007 on 11:20 pm | In Java | No Comments JavaSercer Pages Example 17.18. The QueryTag’s execute( ) Method public Object execute(SQLCommandBean sqlCommandBean) throws SQLException, UnsupportedTypeException { return sqlCommandBean.executeQuery( ); } This method simply calls the bean’s executeQuery( ) method. The UpdateTag tag handler uses the bean’s executeUpdate( ) method instead, and wraps the returned int in an Integer object, as shown in Example 17.19. Example 17.19. The UpdateTag’s execute( ) Method public Object execute(SQLCommandBean sqlCommandBean) throws SQLException, UnsupportedTypeException { return new Integer(sqlCommandBean.executeUpdate( )); } The reason for wrapping the int in an Integer is that only real objects can be saved as JSP scope objects; primitive types are not supported. The
Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services
Jboss hosting - JavaSercer Pages Example 17.15. The DBTag’s doAfterBody( )
January 26, 2007 on 7:04 pm | In Java | No Comments JavaSercer Pages Example 17.15. The DBTag’s doAfterBody( ) Method public int doAfterBody( ) throws JspException { sqlValue = bodyContent.getString( ); return SKIP_BODY; } The SQL statement may contain question marks as placeholders for values set by nested value actions. As you will see later, the value actions create the appropriate Value subclass and call the DBTag ’s addValue( ) method, shown in Example 17.16. Example 17.16. The DBTag’s addValue( ) Method public void addValue(Value value) { if (values == null) { values = new Vector( ); } values.addElement(value); } This method creates a Vector to hold all values the first time it’s called, and then adds the Value object to the Vector. When called by subsequent value action tag handlers, the Value objects are simply added to the list. The real processing happens in the doEndTag( ) method, shown in Example 17.17. This method is called by the container when the action element’s body has been processed and the end tag is encountered. Example 17.17. The DBTag’s doEndTag( ) Method public int doEndTag( ) throws JspException { Connection conn = getConnection( ); sqlCommandBean.setConnection(conn); sqlCommandBean.setSqlValue(sqlValue); sqlCommandBean.setValues(values); Object result = null; try { result = execute(sqlCommandBean); } catch (SQLException e) { … } catch (UnsupportedTypeException e) { … } finally { … } // Save the result with the specified id in the specified scope if (id != null) { pageContext.setAttribute(id, result, scope); } return EVAL_PAGE; } The private getConnection( ) method is used to get a Connection. The Connection is retrieved either from the DataSource specified by the dataSource attribute value for the
Hint: This post is supported by Gama web hosting php mysql provider
JavaSercer Pages 17.3.1 (Php5 hosting) The and Actions The and
January 26, 2007 on 1:11 pm | In Java | No Comments JavaSercer Pages 17.3.1 The
Hint: This post is supported by Gama web hosting php mysql provider
Phpnuke hosting - JavaSercer Pages The Column class is an abstract
January 26, 2007 on 6:32 am | In Java | No Comments JavaSercer Pages The Column class is an abstract class, very similar to the Value class shown in Example 17.4. It contains access methods for all datatypes, with default implementations that throw an UnsupportedConversionException . Each subclass provides a real implementation of the access method corresponding to its type, plus the getString( ) method. Example 17.13 shows the IntColumn class. Example 17.13. The IntColumn Class package com.ora.jsp.sql.column; import com.ora.jsp.sql.Column; public class IntColumn extends Column { private int value; public IntColumn(String name, int value) { super(name); this.value = value; } public int getInt( ) { return value; } public String getString( ) { return String.valueOf(value); } } The constructor takes the column name and value as arguments. The name is used to initialize the Column superclass and is returned by its getName( ) method. 17.3 Developing Generic Database Custom Actions The database custom actions introduced in Chapter 9 can be used like this in a JSP page:
Hint: If you are looking for good and high quality web space to host and run your java application check Vision java web hosting services
Budget web hosting - JavaSercer Pages Example 17.12. The Row’s Column Value
January 25, 2007 on 11:13 pm | In Java | No Comments JavaSercer Pages Example 17.12. The Row’s Column Value Access Methods public BigDecimal getBigDecimal(int columnIndex) throws NoSuchColumnException, UnsupportedConversionException { Column col = null; try { col = columns[columnIndex - 1]; } catch (ArrayIndexOutOfBoundsException e) { throw new NoSuchColumnException(String.valueOf(columnIndex)); } return col.getBigDecimal( ); } public BigDecimal getBigDecimal(String columnName) throws NoSuchColumnException, UnsupportedConversionException { return getBigDecimal(getIndex(columnName)); } public boolean getBoolean(int columnIndex) throws NoSuchColumnException, UnsupportedConversionException { Column col = null; try { col = columns[columnIndex - 1]; } catch (ArrayIndexOutOfBoundsException e) { throw new NoSuchColumnException(String.valueOf(columnIndex)); } return col.getBoolean( ); } public boolean getBoolean(String columnName) throws NoSuchColumnException, UnsupportedConversionException { return getBoolean(getIndex(columnName)); } … public String getString(int columnIndex) throws NoSuchColumnException { Column col = null; try { col = columns[columnIndex - 1]; } catch (ArrayIndexOutOfBoundsException e) { throw new NoSuchColumnException(String.valueOf(columnIndex)); } return col.getString( ); } public String getString(String columnName) throws NoSuchColumnException { return getString(getIndex(columnName)); } All these methods locate the Column subclass instance specified by the argument and call the corresponding method on the instance. Except for the getString( ) method, this call results in an UnsupportedConversionException if the column is not of the requested type. All types can be converted to a String, however, so a getString( ) call is successful provided that the requested column exists. page 248
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services
JavaSercer Pages 17.2.2 The Row and Column Classes (Windows web hosting)
January 25, 2007 on 3:41 pm | In Java | No Comments JavaSercer Pages 17.2.2 The Row and Column Classes Let’s now look at the Row and Column classes. Example 17.10 shows a part of the Row class constructor. Example 17.10. The Row Class Constructor package com.ora.jsp.sql; import java.util.*; import java.sql.*; import java.sql.Date; import java.math.*; import com.ora.jsp.sql.column.*; public class Row { private Column[] columns; public Row(ResultSet rs) throws SQLException, UnsupportedTypeException { ResultSetMetaData rsmd = rs.getMetaData( ); int cols = rsmd.getColumnCount( ); columns = new Column[cols]; // Note! Columns are numbered from 1 in the ResultSet for (int i = 1; i <= cols; i++) { int type = rsmd.getColumnType(i); switch (type) { case Types.DATE: columns[i - 1] = new DateColumn(rsmd.getColumnName(i), rs.getDate(i)); break; case Types.TIME: columns[i - 1] = new TimeColumn(rsmd.getColumnName(i), rs.getTime(i)); break; ... default: throw new UnsupportedTypeException("Unsupported SQL " + "data type: " + type); } } } The Row class keeps all column values as an array of Column objects. The constructor is called with a ResultSet that has been positioned at a new row by the caller using the next( ) method. It loops through all columns in the row and creates a Column subclass instance for each. The column's datatype, retrieved from the ResultSetMetaData object, is used to decide which Column subclass to create. Similarly to the Value class structure, the Column class structure contains subclasses corresponding to JDBC column datatypes, as shown in Figure 17.3. Two methods provide access to the number of columns and the array of Column objects, shown in Example 17.11. Example 17.11. The Row's getColumnCount( ) and getColumns( ) Methods public int getColumnCount( ) { return columns.length; } public Column[] getColumns( ) { return columns; } Another set of methods can be used to retrieve the value of an individual column, given its name or index. This set of methods contains one pair per supported datatype. Example 17.12 shows the methods for the BigDecimal, boolean, and String types. page 247
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services
JavaSercer Pages The executeUpdate( ) method, shown in
January 25, 2007 on 7:38 am | In Java | No CommentsJavaSercer Pages The executeUpdate( ) method, shown in Example 17.9, is very similar to the executeQuery( ) method. Example 17.9. The SQLCommandBean’s executeUpdate( ) Method public int executeUpdate( ) throws SQLException, UnsupportedTypeException { int noOfRows = 0; ResultSet rs = null; PreparedStatement pstmt = null; Statement stmt = null; try { if (values != null && values.size( ) > 0) { // Use a PreparedStatement and set all values pstmt = conn.prepareStatement(sqlValue); setValues(pstmt, values); noOfRows = pstmt.executeUpdate( ); } else { // Use a regular Statement stmt = conn.createStatement( ); noOfRows = stmt.executeUpdate(sqlValue); } } finally { try { if (rs != null) { rs.close( ); } if (stmt != null) { stmt.close( ); } if (pstmt != null) { pstmt.close( ); } } catch (SQLException e) { // Ignore. Probably caused by a previous // SQLException thrown by the outer try block. } } return noOfRows; } The main difference is that the executeUpdate( ) method is used to execute SQL statements that do not return rows, only the number of rows affected by the statement. Examples of such statements are UPDATE, INSERT, and DELETE. In the same way as the executeQuery( ) method, a PreparedStatement is created and initialized with the values defined by the values property, if set. Otherwise a regular Statement is used. The statement is executed and the number of affected rows is returned to the caller. page 246
Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Sandzak.com j2ee web hosting services
JavaSercer Pages The code for creating (Domino hosting) the PreparedStatement
January 25, 2007 on 12:28 am | In Java | No Comments JavaSercer Pages The code for creating the PreparedStatement or Statement object and executing the statement is enclosed in a try/finally block. This is important, because if something fails (due to an invalid SQL statement, for instance), the JDBC methods throw an SQLException . You want the exception to be handled by the application using the SQLCommandBean, but first you must make sure that all JDBC resources are released and the Connection object is returned to the pool. Using a try block with a finally clause but no catch clause gives this behavior. If an exception is thrown, the finally clause is executed, and then the exception is automatically thrown to the object that called the executeQuery( ) method. In the finally clause, the ResultSet object and either the PreparedStatement or Statement object are closed. It should be enough to close the statement object according to the JDBC specification (closing the statement should also close the ResultSet associated with the statement), but doing it explicitly doesn’t hurt and makes the code work even with a buggy JDBC driver. Example 17.7 shows a part of the setValues( ) method. Example 17.7. The SQLCommandBean’s setValues( ) Method private void setValues(PreparedStatement pstmt, Vector values) throws SQLException { for (int i = 0; i < values.size( ); i++) { try { Value v = (Value) values.elementAt(i); // Set the value using the method corresponding to // the type. // Note! Set methods are indexed from 1, so we add // 1 to i if (v instanceof BigDecimalValue) { pstmt.setBigDecimal(i + 1, v.getBigDecimal( )); } else if (v instanceof BooleanValue) { pstmt.setBoolean(i + 1, v.getBoolean( )); } ... } catch (UnsupportedConversionException e) { // Can not happen here since we test the type first } } } The setValue( ) method loops through all elements in the Vector with values. For each element, it tests which Value subclass it is and uses the corresponding JDBC method to set the value for the PreparedStatement object. You may wonder why a PreparedStatement is used here, since it's used only once. It's true that a PreparedStatement is intended to be reused over and over again to execute the same SQL statement with new values. But it offers a convenient solution to the problem of different syntax for values of type date/time and numbers when represented by a string literal. When a PreparedStatement is used, the variable values in the SQL statement can be represented by Java variables of the appropriate types without worrying about what literal representation a certain JDBC driver supports. So even though it's used only once, a PreparedStatement still has an advantage over a regular Statement. The toVector( ) method is shown in Example 17.8. Example 17.8. The SQLCommandBean's toVector( ) Method private Vector toVector(ResultSet rs) throws SQLException, UnsupportedTypeException { Vector rows = new Vector( ); while (rs.next( )) { Row row = new Row(rs); rows.addElement(row); } return rows; } This method simply walks through the ResultSet and adds a new Row object for each row to a Vector that it then returns. As you will see later, the Row constructor reads all column values and creates a Column object for each. page 245
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services
...I
just wanted to take the time to say "Thank you!" for our new webmail
system. It's great! Thanks for taking such good care of us.
Thanks
for helping me out. Just for the record, Webhostingjava.net has been a great
web host! So far your support and handling of questions has far
exceeded that of a "larger web hosting company".
I
would like to thank you for helping me with my domain...You have
shown me great patience and professionalism. I would not hesitate to
recommend you to my clients.