This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes PreparedStatement (displaying records) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatement (displaying records)" Watch "PreparedStatement (displaying records)" New topic
Author

PreparedStatement (displaying records)

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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?
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59

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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Feb 26, 2009
Posts: 59

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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Apr 14, 2004
Posts: 10336

OK, well can you post the DDL for your tables and explain what results you hope to get?
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Feb 26, 2009
Posts: 59

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

Joined: Apr 14, 2004
Posts: 10336

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

    Joined: Mar 22, 2008
    Posts: 732
    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

    Joined: Apr 14, 2004
    Posts: 10336


    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

    Joined: Mar 22, 2008
    Posts: 732
    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

    Joined: Apr 14, 2004
    Posts: 10336

    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

    Joined: Mar 22, 2008
    Posts: 732
    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

    Joined: Apr 14, 2004
    Posts: 10336


    then the columns are here there not displaying in accurate way

    Can you explain?

    Farakh khan
    Ranch Hand

    Joined: Mar 22, 2008
    Posts: 732
    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

    Joined: Feb 26, 2009
    Posts: 59

    All the best farakh khan
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: PreparedStatement (displaying records)
     
    Similar Threads
    gps week/seconds conversion
    continue statement
    flow control
    Java & MS Access
    Reference variable code