Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

select statements in the methods

Anybody see anything wrong for the following two method(select statements)? It doesn’t seem to work…
+++++++++++++++
      public void getStairManagement(String StairManagementId, StairManagement sm){
            //String sql="SELECT stair_management_id" +
            String sql="SELECT stair_management_id"  +
                    ",visit_id" +
                        ",total_steps" +
                        ",flights" +
                        ",steps_per_flight" +
                        ",assistance" +
                        ",gait_pattern" +
                        ",handrail" +      
                        ",comments" +
                        " FROM stair_management" +
                        " WHERE stair_management_id =" + StairManagementId;
            ResultSet rs = null;
            Statement stmt = null;
            try {
                  stmt=conn.createStatement();
                  rs=stmt.executeQuery(sql);
                  if(rs.next()){
                        sm.setStairManagementId("stair_management_id");
                        sm.setVisitId(rs.getString("visit_id"));
                        sm.setStepsTotal(rs.getString("total_steps"));
                        sm.setFlights(rs.getString("flights"));
                        sm.setStepsPerFlight(rs.getString("steps_per_flight"));
                        sm.setAssistance(rs.getString("assistance"));
                        sm.setGaitPattern(rs.getString("gait_pattern"));
                        sm.setHandrail(rs.getString("handrail"));
                        sm.setComments(rs.getString("comments"));
                  }
                  rs.close();
            } catch (SQLException e) {
                  log.error(sql + "\n"+e);
            }finally{
                  if(stmt!=null){
                        try {
                              stmt.close();
                        } catch (SQLException e1) {
                              log.error(e1);
                        }
                  }
            }//end try/catch
            System.out.println("rs : "+ rs);
      }//end get
      
      public StairManagement[] getStairManagements(String visitId){
            String sql="SELECT stair_management_id" +
                        ",total_steps" +
                        ",flights" +
                        ",steps_per_flight" +
                        ",assistance" +
                        ",gait_pattern" +
                        ",handrail" +
                        ",comments" +
                        " FROM stair_management" +
                        " WHERE visit_id =" + visitId;
            ResultSet rs = null;
            Statement stmt = null;
            StairManagement[] sms=null;
            try {
                  stmt=conn.createStatement();
                  rs=stmt.executeQuery(sql);
                  if(!rs.isBeforeFirst()){
                        stmt.close();
                        return null;
                  }
                  ArrayList al =new ArrayList();
                  while(rs.next()){
                        StairManagement sm = new StairManagement();
                        sm.setStairManagementId(rs.getString("stair_management_id"));
                        sm.setStepsTotal(rs.getString("total_steps"));
                        sm.setFlights(rs.getString("flights"));
                        sm.setStepsPerFlight(rs.getString("steps_per_flight"));
                        sm.setAssistance(rs.getString("assistance"));                  
                        sm.setGaitPattern(rs.getString("gait_pattern"));
                        sm.setHandrail(rs.getString("handrail"));
                        sm.setComments(rs.getString("comments"));
                        al.add(sm);
                  }//end loop
                  rs.close();
                  sms=(StairManagement[])al.toArray(new StairManagement[al.size()]);
            } catch (SQLException e) {
                  log.error(sql + "\n"+e);
            }finally{
                  if(stmt!=null){
                        try {
                              stmt.close();
                        } catch (SQLException e1) {
                              log.error(e1);
                        }
                  }
            }//end try/catch
            return sms;
      }//end get      
0
dkim18
Asked:
dkim18
  • 5
  • 2
  • 2
  • +1
4 Solutions
 
TimYatesCommented:
I would try:

---------------------------

     public void getStairManagement(String StairManagementId, StairManagement sm){
          //String sql="SELECT stair_management_id" +
          String sql="SELECT stair_management_id"  +
                    ",visit_id" +
                    ",total_steps" +
                    ",flights" +
                    ",steps_per_flight" +
                    ",assistance" +
                    ",gait_pattern" +
                    ",handrail" +    
                    ",comments" +
                    " FROM stair_management" +
                    " WHERE stair_management_id = ?";
          ResultSet rs = null;
          PreparedStatement stmt = null;
          try {
               stmt=conn.prepareStatement( sql );
               stmt.setString( 1, StairManagementId ) ;
               rs=stmt.executeQuery();
               if(rs.next()){
                    sm.setStairManagementId("stair_management_id");
                    sm.setVisitId(rs.getString("visit_id"));
                    sm.setStepsTotal(rs.getString("total_steps"));
                    sm.setFlights(rs.getString("flights"));
                    sm.setStepsPerFlight(rs.getString("steps_per_flight"));
                    sm.setAssistance(rs.getString("assistance"));
                    sm.setGaitPattern(rs.getString("gait_pattern"));
                    sm.setHandrail(rs.getString("handrail"));
                    sm.setComments(rs.getString("comments"));
               }
               rs.close();
          } catch (SQLException e) {
               log.error(sql + "\n"+e);
          }finally{
               if(stmt!=null){
                    try {
                         stmt.close();
                    } catch (SQLException e1) {
                         log.error(e1);
                    }
               }
          }//end try/catch
          System.out.println("rs : "+ rs);
     }//end get
     
     public StairManagement[] getStairManagements(String visitId){
          String sql="SELECT stair_management_id" +
                    ",total_steps" +
                    ",flights" +
                    ",steps_per_flight" +
                    ",assistance" +
                    ",gait_pattern" +
                    ",handrail" +
                    ",comments" +
                    " FROM stair_management" +
                    " WHERE visit_id = ?" ;
          ResultSet rs = null;
          PreparedStatement stmt = null;
          StairManagement[] sms=null;
          try {
               stmt=conn.prepareStatement( sql );
               stmt.setString( 1, visitId ) ;
               rs=stmt.executeQuery();
               ArrayList al =new ArrayList();
               while(rs.next()){
                    StairManagement sm = new StairManagement();
                    sm.setStairManagementId(rs.getString("stair_management_id"));
                    sm.setStepsTotal(rs.getString("total_steps"));
                    sm.setFlights(rs.getString("flights"));
                    sm.setStepsPerFlight(rs.getString("steps_per_flight"));
                    sm.setAssistance(rs.getString("assistance"));              
                    sm.setGaitPattern(rs.getString("gait_pattern"));
                    sm.setHandrail(rs.getString("handrail"));
                    sm.setComments(rs.getString("comments"));
                    al.add(sm);
               }//end loop
               rs.close();
               sms=(StairManagement[])al.toArray(new StairManagement[al.size()]);
          } catch (SQLException e) {
               log.error(sql + "\n"+e);
          }finally{
               if(stmt!=null){
                    try {
                         stmt.close();
                    } catch (SQLException e1) {
                         log.error(e1);
                    }
               }
          }//end try/catch
          return sms;
     }//end get    
0
 
TimYatesCommented:
ACTUALLY:

     public void getStairManagement(String StairManagementId, StairManagement sm){
          //String sql="SELECT stair_management_id" +
          String sql="SELECT stair_management_id"  +
                    ",visit_id" +
                    ",total_steps" +
                    ",flights" +
                    ",steps_per_flight" +
                    ",assistance" +
                    ",gait_pattern" +
                    ",handrail" +    
                    ",comments" +
                    " FROM stair_management" +
                    " WHERE stair_management_id = ?";
          ResultSet rs = null;
          PreparedStatement stmt = null;
          try {
               stmt=conn.prepareStatement( sql );
               stmt.setString( 1, StairManagementId ) ;
               rs=stmt.executeQuery();
               if(rs.next()){
                    sm.setStairManagementId("stair_management_id");
                    sm.setVisitId(rs.getString("visit_id"));
                    sm.setStepsTotal(rs.getString("total_steps"));
                    sm.setFlights(rs.getString("flights"));
                    sm.setStepsPerFlight(rs.getString("steps_per_flight"));
                    sm.setAssistance(rs.getString("assistance"));
                    sm.setGaitPattern(rs.getString("gait_pattern"));
                    sm.setHandrail(rs.getString("handrail"));
                    sm.setComments(rs.getString("comments"));
               }
          } catch (SQLException e) {
               log.error(sql + "\n"+e);
          }finally{
               if(rs!=null){
                    try {
                         rs.close();
                    } catch (SQLException e1) {
                         log.error(e1);
                    }
               }
               if(stmt!=null){
                    try {
                         stmt.close();
                    } catch (SQLException e1) {
                         log.error(e1);
                    }
               }
          }//end try/catch
          System.out.println("rs : "+ rs);
     }//end get
     
     public StairManagement[] getStairManagements(String visitId){
          String sql="SELECT stair_management_id" +
                    ",total_steps" +
                    ",flights" +
                    ",steps_per_flight" +
                    ",assistance" +
                    ",gait_pattern" +
                    ",handrail" +
                    ",comments" +
                    " FROM stair_management" +
                    " WHERE visit_id = ?" ;
          ResultSet rs = null;
          PreparedStatement stmt = null;
          StairManagement[] sms=null;
          try {
               stmt=conn.prepareStatement( sql );
               stmt.setString( 1, visitId ) ;
               rs=stmt.executeQuery();
               ArrayList al =new ArrayList();
               while(rs.next()){
                    StairManagement sm = new StairManagement();
                    sm.setStairManagementId(rs.getString("stair_management_id"));
                    sm.setStepsTotal(rs.getString("total_steps"));
                    sm.setFlights(rs.getString("flights"));
                    sm.setStepsPerFlight(rs.getString("steps_per_flight"));
                    sm.setAssistance(rs.getString("assistance"));              
                    sm.setGaitPattern(rs.getString("gait_pattern"));
                    sm.setHandrail(rs.getString("handrail"));
                    sm.setComments(rs.getString("comments"));
                    al.add(sm);
               }//end loop
               sms=(StairManagement[])al.toArray(new StairManagement[al.size()]);
          } catch (SQLException e) {
               log.error(sql + "\n"+e);
          }finally{
               if(rs!=null){
                    try {
                         rs.close();
                    } catch (SQLException e1) {
                         log.error(e1);
                    }
               }
               if(stmt!=null){
                    try {
                         stmt.close();
                    } catch (SQLException e1) {
                         log.error(e1);
                    }
               }
          }//end try/catch
          return sms;
     }//end get    
0
 
TimYatesCommented:
That 2nd one closes the result set in the finally block too....just to be sure ;-)

What errors do you get?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ldbkuttyCommented:
Do you get any errors/exceptions ?

>> sm.setStairManagementId("stair_management_id");
should be:
sm.setStairManagementId(rs.getString("stair_management_id"));
0
 
TimYatesCommented:
Well spotted! :-D
0
 
dkim18Author Commented:
Still nothing working...
no errors or exceptions...
0
 
TimYatesCommented:
Is the code running?

Try putting System.out.println( "XX" ) ; messages at relevant points

Does the SQL give you results if you type it straight into the db tool for your database?
0
 
MogalManicCommented:
Try the SQL statements interactivly using SQL tool such as Squirrel(http://www.squirrelsql.org) or DBVisualizer(http://www.minq.se/products/dbvis/).

Make sure the keys(StairManagementId) you are passing in results in 1 or more rows.
0
 
dkim18Author Commented:
TimYates,

I fixed the bug. It was from other files...
Since you all put some effort, I will give 80% of point for  you if you can explain the change you made in those methods and what are the differences.

I will give give rest of points to others.

thanks,
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now