Solved

select statements in the methods

Posted on 2004-09-21
10
205 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
  • 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 120 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 32

Assisted Solution

by:ldbkutty
ldbkutty earned 60 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 20 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 60 total points
ID: 12125160
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display the newly inserted values 3 77
caughtSpeeding java challenge 16 137
Java Exception example issues 11 84
jdbc error in jsp application 20 46
An article on effective troubleshooting
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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