• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating/inserting data through servlet using JDBC

 
induz sat
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a Social network site where a user can log or register. Once logged in with correct credentials, the user could search in a friends list and invite him/her to be friend.

So far I have made 2 jsp files-
[1] index.jsp [2] UserRegistration.jsp

and HttpServlets-UserLogIn and RegistrationServlet


The Dynamic project gets connected through Tomcat using JDBC 5.1.23 to mySQL database.

schema is SocialNetwork with 2 tables:


My index.jsp in webContent folder is:



my UserRegistration JSP is



userLogIn servlet is:



RegistrationServlet is :-



I am not clear about the inserting data in to the DataBase and connection.

How could I connect the UserLogin servlet to the UserInfo table so that if the user’s name and password are found and matched in the DB , he could see his/her friends’ list.


If that username and password is not found in the UserInfo, a message should display that the user has to Register first.

Once the user is logged-in, he should see a list of all the Usernames and Age.
Later, the user could select a user from that list and invite that user to be his/her friend.

Right now I am getting attributes added to userInfo table in username and userPW columns:[ serial# is th PK] when the user Register on RegistrationServlet:


 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For connecting to other servlet from first, you can make use of RequestDispatcher here.

Also, for inserting or updating the records in database, make use of PreparedStatement class of JDBC.

Thanks,
Ashwini Kashyap | www.infocepts.com
 
induz sat
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for suggestions.

if I use PreparedStatement I dont know why I have to add cast to PrepareStatement.


Also my first column is serial# and its PK with AIncrement. I get "No value specified for parameter 1" on tomcat console. Line 13.

How to go about it?


 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to type cast it. Its returning PreparedStatement object only.

Also, there is no need to set primary key as its auto increment, JDBC internally will provide it for you.

Thanks,
Ashwini
 
induz sat
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to type cast it. Its returning PreparedStatement object only.


I get red underline in Eclipse with


For Auto increment I corrected it:

now my data is being saved in the userInfo Db and serial# is being auto incremented.

Now Using my first index jsp, how can I check userName and password against the UserInfo data for authenticated credentials?


Thanks


 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that you may make use of select query using Statement class and check entry against UserInfo.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic