算是具體例子的模板吧,功能還沒全部完成,算是一個記錄
goods管理
與數據庫相連--Dbutil.java
package Good.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Dbutil {
private static String className="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/eshop";
private static String user="root";
private static String password="";
private static String className="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/eshop";
private static String user="root";
private static String password="";
static {
// (1)加載驅動:Class.forName(“”);
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// (1)加載驅動:Class.forName(“”);
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
// (2)獲得連接驅動Connection
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// (2)獲得連接驅動Connection
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 關閉資源
*
* @param connection
* @param statement
* @param resultSet
*/
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
* 關閉資源
*
* @param connection
* @param statement
* @param resultSet
*/
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 關閉資源
*
* @param connection
* @param statement
*/
public static void close(Connection connection, Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
* 關閉資源
*
* @param connection
* @param statement
*/
public static void close(Connection connection, Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
寫數據庫中的類--Good.java
package Good.bean;
public class Good {
private int id;
private String name;
private int number;
private double price;
private String depict;
public Good() {
}
public Good(int id, String name, int number, double price, String depict) {
super();
this.id = id;
this.name = name;
this.number = number;
this.price = price;
this.depict = depict;
}
private int id;
private String name;
private int number;
private double price;
private String depict;
public Good() {
}
public Good(int id, String name, int number, double price, String depict) {
super();
this.id = id;
this.name = name;
this.number = number;
this.price = price;
this.depict = depict;
}
public int getId() {
return id;
}
return id;
}
public void setId(int id) {
this.id = id;
}
this.id = id;
}
public String getName() {
return name;
}
return name;
}
public void setName(String name) {
this.name = name;
}
this.name = name;
}
public int getNumber() {
return number;
}
return number;
}
public void setNumber(int number) {
this.number = number;
}
this.number = number;
}
public double getPrice() {
return price;
}
return price;
}
public void setPrice(double price) {
this.price = price;
}
this.price = price;
}
public String getDepict() {
return depict;
}
return depict;
}
public void setDepict(String depict) {
this.depict = depict;
}
this.depict = depict;
}
@Override
public String toString() {
return "Good [id=" + id + ", name=" + name + ", number=" + number + ", price=" + price + ", depict=" + depict
+ "]";
}
public String toString() {
return "Good [id=" + id + ", name=" + name + ", number=" + number + ", price=" + price + ", depict=" + depict
+ "]";
}
}
寫Dao層--GoodDao.java
package Good.Dao;
import Good.bean.Good;
import jdk.nashorn.internal.ir.RuntimeNode.Request;
import Good.Util.Dbutil;
import Good.bean.Good;
import jdk.nashorn.internal.ir.RuntimeNode.Request;
import Good.Util.Dbutil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.sun.xml.internal.ws.api.config.management.policy.ManagementAssertion.Setting;
public class GoodDao {
public static List<Good> list() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<Good> list = new ArrayList<>();
try {
connection = Dbutil.getConnection();
//3、寫sql語句
String sql = "select * from goods";
//4、創建Statement(可以理解爲一艘船,發送sql語句到服務器,如果是
statement = connection.createStatement();
//查詢類的將結果集返回給瀏覽器,更新類返回的是影響的行數)
//5、執行sql語句
// 更新類:delete/update/insert executeUpdate() 返回的是影響的行數
// 查詢類:select executeQuery() 返回的是結果集:ResultSet
resultSet = statement.executeQuery(sql);
while(resultSet.next()) {//遍歷一行
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int number = resultSet.getInt("number");
double price = resultSet.getDouble("price");
String depict = resultSet.getString("depict");
Good goods = new Good(id, name, number, price, depict);
list.add(goods);
}
for (Good goods : list) {
System.out.println(goods);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(connection, statement, resultSet);
}
return list;
}
public static void deleteById(String id) {
Connection connection = null;
Statement statement = null;
try {
connection = Dbutil.getConnection();
String sql = "delete from goods where id=" + id;
statement = connection.createStatement();
int count = statement.executeUpdate(sql);
System.out.println("count: " + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(connection, statement);
}
}
Connection connection = null;
Statement statement = null;
try {
connection = Dbutil.getConnection();
String sql = "delete from goods where id=" + id;
statement = connection.createStatement();
int count = statement.executeUpdate(sql);
System.out.println("count: " + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(connection, statement);
}
}
public static Good find(String id) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
Good good = new Good();
try {
System.out.println(id);
connection = Dbutil.getConnection();
String sql = "select * from goods where id="+id;
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
while(resultSet.next()) {//遍歷一行
good.setId(resultSet.getInt("id"));
good.setName(resultSet.getString("name"));
good.setNumber(resultSet.getInt("number"));
good.setPrice(resultSet.getDouble("price"));
good.setDepict(resultSet.getString("depict"));
System.out.println(good.getName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(connection, statement, resultSet);
}
return good;
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
Good good = new Good();
try {
System.out.println(id);
connection = Dbutil.getConnection();
String sql = "select * from goods where id="+id;
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
while(resultSet.next()) {//遍歷一行
good.setId(resultSet.getInt("id"));
good.setName(resultSet.getString("name"));
good.setNumber(resultSet.getInt("number"));
good.setPrice(resultSet.getDouble("price"));
good.setDepict(resultSet.getString("depict"));
System.out.println(good.getName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(connection, statement, resultSet);
}
return good;
}
public static void change(String id, String name, String number, String price, String depict) {
System.out.println("111"+number);
System.out.println("111"+depict);
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
System.out.println("修改!!");
connection = Dbutil.getConnection();
String sql = "UPDATE goods set name="+name+"number="+number+"price="+price+"depict="+depict+" where id="+id;
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
System.out.println(number);
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(connection, statement, resultSet);
}
}
System.out.println("111"+number);
System.out.println("111"+depict);
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
System.out.println("修改!!");
connection = Dbutil.getConnection();
String sql = "UPDATE goods set name="+name+"number="+number+"price="+price+"depict="+depict+" where id="+id;
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
System.out.println(number);
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(connection, statement, resultSet);
}
}
}
寫與前端和Dao層相連的Servlet--Goodservlet
package Good.Servlet;
import java.io.IOException;
import java.util.List;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Good.Dao.GoodDao;
import Good.bean.Good;
import Good.bean.Good;
/**
* Servlet implementation class Goodservlet
*/
@WebServlet(urlPatterns={"/goods"})
public class Goodservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Goodservlet() {
super();
// TODO Auto-generated constructor stub
}
* Servlet implementation class Goodservlet
*/
@WebServlet(urlPatterns={"/goods"})
public class Goodservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Goodservlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
System.out.println("method: " + method);
if (method.equals("list")) {
list(req, resp);
} else if (method.equals("delete")) {
delete(req, resp);
} else if (method.equals("toEdit")) {
toEdit(req, resp);
}else if (method.equals("toFind")) {
toFind(req, resp);
}
}
private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String id=req.getParameter("id");
String name=req.getParameter("name");
String number=req.getParameter("number");
String price=req.getParameter("price");
String depict=req.getParameter("depict");
System.out.println(depict);
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
System.out.println("method: " + method);
if (method.equals("list")) {
list(req, resp);
} else if (method.equals("delete")) {
delete(req, resp);
} else if (method.equals("toEdit")) {
toEdit(req, resp);
}else if (method.equals("toFind")) {
toFind(req, resp);
}
}
private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String id=req.getParameter("id");
String name=req.getParameter("name");
String number=req.getParameter("number");
String price=req.getParameter("price");
String depict=req.getParameter("depict");
System.out.println(depict);
GoodDao.change(id,name,number,price,depict);
list(req, resp);
}
private void toFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id=req.getParameter("id");
Good good=GoodDao.find(id);
req.setAttribute("good",good);
//轉發到界面展示
req.getRequestDispatcher("Change.jsp").forward(req, resp);
}
list(req, resp);
}
private void toFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id=req.getParameter("id");
Good good=GoodDao.find(id);
req.setAttribute("good",good);
//轉發到界面展示
req.getRequestDispatcher("Change.jsp").forward(req, resp);
}
/*
* 刪除
*/
private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String id = req.getParameter("id");
GoodDao.deleteById(id);
resp.sendRedirect(req.getContextPath()+"/goods?method=list");
}
* 刪除
*/
private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String id = req.getParameter("id");
GoodDao.deleteById(id);
resp.sendRedirect(req.getContextPath()+"/goods?method=list");
}
/*
* 展示所有列表
*/
private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Good> list = GoodDao.list();
req.setAttribute("list", list);
//轉發到界面展示
req.getRequestDispatcher("goods_list.jsp").forward(req, resp);
}
}
* 展示所有列表
*/
private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Good> list = GoodDao.list();
req.setAttribute("list", list);
//轉發到界面展示
req.getRequestDispatcher("goods_list.jsp").forward(req, resp);
}
}
前端頁面1:進去的頁面--NewFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/Good/goods?method=list">
<span >商品管理</span>
</a>
</body>
</html>
顯示列表的頁面--goods_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,Good.bean.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function del(id) {
var isDel = confirm("您確認要刪除麼?");
if(isDel) {
location.href="${ctx}/goods?method=delete&id=" + id;
}
}
</script>
</head>
<body>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function del(id) {
var isDel = confirm("您確認要刪除麼?");
if(isDel) {
location.href="${ctx}/goods?method=delete&id=" + id;
}
}
</script>
</head>
<body>
<%
List<Good> list = (List<Good>)request.getAttribute("list");
%>
<table class="table table-striped table-bordered table-hover">
<tr>
<td>id</td>
<td>名字</td>
<td>庫存</td>
<td>價格</td>
<td>描述</td>
<td>修改</td>
<td>刪除</td>
</tr>
<%
if(list!=null){
for(Good goods : list) {
%>
<tr>
<td><%=goods.getId() %></td>
<td><%=goods.getName() %></td>
<td><%=goods.getNumber() %></td>
<td><%=goods.getPrice() %></td>
<td><%=goods.getDepict() %></td>
<td><a href="${ctx}/goods?method=toFind&id=<%=goods.getId()%>">修改</a></td>
<td><a href="javascript:del(<%=goods.getId() %>)">刪除</a></td>
</tr>
<%
}}
%>
</table>
List<Good> list = (List<Good>)request.getAttribute("list");
%>
<table class="table table-striped table-bordered table-hover">
<tr>
<td>id</td>
<td>名字</td>
<td>庫存</td>
<td>價格</td>
<td>描述</td>
<td>修改</td>
<td>刪除</td>
</tr>
<%
if(list!=null){
for(Good goods : list) {
%>
<tr>
<td><%=goods.getId() %></td>
<td><%=goods.getName() %></td>
<td><%=goods.getNumber() %></td>
<td><%=goods.getPrice() %></td>
<td><%=goods.getDepict() %></td>
<td><a href="${ctx}/goods?method=toFind&id=<%=goods.getId()%>">修改</a></td>
<td><a href="javascript:del(<%=goods.getId() %>)">刪除</a></td>
</tr>
<%
}}
%>
</table>
</body>
</html>
</html>
顯示更新的頁面--Change.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,Good.bean.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
pageEncoding="UTF-8" import="java.util.*,Good.bean.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
Good good=(Good)request.getAttribute("good");
int id=good.getId();
String name=good.getName();
int number=good.getNumber();
double price=good.getPrice();
String depict=good.getDepict();
%>
<table class="table table-striped table-bordered table-hover">
<tr>
<form method="post" action="goods">
<input type="hidden" value="toEdit" name="method" id="method">
<input type="hidden" value=<%=id%> name="id" id="id" >
<td>id:<%=id%></td>
<td>名稱:<input type="text" name="name" id="name" value="<%=name %>"></td>
<td>庫存:<input type="text" name="number" id="number" value="<%=number%>"></td>
<td>價格:<input type="text" name="price" id="price" value="<%=price%>"></td>
<td>評價:<input type="text" name="depict" id="depict" value="<%=depict %>"></td>
<td><input type="submit" value="qu"></td>
<%System.out.print("Change裏顯示"+depict);%>
<!-- <td><a href="${ctx}/goods?method=toEdit&id=<%=good.getId()%>">確認修改</a></td>-->
</form>
</tr>
</table>
request.setCharacterEncoding("utf-8");
Good good=(Good)request.getAttribute("good");
int id=good.getId();
String name=good.getName();
int number=good.getNumber();
double price=good.getPrice();
String depict=good.getDepict();
%>
<table class="table table-striped table-bordered table-hover">
<tr>
<form method="post" action="goods">
<input type="hidden" value="toEdit" name="method" id="method">
<input type="hidden" value=<%=id%> name="id" id="id" >
<td>id:<%=id%></td>
<td>名稱:<input type="text" name="name" id="name" value="<%=name %>"></td>
<td>庫存:<input type="text" name="number" id="number" value="<%=number%>"></td>
<td>價格:<input type="text" name="price" id="price" value="<%=price%>"></td>
<td>評價:<input type="text" name="depict" id="depict" value="<%=depict %>"></td>
<td><input type="submit" value="qu"></td>
<%System.out.print("Change裏顯示"+depict);%>
<!-- <td><a href="${ctx}/goods?method=toEdit&id=<%=good.getId()%>">確認修改</a></td>-->
</form>
</tr>
</table>
</body>
</html>
</html>