This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Struts and the fly likes couldn't access data using Dao Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "couldn Watch "couldn New topic

couldn't access data using Dao

sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
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();
public void insert()
UserRegistrationForm userForm= new UserRegistrationForm();

PreparedStatement ps= con.prepareStatement("insert into USERS" + "(EMAIL,FIRST_NAME,LAST_NAME,PASSWORD,PHONE,FAX)" + "values(?,?,?,?,?,?)");
catch(Exception ex)
System.out.println("exception occured in dao layer"); //directly comming to this point
Sudarshan Chakrabarty
Ranch Hand

Joined: Apr 10, 2008
Posts: 38
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

Joined: Mar 23, 2007
Posts: 97
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 javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97

problem solved. I got it.

It is sorta covered in the JavaRanch Style Guide.
subject: couldn't access data using Dao
It's not a secret anymore!