Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

select statements in the methods

Posted on 2004-09-21
10
Medium Priority
?
217 Views
Last Modified: 2010-04-01
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
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12113568
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
 
LVL 35

Accepted Solution

by:
TimYates earned 480 total points
ID: 12113584
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12113587
That 2nd one closes the result set in the finally block too....just to be sure ;-)

What errors do you get?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 32

Assisted Solution

by:ldbkutty
ldbkutty earned 240 total points
ID: 12113621
Do you get any errors/exceptions ?

>> sm.setStairManagementId("stair_management_id");
should be:
sm.setStairManagementId(rs.getString("stair_management_id"));
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12113641
Well spotted! :-D
0
 

Author Comment

by:dkim18
ID: 12114350
Still nothing working...
no errors or exceptions...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12114395
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
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 80 total points
ID: 12118643
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
 

Author Comment

by:dkim18
ID: 12122552
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
 
LVL 32

Assisted Solution

by:ldbkutty
ldbkutty earned 240 total points
ID: 12125160
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, WatchGuard's Director of Security Strategy and Research Teri Radichel, takes a look at insider threats, the risk they can pose to your organization, and the best ways to defend against them.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question