JavaSercer Pages public int getInt( ) { return (Verizon web hosting)

January 24, 2007 on 8:00 pm | In Java |

JavaSercer Pages public int getInt( ) { return value; } public String getString( ) { return String.valueOf(value); } } An application that uses the SQLCommandBean can create Value objects and set the bean’s properties like this: SQLCommandBean sqlBean = new SQLCommandBean( ); sqlBean.setConnection(ds.getConnection( )); String sqlValue = “SELECT * FROM MyTable WHERE IntCol = ? AND TextCol = ?”; sqlBean.setSqlValue(sqlValue); Vector values = new Vector( ); values.addElement(new IntValue(10)); values.addElement(new StringValue(”Hello!”)); sqlBean.setValues(values); One of two methods in the SQLCommandBean is used to execute the SQL statement: the executeQuery( ) method for a SELECT statement, and the executeUpdate( ) method for all other types of statements. Example 17.6 shows the executeQuery( ) method. Example 17.6. The SQLCommandBean’s executeQuery( ) Method public Vector executeQuery( ) throws SQLException, UnsupportedTypeException { Vector rows = null; 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); rs = pstmt.executeQuery( ); } else { // Use a regular Statement stmt = conn.createStatement( ); rs = stmt.executeQuery(sqlValue); } // Save the result in a Vector of Row object rows = toVector(rs); } 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 rows; } If the values property is set, a JDBC PreparedStatement is needed to associate the values with the question mark placeholders in the SQL statement. A method named setValues( ) takes care of setting all values, using the appropriate JDBC method for the datatype represented by each Value object. If the values property is not set, a regular JDBC Statement is created instead. In both cases, the JDBC driver is asked to execute the statement, and the resulting ResultSet is turned into a Vector with Row objects by the toVector( ) method. The Vector is then returned to the caller. You may wonder why the ResultSet is not returned directly instead of creating a Vector with Row objects. The reason is that a ResultSet is tied to the Connection that was used to generate it. When the Connection is closed or used to execute a new SQL statement, all open ResultSet objects for the Connection are released. You must therefore make sure to save the information from the ResultSet in a new data structure before reusing the Connection or returning it to the pool. page 244

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Sandzak.com tomcat web hosting provider

No Comments yet

TrackBack URI

Sorry, the comment form is closed at this time.