• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

couldn't access data using Dao

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

This is my first data access program. I am able to connect to database using getConnection() method,but I am not able to insert data into the table "USERS". I couldn't understand what is going wrong. Could you please guide me. Following is the part of Action and dao programs.

public class UserRegistrationAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
UserRegistrationDao urd=new UserRegistrationDao();
urd.getConnection();
urd.insert();
---------------------------------------------------------------------------------------------------------------------
public void insert()
{
UserRegistrationForm userForm= new UserRegistrationForm();
try
{

PreparedStatement ps= con.prepareStatement("insert into USERS" + "(EMAIL,FIRST_NAME,LAST_NAME,PASSWORD,PHONE,FAX)" + "values(?,?,?,?,?,?)");
ps.setString(1,userForm.getEmail());
ps.setString(2,userForm.getFirstName());
ps.setString(3,userForm.getLastName());
ps.setString(4,userForm.getPassword());
ps.setString(5,userForm.getPhone());
ps.setString(6,userForm.getFax());
ps.executeUpdate();
}
catch(Exception ex)
{
System.out.println("exception occured in dao layer"); //directly comming to this point
}
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha,

In your insert method, you created a new object of class UserRegistrationForm method using

Then you are calling a method on the userForm object


But, you haven't populated the userForm object yet, so your userForm.getEmail() will throw a null pointer exception. Hence, you are getting the Exception.
So, you need to populate the userForm object before calling any method on it.
Hope this helps .
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sudarshan,

Thank you for your reply. I dont want to put all the insert code in ActionForm execute method. I need the form object in dao class. How can I get that form object in dao class? I tried different ways but no luck. please guide me.

Thank you,
Sudha.
SCJP 5
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

problem solved. I got it.

Sudha.
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic