This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "couldn Watch "couldn New topic
Author

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();
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
}
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.
SCJP 5
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Hi,

problem solved. I got it.

Sudha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: couldn't access data using Dao