js和jstl顯示錶格數據servlet

創建表

分析代碼,在所使用數據庫創建表

創建工程

eclipse或idea(web工程)

目錄結構

src

在這裏插入圖片描述

web

在這裏插入圖片描述

代碼

首頁dxh.do

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>web508</display-name>
  <welcome-file-list>
    <welcome-file>dxh.do</welcome-file>  
  </welcome-file-list>
</web-app>

Servlet

package com.web508.servlet;

import java.io.IOException;
import java.io.PrintWriter;
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 org.apache.jasper.JasperException;

import com.web508.dao.DxhDao;
import com.web508.model.Dxh;

@WebServlet(urlPatterns = { "/dxhAction", "/dxh.php", "/dxh.do" })
public class DxhServlet extends HttpServlet {	
	DxhDao dao=new DxhDao();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String op = req.getParameter("op");
		if (op == null)
			op = "";
		switch (op) {
		case "check":
            check(req,resp);
			break;
		case "insert":
			insert(req,resp);
			break;
		case "delete":
			delete(req,resp);
			break;
		case "select":
			select(req,resp);
			break;			
		default:
			select2(req,resp);
			break;
		}
	}
	private void select2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		String dxhb=req.getParameter("dxhb");
		if (dxhb==null) {
			dxhb="";
		}
		List<Dxh>  lst=dao.getDxh("select * from dxh where dxhb like '%"+dxhb+"%'");
		
		req.getSession().setAttribute("lstDxh", lst);
		resp.sendRedirect("showDxhByJava.jsp");
		
		
	}

	private void select(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		resp.setCharacterEncoding("UTF-8");
		String dxhb=req.getParameter("dxhb");
		if (dxhb==null) {
			dxhb="";
		}
		List<Dxh>  lst=dao.getDxh("select * from dxh where dxhb like '%"+dxhb+"%'");
		out.print(lst);
		
	}
	private void delete(HttpServletRequest req, HttpServletResponse resp) {
		//調用dao執行刪除sql語句,刪除成功向瀏覽器輸出success  失敗輸出failure
		
	}
	private void check(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		String dxha=req.getParameter("dxha");
		if (dao.isExist("select * from dxh where dxha='"+dxha+"'")) {
			out.print("exist");
		} else {
			out.print("not");
		}
	}
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException, JasperException {
		PrintWriter out = resp.getWriter();
		Dxh dxh=new Dxh();		
		org.apache.jasper.runtime.JspRuntimeLibrary.introspect(dxh, req);
		
		String  sql="insert into dxh(dxha,dxhb,dxhc,dxhd) values(?,?,?,?)";
		if (dao.exeUpdate(sql, new Object[] {dxh.getDxha(),dxh.getDxhb(),dxh.getDxhc(),dxh.getDxhd()})) {
			out.print("success");
		} else {
			out.print("failure");
		}
		
	}

}

首頁效果

執行select2

private void select2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		String dxhb=req.getParameter("dxhb");
		if (dxhb==null) {
			dxhb="";
		}
		List<Dxh>  lst=dao.getDxh("select * from dxh where dxhb like '%"+dxhb+"%'");
		
		req.getSession().setAttribute("lstDxh", lst);
		resp.sendRedirect("showDxhByJava.jsp");
		
		
	}

showDxhByJava.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
input[type=button]{
	background-color: #4CAF50;
	color:white;
}
</style>
</head>
<link rel="stylesheet" href="bootstrap.min.css">
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function allSel(allsel){
	$(".choice").prop("checked",allsel.checked);
}

function delOne(i){
	if(confirm("確定刪除?")) {
		//發異步刪除請求,若刪除成功,則執行		$("#"+i).remove();
	}
}




function delSel(){
	if(!confirm("確定刪除?")) return;
	$("input[name='chk']:checked").each(
		function(){
			var i=$(this).val();
			$("#"+i).remove();
		}
	);
}
</script>
</head>
<body>
	<div class="container">
	<div class="row clearfix">
	    <div class="col-md-12 column">
	    <nav class="navbar navbar-default" role="navigation">
				
				
				<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
					
					<form class="navbar-form navbar-left" action="dxhAction">
						<div class="form-group">
						<input type="text" class="form-control"   name="dxhb" placeholder="search by sencond field..." />
						</div> <button type="submit" class="btn btn-default">查詢</button>
					</form>
					
				</div>
				
			</nav>

	    </div>
		<div class="col-md-12 column">
		
			<table id="tbl" class="table">
				<thead><tr><th>全選<input type="checkbox" value="全選" onclick="allSel(this)"></th>
						<th>編號	</th>	<th>	產品</th>		<th>交付時間	</th><th>狀態</th><th>處理</th>
					</tr>				</thead>
					
				<tbody>
				<!--   使用java提供的標籤來顯示數據                    -->
				<%int i=1; %>				
				<c:forEach items="${sessionScope.lstDxh}"  var="dxh" varStatus="zz">				
			    <c:choose>
			    <c:when test="${zz.index mod 2  == 1}">
			    <tr id="${dxh.dxha}" class='success'> 
			    </c:when>
			    <c:otherwise>
			     <tr id="${dxh.dxha}" >
			    </c:otherwise>
			    </c:choose>
				
				   
				   <td style='padding-left:36px;'> <%=i++ %>   <input name='chk' class='choice' value="${dxh.dxha}" type='checkbox'></td>
				   <td>${dxh.dxha}</td><td>${dxh.dxhb}</td><td>${dxh.dxhc}</td>
				   <td>${dxh.dxhd}</td><td><a href='javascript:void(0)' onclick='delOne(${dxh.dxha})'>del</td>
				 </tr>
				</c:forEach>				
				</tbody>
			</table>
		</div>
		<div class="col-md-12 column">
			<input class="btn" type="button" value="刪除所選" style="margin-right:20px" onclick="delSel()">
			<input class="btn" type="button" value="&nbsp;&nbsp;&nbsp;"  style="margin-right:20px" onclick="window.location.href='add.html'">
			<input class="btn" type="button" value="js顯示數據"  style="padding:6px 20px" onclick="window.location.href='showDxhByJs.jsp'">
		</div>
		
	</div>
</div>

</body>
<script type="text/javascript">

</script>
</html>

效果

在這裏插入圖片描述

增加

代碼view

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<title>Insert title here</title>
<style type="text/css">
div{
	border-radius:5px;
	background-color:#f2f2f2;
	paddig:20px;
}

input[type=text],input[type=date]{
	width: 100%;
	padding: 12px 20px;
	margin: 8px 0;
	display: inline-block;
	border: 1px solid #ccc;
	border-radius: 4px;
	box-sizing: border-box;
}

input[type=button]{
	width: 100%;
	background-color: grey;
	color: white;
	padding: 14px 20px;
	margin: 8px 0;
	border: none;
	border-radius: 4px;
	cursor: pointer;
}
</style>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
var dxha=$("#dxha").val();
var dxhb=$("#dxhb").val();
var dxhc=$("#dxhc").val();
var dxhd=$("#dxhd").val();
var flag=[false,false,false,false];

function changeBtn(){
	for(var i=0;i<flag.length;i++){
		if(!flag[i]) {
			$("#btn").prop("disabled",true);
			$("#btn").css("background-color","grey");
			return;
		}
	}
	$("#btn").prop("disabled",false);
	$("#btn").css("background-color","#4CAF50");
}

$(function(){
	$("#dxha").keyup(function(){
		dxha=$(this).val();
		if(dxha.length==0 || isNaN(dxha) || dxha<1000 || dxha>9999){
			$("#ats").html("請輸入有效數字[1000,9999]");
			flag[0]=false;
			changeBtn();
			return;
		}
		$.ajax({
			//type:'post',
			url:"dxhAction?op=check&dxha="+dxha,
			success:function(res){
			  //  alert(res);
				if(res.indexOf("exist")!=-1){
					$("#ats").html("已被佔用");
					flag[0]=false; return;
				}
			}
		})	
		flag[0]=true;
		$("#ats").html("*");
		changeBtn();
	});
	
	$("#btn").click(function(){
		$.ajax({
			url:"dxhAction?op=insert&dxha="+dxha+"&dxhb="+dxhb+"&dxhc="+dxhc+"&dxhd="+dxhd,
			success:function(res){		     
		        if(res.indexOf("success")!=-1){
		        	alert(res);
		        	if (!confirm("繼續增加嗎?")) {
		        		window.location.href="showDxh.jsp";
					}		        	
		        }else{
		        	alert(res);
		        }			
			}
		})
	});
	
	$("#dxhb").keyup(function(){
		dxhb=$(this).val();
		if(dxhb.length==0){
			$("#bts").html("不能爲空");
			flag[1]=false;
			changeBtn();
			return;
		}
		flag[1]=true;
		$("#bts").html("*");
		changeBtn();
	});
	
	$("#dxhc").keyup(function(){
		dxhc=$(this).val();
		if(dxhc.length==0 || isNaN(dxhc) ){
			$("#cts").html("請輸入有效數字");
			flag[2]=false;
			changeBtn();
			return;
		}
		flag[2]=true;
		$("#cts").html("*");
		changeBtn();
	});
	
	$("#dxhd").bind("change",function(){
		dxhd=$(this).val();
		if(dxhd.length<10){
			flag[3]=false;
			changeBtn();
			return;
		}
		flag[3]=true;
		$("#dts").html("*");
		changeBtn();
	});
	
	
});
</script>
</head>
<body>
<div>
<label for="dxha">DxhA</label> &nbsp;&nbsp;&nbsp;<font id="ats" color="red"></font> <br/>
<input id="dxha" placeholder="數字。。。" type="text"> <br/>
<label for="dxhb">DxhB</label> &nbsp;&nbsp;&nbsp;<font id="bts" color="red"></font> <br/>
<input id="dxhb" type="text"><br/>
<label for="dxhc">DxhC</label> &nbsp;&nbsp;&nbsp;<font id="cts" color="red"></font> <br/>
<input id="dxhc" type="text"><br/>
<label for="dxhd">DxhD</label> &nbsp;&nbsp;&nbsp;<font id="dts" color="red"></font> <br/>
<input id="dxhd" type="date"><br/>
<input id="btn" value="add" type="button" disabled="disabled">
<h1 id="abc"></h1>
</div>
</body>
</html>

校驗

$("#dxha").keyup(function(){
		dxha=$(this).val();
		if(dxha.length==0 || isNaN(dxha) || dxha<1000 || dxha>9999){
			$("#ats").html("請輸入有效數字[1000,9999]");
			flag[0]=false;
			changeBtn();
			return;
		}
		$.ajax({
			//type:'post',
			url:"dxhAction?op=check&dxha="+dxha,
			success:function(res){
			  //  alert(res);
				if(res.indexOf("exist")!=-1){
					$("#ats").html("已被佔用");
					flag[0]=false; return;
				}
			}
		})	
		flag[0]=true;
		$("#ats").html("*");
		changeBtn();
	});
	private void check(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		String dxha=req.getParameter("dxha");
		if (dao.isExist("select * from dxh where dxha='"+dxha+"'")) {
			out.print("exist");
		} else {
			out.print("not");
		}
	}

增加

$("#btn").click(function(){
		$.ajax({
			url:"dxhAction?op=insert&dxha="+dxha+"&dxhb="+dxhb+"&dxhc="+dxhc+"&dxhd="+dxhd,
			success:function(res){		     
		        if(res.indexOf("success")!=-1){
		        	alert(res);
		        	if (!confirm("繼續增加嗎?")) {
		        		window.location.href="showDxh.jsp";
					}		        	
		        }else{
		        	alert(res);
		        }			
			}
		})
	});
private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException, JasperException {
		PrintWriter out = resp.getWriter();
		Dxh dxh=new Dxh();		
		org.apache.jasper.runtime.JspRuntimeLibrary.introspect(dxh, req);
		
		String  sql="insert into dxh(dxha,dxhb,dxhc,dxhd) values(?,?,?,?)";
		if (dao.exeUpdate(sql, new Object[] {dxh.getDxha(),dxh.getDxhb(),dxh.getDxhc(),dxh.getDxhd()})) {
			out.print("success");
		} else {
			out.print("failure");
		}
		
	}

效果

在這裏插入圖片描述
在這裏插入圖片描述

其他代碼

jar包

在這裏插入圖片描述

js顯示數據

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
input[type=button]{
	background-color: #4CAF50;
	color:white;
}
</style>
</head>
<link rel="stylesheet" href="bootstrap.min.css">
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function allSel(allsel){
	$(".choice").prop("checked",allsel.checked);
}

function delOne(i){
	if(confirm("確定刪除?")) {
		//發異步刪除請求,若刪除成功,則執行		$("#"+i).remove();
	}
}


function delSel(){
	if(!confirm("確定刪除?")) return;
	$("input[name='chk']:checked").each(
		function(){
			var i=$(this).val();
			$("#"+i).remove();
		}
	);
}

function getDxh() {
	
	var dxhb=$("#dxhb").val();
	$.ajax({
		type:'post',
		url:"dxhAction?op=select&dxhb="+dxhb,
		success:function(res){
		    console.log(typeof res);    
			res=eval("("+res+")");  //返回的一串數據格式符合JavaScript的對象格式 {a:value,b:value}  ,使用eval函數將字符串轉換爲對象
		//	alert(typeof res);
		    var  dxhs="";
		    for (var i = 0; i < res.length; i++) {    //遍歷數據
		    	var dxh=res[i];
				if (i%2==1) {
					dxhs=dxhs+	"<tr id="+dxh.dxha+" class='success'>";				
				} else {
					dxhs=dxhs+	"<tr id="+dxh.dxha+">";
				}
				dxhs=dxhs+"<td style='padding-left:36px;'><input name='chk' class='choice' value="+dxh.dxha+" type='checkbox'></td>";
				dxhs=dxhs+"<td>"+dxh.dxha+"</td><td>"+dxh.dxhb+"</td><td>"+dxh.dxhc+"</td>";
				dxhs=dxhs+"<td>"+dxh.dxhd+"</td><td><a href='javascript:void(0)' οnclick='delOne("+dxh.dxha+")'>del</td></tr>"
			}
		    $("#dxh").html(dxhs);  			
		}		
	});	
}

$(function(){
	getDxh();	
	$("#btnFind").click(
			function(){				
				getDxh();
			}
	
	);
});
</script>
</head>
<body>
	<div class="container">
	<div class="row clearfix">
	<div class="col-md-12 column">
	    <nav class="navbar navbar-default" role="navigation">
				
				
				<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
					
					<form class="navbar-form navbar-left" action="dxhAction">
						<div class="form-group">
						<input type="text" class="form-control" id="dxhb"  name="dxhb" placeholder="search by sencond field..." />
						</div> <button  id="btnFind" type="button" class="btn btn-default">查詢</button>
					</form>
					
				</div>
				
			</nav>

	    </div>
	
		<div class="col-md-12 column">
		
		
		
			<table id="tbl" class="table">
				<thead><tr><th>全選<input type="checkbox" value="全選" onclick="allSel(this)"></th>
						<th>編號	</th>	<th>	產品</th>		<th>交付時間	</th><th>狀態</th><th>處理</th>
					</tr>				</thead>
				<tbody  id="dxh">	</tbody>
			</table>
		</div>
		<div class="col-md-12 column">
			<input class="btn" type="button" value="刪除所選" style="margin-right:20px" onclick="delSel()">
			<input class="btn" type="button" value="增&nbsp;&nbsp;&nbsp;加"  style="margin-right:20px" onclick="window.location.href='add.html'">
			<input class="btn" type="button" value="java顯示數據"  style="padding:6px 20px" onclick="window.location.href='showDxhByJava.jsp'">
		</div>
	</div>
</div>
<h1 id="abc"></h1>
</body>

</html>

jstl顯示數據

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
input[type=button]{
	background-color: #4CAF50;
	color:white;
}
</style>
</head>
<link rel="stylesheet" href="bootstrap.min.css">
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function allSel(allsel){
	$(".choice").prop("checked",allsel.checked);
}

function delOne(i){
	if(confirm("確定刪除?")) {
		//發異步刪除請求,若刪除成功,則執行		$("#"+i).remove();
	}
}




function delSel(){
	if(!confirm("確定刪除?")) return;
	$("input[name='chk']:checked").each(
		function(){
			var i=$(this).val();
			$("#"+i).remove();
		}
	);
}
</script>
</head>
<body>
	<div class="container">
	<div class="row clearfix">
	    <div class="col-md-12 column">
	    <nav class="navbar navbar-default" role="navigation">
				
				
				<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
					
					<form class="navbar-form navbar-left" action="dxhAction">
						<div class="form-group">
						<input type="text" class="form-control"   name="dxhb" placeholder="search by sencond field..." />
						</div> <button type="submit" class="btn btn-default">查詢</button>
					</form>
					
				</div>
				
			</nav>

	    </div>
		<div class="col-md-12 column">
		
			<table id="tbl" class="table">
				<thead><tr><th>全選<input type="checkbox" value="全選" onclick="allSel(this)"></th>
						<th>編號	</th>	<th>	產品</th>		<th>交付時間	</th><th>狀態</th><th>處理</th>
					</tr>				</thead>
					
				<tbody>
				<!--   使用java提供的標籤來顯示數據                    -->
				<%int i=1; %>				
				<c:forEach items="${sessionScope.lstDxh}"  var="dxh" varStatus="zz">				
			    <c:choose>
			    <c:when test="${zz.index mod 2  == 1}">
			    <tr id="${dxh.dxha}" class='success'> 
			    </c:when>
			    <c:otherwise>
			     <tr id="${dxh.dxha}" >
			    </c:otherwise>
			    </c:choose>
				
				   
				   <td style='padding-left:36px;'> <%=i++ %>   <input name='chk' class='choice' value="${dxh.dxha}" type='checkbox'></td>
				   <td>${dxh.dxha}</td><td>${dxh.dxhb}</td><td>${dxh.dxhc}</td>
				   <td>${dxh.dxhd}</td><td><a href='javascript:void(0)' onclick='delOne(${dxh.dxha})'>del</td>
				 </tr>
				</c:forEach>				
				</tbody>
			</table>
		</div>
		<div class="col-md-12 column">
			<input class="btn" type="button" value="刪除所選" style="margin-right:20px" onclick="delSel()">
			<input class="btn" type="button" value="增&nbsp;&nbsp;&nbsp;加"  style="margin-right:20px" onclick="window.location.href='add.html'">
			<input class="btn" type="button" value="js顯示數據"  style="padding:6px 20px" onclick="window.location.href='showDxhByJs.jsp'">
		</div>
		
	</div>
</div>

</body>
<script type="text/javascript">

</script>
</html>

dao

package com.web508.dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.web508.model.Dxh;

public class DxhDao {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	static{
		String sPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		try {
			FileInputStream fin=new FileInputStream(sPath+"/db.properties");
			System.out.println(sPath);
			Properties prop=new Properties();
			prop.load(fin);
			driver=prop.getProperty("driver");
			url=prop.getProperty("url");
			user=prop.getProperty("user");
			password=prop.getProperty("password");
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}
	public DxhDao() {}
	private Connection getConn() {
		Connection con = null;
//		String driver = "com.mysql.cj.jdbc.Driver";
//		String url = "jdbc:mysql://localhost:3306/njdb?UseSSL=false&serverTimezone=Asia/Shanghai";
//		String user = "root";
//		String password = "123456";
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		return con;

	}

	public boolean isExist(String sql) {

		Connection conn = getConn();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			return rs.next();
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {

				}
			}
		}

	}

	public int isExist(String sql, Object[] obj) {
		int i = 0;
		Connection conn = getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			for (int j = 0; j < obj.length; j++) {
				pstmt = conn.prepareStatement(sql);
				pstmt.setObject(j, obj[j]);
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				i++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	public boolean exeUpdate(String sql, Object[] obj) {
		int i = 0;
		Connection conn = getConn();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			for (int j = 0; j < obj.length; j++) {
				pstmt.setObject(j + 1, obj[j]);
			}
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			i = -1;
		} finally {
			try {
				if (pstmt != null)
					pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i > 0;
	}

	public List<Dxh> getDxh(String sql) {
		List<Dxh> list = new ArrayList<Dxh>();
		Connection conn = getConn();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Dxh dxh = new Dxh();
				dxh.setDxha(rs.getInt("dxha"));
				dxh.setDxhb(rs.getString("dxhb"));
				dxh.setDxhc(rs.getFloat("dxhc"));
				dxh.setDxhd(rs.getString("dxhd"));
				list.add(dxh);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

}

實體類(自己生成)

jquery v1.10.2

bootstrap v3.3.7

過濾器

package com.web508.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * Servlet Filter implementation class TestFilter
 */
@WebFilter("/*")
public class TestFilter implements Filter {

    /**
     * Default constructor. 
     */
    public TestFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
//		System.out.println(request.getProtocol());
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

分析運行代碼補上所缺功能

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章