You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Deepak Suldhal <dt...@yahoo.com> on 2005/03/09 22:58:12 UTC

Need to query database in tapestry - Please help.

 Hi
I have a form which has a datepicker and a radiogroup
consisting of 3 choices. user enters date and selects
on of the option and submits ok. based on which radion
button is on I present the next page. All this works
good.

I now just want to add one small function to this
before proceeding to the next page I just want to see
the count of records in the table. And this is where I
have problems,

I do not want to display anything on the form from
database. I just want to query and see if there are
records.

Please help me contruct this code. As it has to be in
production very soon.

Here is what I have tried. 


package com.test.fin46; 

import org.apache.tapestry.IRequestCycle; 
import org.apache.tapestry.html.BasePage; 

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 
import java.util.Date; 

public class Home extends BasePage { 
        private Date date; 
        public static final int _Mortgage = 1; 
        public static final int _LC       = 2; 
        public static final int _BOTH     = 3; 
        

        private int misses = 3; 
        private String error = ""; 
        private String errorMsg; 
        private int _count; 

        public void detach() { 
                misses = 3; 
                error = null; 
                super.detach(); 
        } 

        public int getMisses() { 
                return misses; 
        } 

        public void setMisses(int value) { 
                misses = value; 
        } 

        /** 
         * @return Returns the date. 
         */ 
        public Date getDate() { 
                return date; 
        } 
        /** 
         * @param date The date to set. 
         */ 
        public void setDate(Date date) { 
                this.date = date; 
        } 
        public String getError() { 
                return error; 
        } 

        public boolean getHasError() { 
                if ((error == null) || (error.length() == 0)) { 
                        return (false); 
                } 
                return (true); 
        } 

        public void formSubmit(IRequestCycle cycle) { 
                if (misses == 0) { 
                        error = "Please select a game difficulty."; 
                        return; 
                } 
                
                if (getDate()==null) { 
                        errorMsg = "Please Enter a Valid Date"; 
                        return; 
                        } 
                

//Here I want to query the database and see if there are records for processing only then I would like to proceed to next page. 
                try 
                { 
                        DatabaseManager dm = new DatabaseManager(); 
                        Connection con = dm.getConnection(); 
                        
                        String myquery  = "select count(*) as current_count from  pslwrkdb..fin46_instmt"; 
                        PreparedStatement pstmt = null; 
                        ResultSet rs = null; 
                        try { 
                                pstmt = con.prepareStatement(myquery); 
                                rs = pstmt.executeQuery(); 
                                while(rs.next()) 
                                { 
                                        _count =        rs.getInt("current_count"); 
                                        
                                } 
                        } 
                        catch (SQLException se) { 
                                //System.out.println(se.getMessage()); 
                        } 
                        finally { 
                                if (pstmt != null) { 
                                        try { 
                                                pstmt.close(); 
                                        } 
                                        catch (SQLException ex1) { 
                                                ex1.printStackTrace(); 
                                        } 
                                } 
                                if (rs != null) { 
                                        try { 
                                                rs.close(); 
                                        } 
                                        catch (SQLException ex1) { 
                                                ex1.printStackTrace(); 
                                        } 
                                } 
                                if (con != null) { 
                                        try { 
                                                //oppslogger.info("Releasing Connection "); 
                                                con.close(); 
                                        } 
                                        catch (SQLException ex2) { 
                                                ex2.printStackTrace(); 
                                        } 
                                } 
                        } 
                } 
                catch (Exception e) 
                { 
                        errorMsg = "Error occured while reading the database"; 
                } 
                if ( _count == 0) { 
                        errorMsg = "Data is not yet ready Please try later"; 
                        return; 
                        } 
                
                
                if (misses == 1){ 
                cycle.activate("Mortgage"); 
                } 
                if (misses == 2){ 
                        cycle.activate("LC"); 
                        } 
                if (misses == 3){ 
                        cycle.activate("BOTH"); 
                        } 
                
        } 
        /** 
         * @return Returns the errorMsg. 
         */ 
        
        public String getErrorMsg() { 
                return errorMsg; 
        } 
        
} 


		
---------------------------------
Celebrate Yahoo!'s 10th Birthday! 
 Yahoo! Netrospective: 100 Moments of the Web