MVC (jsp + servlet + javabean) 以及jdbc

前端: 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
int size;
ArrayList temp = new ArrayList();
%>
<%
ArrayList result = (ArrayList)request.getAttribute("result");
size = result.size();
%>
<table>
<tr>
<th>ID</th>
<th>品牌</th>
<th>价格</th>
<th>保质期</th>
</tr>
<%
for(int j = 0; j <size; j++){
temp.clear();
temp = (ArrayList)result.get(j); %>
<tr>
<th><a href = "change?id=<%=temp.get(0)%>"><%=temp.get(0)%></a></th>
<th><%= temp.get(1)%></th>
<th><%= temp.get(2)%></th>
<th><%= temp.get(3)%></th>
</tr>
<%}
%>
</table>
<a href = "add.jsp"> 增加 </a>

</body>
</html>

 

后端: 

@WebServlet("/show")
public class show extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service service = new service();
ArrayList result = service.queryAll();
//将传递数据放入到request里面
request.setAttribute("result", result);
request.getRequestDispatcher( "show.jsp").forward(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

实体类:

package dao;

//专门用于数据的传递
public class entity {
private int id;
private String name;
private int price;
private int date;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
}

 数据库jdbc:JDBC的工作量大:需要先注册驱动和数据库信息、操作Connection、通过statement对象执行SQL,将结果返回给resultSet,然后从resultSet中读取数据并转换为pojo对象,最后需要关闭数据库相关资源。

jdbc实例:

  public class add {
    public static boolean add(int Id, String Name, int Price, int Data) {
      String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
      String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName = test";
      String userName = "sa";
      String userPwd = "123456";
      try {
        Class.forName(dbDriver);
        Connection connection = DriverManager.getConnection(dbURL, userName, userPwd);
        Statement state = connection.createStatement();
        String sql = "use test";
        state.execute(sql);
        sql = "insert into dbo.car(id,name,price,data)values(" + Id +"," + "'Name'" +"," + Price +"," + Data +")";
        System.out.println("检查传入的字段");
        System.out.println(sql);
        boolean result = state.execute(sql);
        System.out.println(result);
        return result;
      }
    catch(Exception e) {
        e.printStackTrace();
      }
    return false;
    }
  }

posted on 2019-04-18 16:17  黄山一叶  阅读(198)  评论(0编辑  收藏  举报