File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

couldn't access data using Dao

 
sudha javvadi
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • 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
}
 
Sudarshan Chakrabarty
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

problem solved. I got it.

Sudha.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic