dpk12
dpk12

Reputation: 85

Saving form data in the database using Struts 2

When we click the save button it must save the form data in the database but its doing nothing.

Below is the code:

BodyDaywise.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Day Wise</title>
</head>
<body align="center">
    <h1 align="center">Day Wise Form</h1>
        <html:form action="daywise" class="BodyDaywiseAction" method="POST" >
            
            LoginDate: <input type="date" name="LoginDate" displayformat="yyyy-mm-dd" label="Login Date(yyyy-mm-dd)"/><br><br>
            LoginTime:<input id="start" type="time"  name="LoginTime"/><br><br>
            LogoutTime:<input id="end" type="time"  name="LogoutTime"/><br><br>
            Task:<input type="textarea" name="Task" label="Task" cols="20" rows="5"/><br><br>
        
            <input type="submit" value="save" name="Save" onClick=""/>
            <button type="submit" value="Clear" name="clear">Clear</button>
            <input type="button" value="cancel" onClick="history.back();"/>
            </html:form>
</body>
</html>

Action class: BodyDaywiseAction.java:

package com.timesheet.action;
import com.opensymphony.xwork2.ActionSupport;
import com.timesheet.db.DaywiseDBO;
import java.sql.Date;
import java.sql.Time;
public class BodyDaywiseAction extends ActionSupport {
public BodyDaywiseAction()
{
}

private Date LoginDate;
private Time LoginTime;
private Time LogoutTime;
private String Task;

 
public Date getLoginDate() {
    return LoginDate;
}
    
public void setLoginDate(Date LoginDate) {
    this.LoginDate = LoginDate;
}

public Time getLoginTime() {
    return LoginTime;
}

public void setLoginTime(Time LoginTime) {
    this.LoginTime = LoginTime;
}

public Time getLogoutTime() {
    return LogoutTime;
}

public void setLogoutTime(Time LogoutTime) {
    this.LogoutTime = LogoutTime;
}

public String getTask() {
    return Task;
}

public void setTask(String Task) {
    this.Task = Task;
}

    private static final long serialVersionUID = 1L;

 
             
 @Override
public String execute() throws Exception {
    
     int i=DaywiseDBO.save(this);  
    if(i>0){  
    return "success";  
    }  
    return "error";  
}

@Override
public void validate() {
     if("".equals(getTask())){  
        addFieldError("Task", "Task must be filled !");
     }
}

}

DaywiseDBO.java:

package com.timesheet.db;
import com.timesheet.action.BodyDaywiseAction;
import com.timesheet.dbutil.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DaywiseDBO {  

public static int save(BodyDaywiseAction BDA) throws Exception{  
int status=0;  
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
DBUtil util = null;

try{  
util = new DBUtil();
conn = util.getConnection();
ps = conn.prepareStatement("insert into logintable values(?,?,?,?)");  
ps.setDate(1,BDA.getLoginDate());  
ps.setTime(2,BDA.getLoginTime());  
ps.setTime(3,BDA.getLogoutTime());  
ps.setString(4,BDA.getTask());  

status=ps.executeUpdate();  

}catch(Exception e){
e.printStackTrace();}  
return status;  
}  

} 

struts.xml:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default, tiles-default">
    <result-types>
        <result-type name="tiles"
            class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    <action name="loginaction" class="com.timesheet.action.LoginAction" method="execute">
        <result name="input" >/Login.jsp</result>
        <result name="success" type="tiles">/bodydaywise.tiles</result>
        <result name="error" type="tiles">/error.jsp</result>
    </action>
    <action name="daywise" class="com.timesheet.action.BodyDaywiseAction">
        <result name="success" type="tiles">/bodydaywisesuccess.tiles</result>
        <result name="error" type="tiles">/error.jsp</result>
    </action>
    </package>
  </struts>

Please let me know if I'm missing something.

Upvotes: -1

Views: 3335

Answers (1)

Roman C
Roman C

Reputation: 1

You are using wrong date/time type in the action class. Struts2 have not build-in converters for java.sql.* types. The date/time values should be converted to date if you use java.util.Date. objects of this type can contain both date and time values.

import java.util.Date;
import java.sql.Time;

Change the getters and setters accordingly to return the required. You can also set the date object to Calendar and do some calculations, after that you can create a Timestamp object from the calendar. For example

user.setCreateDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));

sets the current date & time to the user object before the user is added to the database.

The example of using PreparedStatement: JDBC PreparedStatement example – Insert a record.

There's also an example to save only date part of the Date: Insert date value in PreparedStatement. (Don't use it, because it doesn't save a time portion of the Date).

Upvotes: 1

Related Questions