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.
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?
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.
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!)
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?
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.
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.
Post by:autobot
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