This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes reading url response and writing it to database 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 » Java » Servlets
Bookmark "reading url response and writing it to database" Watch "reading url response and writing it to database" New topic
Author

reading url response and writing it to database

Jaikiran mathukumalli
Greenhorn

Joined: Sep 05, 2013
Posts: 5
 have been trying this program fr 2 days now please help someoneU have a url whose result is somewhat likeUsername1= xxx,username2=yyy.....40 values.....We need to read the response of the url and insert it in database....where already a table is present with all keys(username1,username2,..) as header fields...we need to insert their values dynamically...the keys may not be in the same order in database as in url response..... i was able to get response and store it in a string array but am unable to get any idea on how to insert only values dynamically ...............Ill be very thankful if someone can help
Amir Keibi
Ranch Hand

Joined: Apr 23, 2012
Posts: 49

Indeed. Please format your posts to make easily readable.

As for your question, what I understand is that you have a Servlet in which you received and process 40 query string items and you'd like to store the values in a database where the same keys exist. So far you've parsed the key-value pairs into an Array. If this is correct, in the simplest form, you'll ought to create a middle tier class in which you use JDBC to connect to your database and execute an UPDATE command. I recommend using database stored procedures instead of writing the SQL command in your database.
Sourabh Bawage
Ranch Hand

Joined: Sep 03, 2013
Posts: 33

show some code,lets see what you've done.....


SCJP
Jaikiran mathukumalli
Greenhorn

Joined: Sep 05, 2013
Posts: 5
sorry....first time posting in a blog so i do not know all the rules,well here's the code i could write so far




(The url response would be like

username1=sudeep,username2=kiran,username3=jai,username4=jaikiran.......)



and the table would have username1,username2,username3,username4......as headers,i need to insert the values(i.e sudeep,kiran,jai...) according to their respective keys.

Now i have tried putting them into a map because i thought i could make them into key,values and insert the values into database with respect to their keys but i am unable to get any idea on how to do it.

Hope this information is enough,i will provide more if asked




Amir Keibi
Ranch Hand

Joined: Apr 23, 2012
Posts: 49

That's better.
First thing first, it seems that the key-value pairs aren't in the URL but in the content of the HTTP response (you're parsing the content).
Like I mentioned, in order to update the database, the bare minimum you need to do is to use JDBC and execute an "Update" SQL command for each pair which updates the values.

Are you facing any specific problem or you just don't know how to connect to DB from within your code?
Jaikiran mathukumalli
Greenhorn

Joined: Sep 05, 2013
Posts: 5
What? No i do know how to insert them into database.The key's are already present in the database tables as header fields of the table.The table was created like.

Create table value_1(username1 char(10),........)

Now what i need to do is match the values with their respective keys and insert them in a way that they are under their key , which is what i am unable to figure out how.

Can you please write me down a code or just give me a rough idea on how to do it?
Amir Keibi
Ranch Hand

Joined: Apr 23, 2012
Posts: 49

SQL problem then.

You have two options:

1- Not very good option is to write 40 different UPDATE commands:

Sudo sample:


2- Create a dynamic UPDATE command (could have performance implication):

Sudo sample:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: reading url response and writing it to database
 
Similar Threads
EJB and Database
Hibernate insert fails due to generate key failure
Toplink JPA insertable problem
JDBC and database
Problems with .sql(script files) and .properties files. [Using Netbeans]