Forums Register Login

What is the "standard way" when getting data from a DB?

+Pie Number of slices to send: Send
Hello,

What is the standard way to get data from a DB in a scalable way?

Currently I am using a script to pull out all details from a DB "where id=1", then looping through the "results.getString(i)" and putting them into a fixed length array before returning them to the servlet that requested the information. For example with the following code;



Then in the file that called the above code;


This is not a very robust way to code since if I add a new column to the DB then I need to update all of my scripts to deal with this accordingly, but I am unsure how I would do this differently?

Ideally I would like to do something like
- Select all data from DB "where id=1" (which I already know how to do)
- While results.next() ----- which will only be the 1 row
- loop through each column in the results set and put them into a variable length name/value paired array (does something like this even exist?) where the "name" is the column heading and the "value" is the data from the results set.
- Then I can return the name/value paired array which I can easily pull the data out of, instead of via the index number of the array like I am doing at the minute, but by the "name" of the array value.


Any advice on how I can code this up in a more robust way would be greatly appreciated.

Thanks
Michael
+Pie Number of slices to send: Send
Hint: getResultsetMetaData()
+Pie Number of slices to send: Send
Ta very much, ill go and read up on that :-)

I'll let you know if I have any issues.

Thanks
+Pie Number of slices to send: Send
I seem to be having an issue with a java file I am trying to adapt using HashMaps.

I cannot seem to find any useful documentation anywhere about how to return a HashMap correctly, or set up the separate java file which does this correctly.

Currently I have got similar to the following....



Calling this method within a separate class.....



The issue I keep getting when I try to compile is with the line "return map;".

Has anyone got any ideas where I am going wrong here?

Thanks
Michael
+Pie Number of slices to send: Send
 

Michael Cropper wrote:The issue I keep getting when I try to compile is with the line "return map;".


Keeping the issue secret won't help us help you.
+Pie Number of slices to send: Send
Issue is when I try to compile using ..... "javac ./mypackage/*.java"




Then when I recompile as suggested I get the following.....



The HashMap may not be the best way that I need to accomplish this?

I just need a variable array where I can have a name/value pair which can be returned from a class when I require.

Does the above printouts help with identifying what the issue is?

Thanks
Michael
+Pie Number of slices to send: Send
Seems like you just have a bunch of syntax errors in your Java. As the indentation of your code is pretty wonky, it's impossible to spot via inspection.
+Pie Number of slices to send: Send
I have re-indented for easier readability;



All of the other class files mentioned in the above error list work fine and have compiled and been run perfectly.

Here is the full class which is being called as via the following line of code, which should return a HashMap I can use.

Code calling the method;






Thanks
Michael



+Pie Number of slices to send: Send
If the issue with my code above cannot be identified, would someone be able to point me in the direction of what I need to be able to accomplish my task.

Calling a method within a separate class which returns something that is like

Name - Value
----------------
id - sometext
otherid - someothertext
etc


Then I can easily pull out the correct value data by its associated unique name from the list. This would enable me to build more robust code where I can easily add extra columns into the DB which would mean my code would not break by doing this.

All the help is much appreciated as a lot of the documentation online just doesn't cover anything like this (at least what I have been reading for that past 2hrs anyways!)

Thanks
Michael

+Pie Number of slices to send: Send
The error message says that it can't see anywhere you declare a variable named "map". I have to agree with it, I don't see any such declaration either. However if you believe there is one, could you point it out for us?
+Pie Number of slices to send: Send
Decided to go right back to basics with this one and got it fixed.

For reference here is how this issue got fixed....

Call the method from a different class using


Then here is the code for the class & method


Looking back at the original code I posted it seems the error was within the declaration of the HashMap as I was doing some crazy mixture of "Map" and "HashMap" which mustn't have been working correctly....


Well all sorted now, and hopefully anyone else looking for a solution for this issue can reference the above since all other documentation online simply doesn't cover this.

Thanks for all the help people

Michael
+Pie Number of slices to send: Send
 

Michael Cropper wrote:Looking back at the original code I posted it seems the error was within the declaration of the HashMap as I was doing some crazy mixture of "Map" and "HashMap" which mustn't have been working correctly....



Fixed now? Good! Sometimes just blowing away the whole mess and starting again is the best thing to do. But in the code you posted you had a crazy mixture of variables named "map" and "mymap". You only declared one of them.
Your mother was a hamster and your father was a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1863 times.
Similar Threads
Retreiving an image using JDBC
Prepared Statement trouble
populate drop down menu from database
Fetching Bean value from database
Missing Return Statement
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:34:43.