• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

PreparedStatement (displaying records)

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


for(ArrayList record : result){


This line says: for each object of the type ArrayList held by result do something. Your ArrayList result is not typed (e.g. its not written ArrayList<ArrayList> result) so as far as the compiler knows it contains Objects. It looks like your original ArrayList was held Strings* so you need to define your test method to take an ArrayList of Strings.



(* like I've said before, this will is only safe is your row doesn't contains other types of data).
 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Paul the status of the question is changed but I am not seeing in my thread anything from your end. Can you please repost?
 
Ranch Hand
Posts: 59
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Farakh khan wrote:Thanks Ravi for helping me again but its throwing the following errors:




In the above code i mentioned following changes.



Check the code twice before giving reply. You are not verifying the code and simply trying without concentration. That was an minor issue.

Also for the another mistake do the following modifications


 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ravi from the bottom of my heart for extending your help for the last 3 days. Now there is no error but results are wrong.


The OUTPUT:


1) its repeating two times
2) When am casting to String (e.g. String patient_id = (String) record.get(0)) then its not giving compile time error but throwing type casting exception at run time. I have to convert it to String to use some columns for further formatting


Sorry Paul as the page has changed to Next and I not noticed as well as sent you message that I cannot see. Sorry again

Thanks again
 
Ravi Majety
Ranch Hand
Posts: 59
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might have made mistake some where else. Usually you wont such issue with typecasting..

At anytime you face any issue, try to simulate that specific issue with another java file and test yourself.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smarty Ravi wrote:You might have made mistake some where else.


I posted my code and this is the only code that I am testing. I am not touching type casting right now because first I want to have accurate results and then I'll go for type casting or else

 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you are seeing duplicate results presumable this is what your query returns? What happens when you run directly against your db?



I have to convert it to String to use some columns for further formatting


Would it not be better to use a suitable java.text.Format for this?
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Paul but first I have to get accurate results of my query as still my problem not fixed then we can go a head further
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, well can you post the DDL for your tables and explain what results you hope to get?
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DAOMethods.java


OUTPUT:
786115 May 23,2011
786114 May 23,2011
786113 May 23,2011
786112 May 23,2011
786111 May 23,2011
786110 May 23,2011
786115 May 23,2011
786114 May 23,2011
786113 May 23,2011
786112 May 23,2011
786111 May 23,2011
786110 May 23,2011

This displaying records two times. why?

Best reagrds
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String query= "select patient_id,to_char(rDate,'Month dd,yyyy') from patients order by patient_id desc";


Why are you converting text data to a string in your query?

Is there a PK constraint or a unique constraint on patient_id? What do you get when you run this query directly against your database?

Where do you call viewPatients?
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why are you converting text data to a string in your query?

Is there a PK constraint or a unique constraint on patient_id? What do you get when you run this query directly against your database?

Where do you call viewPatients?



1) I not understand your first point of converting text data to a String
2) Yes patient_id is PK and just want to display its result
I have deleted all the records and now inserted one new record

The code OUTPUT is:
===================

and the db output is:


best regards
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to_char (if this is the Oracle function we are talking about) converts numeric data to text data. I can't understand why you are doing this in your query when the normal way would be to format the Date with a java.text.DateFormat.

So if patient_id is a (properly defined) primary key there cannot be duplicates which means you must be calling the code that either populates your ArrayList or writes it out more than once.
 
Ravi Majety
Ranch Hand
Posts: 59
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the coding above is very much perfect and doesnt require any more changes. May be you might have done some mistaken in your coding. Check out again ...
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, perfect is an over statement (there are certainly improvements that could be made) but you are right, the code posted so far will not produce the results Farakh is seeing. What would do this might be:
  • The code in viewPatients is being called more than once
  • There is no constraint on patient_id and it is behaving correctly
  •  
    Farakh khan
    Ranch Hand
    Posts: 851
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Smarty Ravi wrote:Actually the coding above is very much perfect and doesnt require any more changes. May be you might have done some mistaken in your coding. Check out again ...



    Dear Ravi,

    You are right I am so silly the method that was calling PatientMethods was running loop 2 times but this took 2 days to know it. Thank you very much for your patience and bearing me.

    2) When am trying with then it works fine but when am trying with then its compiling fine but at run time its throwing following exception:



    Thanks Paul and Ravi. Please help me to fix recent problem

    Best regards
     
    Paul Sturrock
    Bartender
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    String patientId = (String) record.get(0)



    So record must represent an ArrayList that holds ArrayLists. Check how you build this up and how you pass it to your viewPatiernts array. Or alternatively, stop using ArrayList and define a class to encapsulate your record.
     
    Farakh khan
    Ranch Hand
    Posts: 851
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I seen that when I am type casting into String then its working fine but when I am passing these String to another methods then it throws exception


    This is my whole code:


    I am not able to understand that if array list stored as different Strings and working fine then why its not passing to another method as String?


     
    Paul Sturrock
    Bartender
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah - so the code you posted is not the code that is throwing the exception?




    There is no cast in here, so its not this code that is causing the error.
     
    Farakh khan
    Ranch Hand
    Posts: 851
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This part was creating porblems
    [/code]
    dataSet1+=
    "<tr "+cm.rowColor()+">"+
    "<td class=normalFont>"+
    ""+
    rDate+"
    </td>"+

    "<td class=normalFont>"+
    ""+
    ""+patientId+"
    </td>";

    dataSet3+=
    "<td class=normalFont>"+
    ""+
    name+"
    </td>"+

    "<td class=normalFont>"+
    ""+
    age+"
    </td>"+

    "<td class=normalFont>"+
    ""+country+"</td>"+

    "<td class=normalFont>"+
    ""+nationality+"</td>"+

    "<td class=normalFont>"+
    ""+source+"</td>"+

    "<td >"+
    "
    "+
    "
    </td>"+
    "</tr>";
    }
    records=dataSet1+dataSet2+dataSet3;

    return records;
    }
    [/code]

    This was possible due to Ravi and Paul. Now everything is working but when printing <html> then the columns are here there not displaying in accurate way

    best regards
     
    Paul Sturrock
    Bartender
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    then the columns are here there not displaying in accurate way


    Can you explain?

     
    Farakh khan
    Ranch Hand
    Posts: 851
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The problem fixed as I was getting output in two variables dataSet1 and dataSet2 without any reason

    now I made the output in one variable and everything became fine

    I am grateful to Paul and Ravi for giving their precious time

    Thanks again & best regards
     
    Ravi Majety
    Ranch Hand
    Posts: 59
    Eclipse IDE Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All the best farakh khan
     
    Of course, I found a very beautiful couch. Definitely. And this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic