<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="phone?m=list">列表</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function (){
$(".ck").change(function (){
if($(".ck").attr("checked")){
$(".ch").attr("checked",true);
}else{
$(".ch").attr("checked",false);
}
});
$("[value='反选']").click(function (){
$(".ch").each(function (){
if($(this).attr("checked")){
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});
});
$("[value='新增']").click(function (){
location="add.jsp";
});
});
</script>
</head>
<body>
<table border="1px">
<tr>
<th><input type="checkbox" class="ck" value=""></th>
<th>id</th>
<th>name</th>
<th>price</th>
<th>size</th>
<th>idd</th>
<th>country</th>
<th>操作<input type="button" value="新增" id="add"></th>
</tr>
<c:forEach var="li" items="${list}">
<tr>
<th><input type="checkbox" class="ch" value=""></th>
<th>${li.id}</th>
<th>${li.name}</th>
<th>${li.price}</th>
<th>${li.size}</th>
<th>${li.idd}</th>
<th>${li.country}</th>
<th><input type="button" value="修改">
<input type="button" value="删除"></th>
</tr>
</c:forEach>
<tr>
<td colspan="8" align="center">
<input type="button" value="反选">
<input type="button" value="批量删除">
</td>
</tr>
</table>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'add.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function (){
$.post(
"phone?m=country",
function (msg){
for(var i in msg){
var op = $("<option value="+msg[i].idd+">"+msg[i].country+"</option>");
$("#s1").append(op);
}
},
"json"
);
$("[value='增加']").click(function (){
$.post(
"phone?m=add",
$("#for").serialize(),
function(msg){
if(msg==1){
alert("success");
location="phone?m=list";
}else{
alert("fail");
location.reload();
}
},
"text"
);
});
});
</script>
</head>
<body>
<form id="for">
name:<input type="text" name="name"><br>
price:<input type="text" name="price"><br>
size:<input type="radio" value="大" name="size">大
<input type="radio" value="中" name="size">中
<input type="radio" value="小" name="size">小<br>
country:<select id="s1" name="idd">
</select><br>
<input type="submit" value="增加">
</form>
</body>
</html>
package com.www.entity;
public class Phone {
private int id;
private String name;
private String price;
private String size;
private int idd;
private String country;
public Phone() {
super();
// TODO Auto-generated constructor stub
}
public Phone(int id, String name, String price, String size, int idd,
String country) {
super();
this.id = id;
this.name = name;
this.price = price;
this.size = size;
this.idd = idd;
this.country = country;
}
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 String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public int getIdd() {
return idd;
}
public void setIdd(int idd) {
this.idd = idd;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Phone [id=" + id + ", name=" + name + ", price=" + price
+ ", size=" + size + ", idd=" + idd + ", country=" + country
+ "]";
}
}
package com.www.entity;
public class Country {
private int idd;
private String country;
public Country(int idd, String country) {
super();
this.idd = idd;
this.country = country;
}
public Country() {
super();
// TODO Auto-generated constructor stub
}
public int getIdd() {
return idd;
}
public void setIdd(int idd) {
this.idd = idd;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Country [idd=" + idd + ", country=" + country + "]";
}
}
package com.www.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.www.entity.Country;
import com.www.entity.Phone;
public class PhoneDao {
public List list() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///login", "root", "root");
PreparedStatement pst = con.prepareStatement("select * from phone01 a left join phone02 b on a.id = b.idd;");
ResultSet rs = pst.executeQuery();
ArrayList list = new ArrayList();
while(rs.next()){
Phone p = new Phone(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6));
list.add(p);
}
return list;
}
public List country() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///login", "root", "root");
PreparedStatement pst = con.prepareStatement("select * from phone02;");
ResultSet rs = pst.executeQuery();
ArrayList list = new ArrayList();
while(rs.next()){
Country c = new Country(rs.getInt(1),rs.getString(2));
list.add(c);
}
return list;
}
public int add(String name, String price, String size, String country) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///login", "root", "root");
PreparedStatement pst = con.prepareStatement("insert into phone02 values("+country+","+name+","+price+","+size+")");
int i = pst.executeUpdate();
return i;
}
}
package com.www.service;
import java.util.List;
import com.www.dao.PhoneDao;
public class PhoneService {
PhoneDao dao = new PhoneDao();
public List phonelist() throws Exception {
List list = dao.list();
return list;
}
public List country() throws Exception {
List list = dao.country();
return list;
}
public int add(String name, String price, String size, String country) throws Exception {
int i = dao.add(name,price,size,country);
return i;
}
}
package com.www.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONArray;
import com.www.service.PhoneService;
public class PhoneServlet extends HttpServlet {
PhoneService service = new PhoneService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String m = request.getParameter("m");
if(m.equals("list")){
list(request,response);
}else if(m.equals("country")){
country(request,response);
}else if(m.equals("add")){
add(request,response);
}
}
private void add(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String name = request.getParameter("name");
String price = request.getParameter("price");
String size = request.getParameter("size");
String country = request.getParameter("msg[i].idd");
try {
int i = service.add(name,price,size,country);
if(i>0){
response.getWriter().write("1");
}else{
response.getWriter().write("0");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void country(HttpServletRequest request,
HttpServletResponse response) {
try {
List list = service.country();
String country = JSONArray.toJSONString(list);
System.out.println(country);
response.getWriter().write(country);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void list(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
List list = service.phonelist();
request.setAttribute("list", list);
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
钟鼓馔玉不足贵,但愿长醉不复醒。
古来圣贤皆寂寞,惟有饮者留其名。