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>
数据库表empp
+-------+--------+-------------+
| name | salary | designation |
+-------+--------+-------------+
| null | 0 | null |
| null | 0 | null |
| srinu | 5000 | test |
| null | 0 | null |
| null | 0 | null |
| null | 0 | null |
+-------+--------+-------------+
HIII
我试图将数据插入数据库表,但它存储空值。数据库表名是name varchar salary int designation varchar。
请提供解决方案。
谢谢你。答案 0 :(得分:0)
您只需在表单中设置path
,但它是普通表单而您没有设置其名称,因此部分值为空。
解决问题的两种方法:
一个。如果要使用普通表单传递参数,则需要设置每个属性的名称:
<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>
湾如果您想使用path
属性,则需要在春季使用它,可以在 enter link description here 找到更多详细信息,更改您的代码如下:
<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>