mvc4自动生成增删改查之后怎么办,我用控制器直接生成增删改查之后呢?添加,删除,修改都不好使是为啥
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mvc4自动生成增删改查之后怎么办,我用控制器直接生成增删改查之后呢?添加,删除,修改都不好使是为啥相关的知识,希望对你有一定的参考价值。
参考技术A 当然不好使了,内容要自己填的java MVC 实现增删改查
public class AddServlet extends HttpServlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String description = req.getParameter("description");
String baseprice = req.getParameter("baseprice");
String writer = req.getParameter("writer");
String publish = req.getParameter("publish");
String pages = req.getParameter("pages");
String images = req.getParameter("images");
String stock = req.getParameter("stock");
Connection conn=null;
PreparedStatement pstm=null;
try
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ecport";
conn = DriverManager.getConnection(url, "root", "");
String sql = "insert into product values(0,?,?,?,?,?,?,?,?)";
pstm = conn.prepareStatement(sql);
pstm.setObject(1, name);
pstm.setObject(2, description);
pstm.setObject(3, baseprice);
pstm.setObject(4, writer);
pstm.setObject(5, publish);
pstm.setObject(6, pages);
pstm.setObject(7, images);
pstm.setObject(8, stock);
//执行sql
int result = pstm.executeUpdate();
//3.把处理的结果响应给用户(响应对象)
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
if(result == 1)
out.println("<html><body><h3 style='color:red'>恭喜你,新增图书成功!!!</h3></body></html>");
else
out.println("<html>新增失败</html>");
out.close();
catch(Exception e)
e.printStackTrace();
finally
try
pstm.close();
conn.close();
catch (SQLException e)
e.printStackTrace();
求大神帮忙把 上面的增加功能改成MVC框架实现
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddServlet extends HttpServlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String description = req.getParameter("description");
String baseprice = req.getParameter("baseprice");
String writer = req.getParameter("writer");
String publish = req.getParameter("publish");
String pages = req.getParameter("pages");
String images = req.getParameter("images");
String stock = req.getParameter("stock");
Product p=new Product();
ProductDao Dao = new ProductDao();
p.setName(name);
p.setDescription(description);
p.setBaseprice(baseprice);
p.setWriter(writer);
p.setPublish(publish);
p.setPages(pages);
p.setImages(images);
p.setStock(stock);
int result = 0;
try
result = Dao.save(p);
catch (Exception e)
e.printStackTrace();
resp.setContentType("text/html");
if(result == 1)
resp.sendRedirect("ok.jsp");
else
resp.sendRedirect("error.jsp");
ProductDao类实现了使用对象想数据库中添加记录的功能;
实现了MVC中的M模型
package servlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class ProductDao
public int save(Product p) throws Exception
Connection conn = null;
PreparedStatement prep = null;
try
conn = DBUtil.getConnection();
String sql = "insert into product values(0,?,?,?,?,?,?,?,?)";
prep = conn.prepareStatement(sql);
prep.setObject(1, p.getName());
prep.setObject(2, p.getDescription());
prep.setObject(3, p.getBaseprice());
prep.setObject(4, p.getWriter());
prep.setObject(5, p.getPublish());
prep.setObject(6, p.getPages());
prep.setObject(7, p.getImages());
prep.setObject(8, p.getStock());
int num=prep.executeUpdate();
return num;
catch (Exception e1)
e1.printStackTrace();
throw e1;
finally
DBUtil.close(conn);
Product类根据数据库的字段声明类的属性根据表名命名类名;
package servlet;
public class Product
private Object name;
private Object description;
private Object baseprice;
private Object writer;
private Object publish;
private Object pages;
private Object images;
private Object stock;
public Object getName()
return name;
public void setName(Object name)
this.name = name;
public Object getDescription()
return description;
public void setDescription(Object description)
this.description = description;
public Object getBaseprice()
return baseprice;
public void setBaseprice(Object baseprice)
this.baseprice = baseprice;
public Object getWriter()
return writer;
public void setWriter(Object writer)
this.writer = writer;
public Object getPublish()
return publish;
public void setPublish(Object publish)
this.publish = publish;
public Object getPages()
return pages;
public void setPages(Object pages)
this.pages = pages;
public Object getImages()
return images;
public void setImages(Object images)
this.images = images;
public Object getStock()
return stock;
public void setStock(Object stock)
this.stock = stock;
DBUtil类封装的是对数据库的连接:
package servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* JDBC工具类:
* 提供了获得连接,关闭连接的相关的方法。
*
*/
public class DBUtil
private static String url="jdbc:mysql://localhost:3306/ecport";
public static Connection getConnection() throws Exception
Connection conn = null;
try
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.
getConnection(url,"root", "");
catch (Exception e)
e.printStackTrace();
throw e;
return conn;
public static void close(Connection conn)
if(conn != null)
try
conn.close();
catch (SQLException e)
e.printStackTrace();
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
Connection conn =
getConnection();
System.out.println(conn);
最后是MVC中的视图,实现的shiMVC中的V视图,在AddServlet中根据不同的结果受用重定向跳转到指定的页面中,表明是否添加成功。
成功页面:
<%@page pageEncoding="utf-8"
contentType="text/html;charset=utf-8" %>
<html>
<body>
<h3 style='color:red'>恭喜你,新增图书成功!!!</h3>
</body>
</html>
失败页面:
<%@page pageEncoding="utf-8"
contentType="text/html;charset=utf-8" %>
<html>新增失败</html>
以上是关于mvc4自动生成增删改查之后怎么办,我用控制器直接生成增删改查之后呢?添加,删除,修改都不好使是为啥的主要内容,如果未能解决你的问题,请参考以下文章