wood burning stoves*
The moose likes JDBC and the fly likes What is the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What is the "standard way" when getting data from a DB?" Watch "What is the "standard way" when getting data from a DB?" New topic
Author

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

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Hint: getResultsetMetaData()


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Ta very much, ill go and read up on that :-)

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

Thanks
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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



Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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?
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the "standard way" when getting data from a DB?
 
Similar Threads
Missing Return Statement
populate drop down menu from database
Prepared Statement trouble
Retreiving an image using JDBC
Fetching Bean value from database