1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > MVC (jsp + servlet + javabean) 以及jdbc

MVC (jsp + servlet + javabean) 以及jdbc

时间:2019-02-18 19:57:02

相关推荐

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;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。