Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using session.setAttribute() I want to save the data in the database

 
deepak narahare
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing a timesheet app where we must enter data and time and it must go and save in the database.
timesheet.jsp

EmployeeBean.java:

TimesheetDao.java

TimeSheetServlet.java

error:
all the values in the database are not null!
please help me... thanks!!
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have used setLoginTime() instead of setEmpId() by mistake.
 
deepak narahare
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed it... thanks
my mistake... let me check and let you know.
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepak narahare wrote:
As far as I understand, in your database, LOGINTIME is of type 'DATETIME' and you are attempting to insert a String.
You need to convert String into DATETIME in the insert query.
Go through the MySql reference to check how to convert String into DATETIME.
 
deepak narahare
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it thanks... can you please me if I want to insert data of 5 rows at same time what I must do? and how?
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through this
It explains how to execute queries in batch.
But for 5 rows I think a loop is a better option than batch.
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Few more things to remember.

  • Why are you using session? Where are you using the values set in session?
  • It is a good practice to close PreparedStatement before Connection.
  • Java convention says package name should be in lowecase. So replace package com.eis.Dao; with package com.eis.dao;
  •  
    deepak narahare
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks...
    can you please tell me how to do it in a loop? or an example ? or tips to do it?
    If I used addbatch then if one query is failed to insert can we rollback the data ?

    There is a login page before this. And after logging in, only that particular user must insert,save and retrieve the data. So there are many user who will be logging at the same time. So I thought it would be better to use session.
     
    Tapas Chand
    Ranch Hand
    Posts: 583
    8
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    deepak narahare wrote:can you please tell me how to do it in a loop? or an example ? or tips to do it?
    Try to do with a for loop. Post your logic if you face some problem. We will try to help

    deepak narahare wrote:If I used addbatch then if one query is failed to insert can we rollback the data ?
    If insert fails, you can always call Connection.rollback() in catch block. Most Important:You need to set auto commit to false before executing any DML.

    deepak narahare wrote:There is a login page before this. And after logging in, only that particular user must insert,save and retrieve the data. So there are many user who will be logging at the same time. So I thought it would be better to use session.
    If you want to insert, save and retrieve data, you can just remove the session.setAttribute() part from the code. Session enables user to hold data for multiple requests (or as long as that particular user is active) which is clearly not your requirement.
     
    deepak narahare
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Tapas Chand
    I found the following example. but i dont know how to implement it in the above code.
    Please help...
    Thanks!
     
    Tapas Chand
    Ranch Hand
    Posts: 583
    8
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your Servlet you have called a method like this
    You need not pass 'sql'.
    Just pass all the 5 records you want to insert as a List<POJO> or whatever suits your requirement.
    Then in DAO follow the logic of the code snippet you just posted.

    Note that you want to insert 5 rows, but looking at your original code, it seems you have only 1 record.
     
    deepak narahare
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @TapasChand
    I might sound stupid but where I must add List<POJO> in dao or in servlet or I must create a new class.
    I know the example above is for only one row, but I have to do it for five rows.
     
    Tapas Chand
    Ranch Hand
    Posts: 583
    8
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your Servlet, before calling the insert() method, prepare the data which you need to insert.
    let us assume you have created 5 EmplyeeBean objects.
    Add them to a List<EmployeeBean>.
    Then pass this as an argument to the DAO.
     
    deepak narahare
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Tapas Chand
    Thanks for helping me... if i got any doubt i'll post as my next question...
     
    deepak narahare
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Tapas Chand
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic