-3

EmpDao.java

package com.javatpoint;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.List;  
import org.springframework.jdbc.core.BeanPropertyRowMapper;  
import org.springframework.jdbc.core.JdbcTemplate;  
import org.springframework.jdbc.core.RowMapper;  
import com.javatpoint.Emp;  

public class EmpDao {  
JdbcTemplate template;  

public void setTemplate(JdbcTemplate template) {  
    this.template = template;  
}  
public int save(Emp p){  
    String sql="insert into empp(name,salary,designation) values('"+p.getName()+"',"+p.getSalary()+",'"+p.getDesignation()+"')";  
    return template.update(sql);  
}  
public int update(Emp p){  
    String sql="update empp set name='"+p.getName()+"', salary="+p.getSalary()+", designation='"+p.getDesignation()+"' where id="+p.getId()+"";  
    return template.update(sql);  
}  
public int delete(int id){  
    String sql="delete from empp where id="+id+"";  
    return template.update(sql);  
}  
public Emp getEmpById(int id){  
    String sql="select * from empp where name=?";  
    return template.queryForObject(sql, new Object[]{id},new BeanPropertyRowMapper<Emp>(Emp.class));  
}  
public List<Emp> getEmployees(){  
    return template.query("select * from empp",new RowMapper<Emp>(){  
        public Emp mapRow(ResultSet rs, int row) throws SQLException {  
            Emp e=new Emp();  

            e.setName(rs.getString(1));  
            e.setSalary(rs.getFloat(2));  
            e.setDesignation(rs.getString(3));  
            return e;  
        }  
    });  
}  

}

EmpController.java

package com.javatpoint;  
import java.util.ArrayList;  
import java.util.List;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.ModelAttribute;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.servlet.ModelAndView;  
import com.javatpoint.Emp;  
import com.javatpoint.EmpDao;  
@Controller  
public class EmpController {  
    @Autowired  
    EmpDao dao;//will inject dao from xml file  

    /*It displays a form to input data, here "command" is a reserved request attribute 
     *which is used to display object data into form 
     */  
    @RequestMapping("/empform")  
    public ModelAndView showform(){  
        return new ModelAndView("empform","command",new Emp());  
    }  
    /*It saves object into database. The @ModelAttribute puts request data 
     *  into model object. You need to mention RequestMethod.POST method  
     *  because default request is GET*/  
    @RequestMapping(value="/save",method = RequestMethod.POST)  
    public ModelAndView save(@ModelAttribute("emp") Emp emp){  
        dao.save(emp);  
        return new ModelAndView("redirect:/viewemp.jsp");//will redirect to viewemp request mapping  
    }  
    /* It provides list of employees in model object */  
    @RequestMapping("/viewemp")  
    public ModelAndView viewemp(){  
        List<Emp> list=dao.getEmployees();  
        return new ModelAndView("viewemp","list",list);  
    }  
    /* It displays object data into form for the given id.  
     * The @PathVariable puts URL data into variable.*/  
    @RequestMapping(value="/editemp/{id}")  
    public ModelAndView edit(@PathVariable int id){  
        Emp emp=dao.getEmpById(id);  
        return new ModelAndView("empeditform","command",emp);  
    }  
    /* It updates model object. */  
    @RequestMapping(value="/editsave",method = RequestMethod.POST)  
    public ModelAndView editsave(@ModelAttribute("emp") Emp emp){  
        dao.update(emp);  
        return new ModelAndView("redirect:/viewemp");  
    }  
    /* It deletes record for the given id in URL and redirects to /viewemp */  
    @RequestMapping(value="/deleteemp/{id}",method = RequestMethod.GET)  
    public ModelAndView delete(@PathVariable int id){  
        dao.delete(id);  
        return new ModelAndView("redirect:/viewemp");  
    }  

}  

Emp.java

package com.javatpoint;  

public class Emp {  
private int id;  
private String name;  
private float salary;  
private String designation;  

public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public float getSalary() {  
    return salary;  
}  
public void setSalary(float salary) {  
    this.salary = salary;  
}  
public String getDesignation() {  
    return designation;  
}  
public void setDesignation(String designation) {  
    this.designation = designation;  
}  

}

empform.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <h1>Add New Employee</h1>  
       <form method="post" action="save">    
        <table >    
         <tr>    
          <td>Name : </td>   
          <td><input path="name"  /></td>  
         </tr>    
         <tr>    
          <td>Salary :</td>    
          <td><input path="salary" /></td>  
         </tr>   
         <tr>    
          <td>Designation :</td>    
          <td><input path="designation" /></td>  
         </tr>   
         <tr>    
          <td> </td>    
          <td><input type="submit" value="Save" /></td>    
         </tr>    
        </table>    
       <form>    
</body>
</html>

Database table empp

+-------+--------+-------------+
| name  | salary | designation |
+-------+--------+-------------+
| null  |      0 | null        |
| null  |      0 | null        |
| srinu |   5000 | test        |
| null  |      0 | null        |
| null  |      0 | null        |
| null  |      0 | null        |
+-------+--------+-------------+

hiii

I am trying to insert the data into database table but it store the null values. database table names are name varchar salary int designation varchar.

please provide the solution.

thank you.

Shadow
  • 33,525
  • 10
  • 51
  • 64

1 Answers1

1

You just set your path in your form,but it's an ordinary form and you don't set the name of it,so some of the value is null.

Two ways to solve it:

a. If you want to use ordinary form to pass parameter,you need to set the name of each property:

   <form method="post" action="save">    
    <table >    
     <tr>    
      <td>Name : </td>   
      <td><input name="name" type="text" /></td>  
     </tr>    
     <tr>    
      <td>Salary :</td>    
      <td><input name="salary" type="text"/></td>  
     </tr>   
     <tr>    
      <td>Designation :</td>    
      <td><input name="designation" type="text"/></td>  
     </tr>   
     <tr>    
      <td> </td>    
      <td><input type="submit" value="Save" /></td>    
     </tr>    
    </table>    
   </form> 

b. if you want to use the path attribute, you need to use it in spring,more details can be found at enter link description here,change your code as below:

 <form:form method="post" action="save">    
    <table >    
     <tr>    
      <td>Name : </td>   
      <td><form:input path="name" type="text" /></td>  
     </tr>    
     <tr>    
      <td>Salary :</td>    
      <td><form:input path="salary" type="text" /></td>  
     </tr>   
     <tr>    
      <td>Designation :</td>    
      <td><form:input path="designation" type="text" /></td>  
     </tr>   
     <tr>    
      <td> </td>    
      <td><input type="submit" value="Save" /></td>    
     </tr>    
    </table>    
   </form:form>  
flyingfox
  • 13,414
  • 3
  • 24
  • 39