Myeclipse+tomcat+mysql,B/S實現學生資訊的增刪改查(實現篇)
阿新 • • 發佈:2019-01-05
小生第一次用Myeclipse和Mysql做東西,邊學邊做,處處遇到問題,然後自己搜資料和分析去解決
測試通過!
工程檔案目錄
index.jsp
<%@ 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 background="pic/background.jpg"> <br /> <br /> <center> <a href="addStuInfo.jsp">點此新增學生資訊</a> <br /> <br /> <a href="showInfo.jsp">點此查詢學生資訊</a> <br /> <br /> <a href="showInfo.jsp">點此修改學生資訊</a> <br /> <br /> <a href="showInfo.jsp">點此刪除學生資訊</a> <br /> <br /> <br> </center> </body> </html>
showInfo.jsp
<%@ page language="java" import="java.util.*" import="java.sql.*" 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 'showInfo.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 background="pic/background.jpg"> <% response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); String name = request.getParameter("name"); String age = request.getParameter("age"); String gender = request.getParameter("gender"); String major = request.getParameter("major"); Connection conn = null; Statement stat = null; ResultSet rs = null; Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/jsp"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement(); rs = stat.executeQuery("select * from student"); %> <br> <h2>學生資訊</h2> <hr> <br> <h3>全部學生資訊如下</h3> <table width="450" border="100" cellSpacing=3 style="font-size:15pt;border:dashed 1pt"> <tr> <td>學號</td> <td>姓名</td> <td>年齡</td> <td>性別</td> <td>專業</td> </tr> <% while(rs.next()) { out.print("<tr>"); out.print("<td>" + rs.getInt("id") + "</td>"); out.print("<td>" + rs.getString("name") + "</td>"); out.print("<td>" + rs.getInt("age") + "</td>"); out.print("<td>" + rs.getString("gender") + "</td>"); out.print("<td>" + rs.getString("major") + "</td>"); %> <td><a href="delete.jsp?id=<%=rs.getInt("id") %>">刪除</a></td> <td><a href="update.jsp?id=<%=rs.getInt("id") %>">修改</a></td> <% out.print("</tr>"); } %> </table> <br> <form action="select_id.jsp" method="post"> <h3>按學號查詢:<input type="text" name="id" value="" title="學號不能為空" ></input> <input type="submit" value="查詢"/></h3> <br> </form> <form action="select_name.jsp" method="post"> <h3>按姓名查詢:<input type="text" name="name" value="" title="姓名不能為空"></input> <input type="submit" value="查詢" /></h3> <br> </form> <form action="select_age.jsp" method="post"> <h3> 按年齡查詢:<input type="text" name="age" value="" title="年齡不能為空"></input> <input type="submit" value="查詢"/></h3> <br> </form> <form action="select_gender.jsp" method="post"> <h3> 按性別查詢:<input type="text" name="gender" value="" title="性別不能為空"></input> <input type="submit" value="查詢"/></h3> <br> </form> <form action="select_major.jsp" method="post"> <h3> 按專業查詢:<input type="text" name="major" value="" title="專業不能為空"></input> <input type="submit" value="查詢"/></h3> <br> </form> <br> <h3><a href=addStuInfo.jsp>返回新增學生資訊頁面</a></h3> <br> <% if(rs != null) { rs.close(); rs = null; } if(stat != null) { stat.close(); stat = null; } if(conn != null) { conn.close(); conn = null; } %> </body> </html>
insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="java.sql.*"%> <% 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 'insert.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 background="pic/background.jpg"> <% request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); String name = request.getParameter("name"); //System.out.println(name); String age = request.getParameter("age"); String gender = request.getParameter("gender"); String major = request.getParameter("major"); Connection conn = null; Statement stat = null; ResultSet rs = null; Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/jsp"; String user="root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); stat = conn.createStatement(); String sql = "insert into student(id,name,age,gender,major) values(" + id + ",'" + name + "'," + age + ",'" + gender + "','" + major + "')"; stat.executeUpdate(sql); rs = stat.executeQuery("select * from student"); %> <center> <% if(rs.next()) { out.print("<br><h3>成功輸入!</h3>"); } else{ out.print("<br><h3>輸入失敗!</h3>"); } %> <br> <a href=addStuInfo.jsp>返回新增資訊頁面</a> <a href=showInfo.jsp>進入資訊查詢頁面</a> </center> <% if(rs != null) { rs.close(); rs = null; } if(stat != null) { stat.close(); stat = null; } if(conn != null) { conn.close(); conn = null; } %> </body> </html>
delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
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 'delete.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 background="pic/background.jpg">
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id"); //獲取id
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jsp";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url,user,password);
stat = conn.createStatement();
stat.executeUpdate("delete from student where id = " + id + "");
rs = stat.executeQuery("select * from student");
if(rs.next())
{
out.print("<center><br><br><h3>刪除成功!</h3></center>");
}
else{
out.print("<center><h3>刪除失敗!</h3></center>");
}
%>
<br>
<br>
<center> <a href=addStuInfo.jsp>返回新增資訊頁面</a> <a href=showInfo.jsp>返回資訊查詢頁面</a></center>
<%
if(rs != null)
{
rs.close();
rs = null;
}
if(stat != null)
{
stat.close();
stat = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
%>
</body>
</html>
addStuInfo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
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>輸入學生資訊介面</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"">
function validate() {
var id = document.forms[0].id.value;
var name = document.forms[0].name.value;
var age = document.forms[0].age.value;
var major = document.forms[0].major.value;
if (id <= 0) {
alert("學號不能為空,請輸入學號!");
return false;
} else if (name.length <= 0) {
alert("姓名不能為空,請輸入姓名!");
return false;
} else if (age <= 0) {
alert("請輸入合法年齡!");
return false;
}
else if (major.length <= 0) {
alert("專業不能為空,請輸入所學專業!");
return false;
}
else {
return true;
}
//document.getElementById("form").submit();
}
</script>
</head>
<body background="pic/background.jpg">
<br>
<center>
<h2>
新增學生資訊
</h2>
<hr>
<form action="insert.jsp" method="post" id="form"
onSubmit="return validate()">
<h4>
學號:
<input type="text" name="id" class="{required:true}"></input>
<br>
</h4>
<h4>
姓名:
<input type="text" name="name"></input>
<br>
</h4>
<h4>
年齡:
<input type="text" name="age"></input>
<br>
</h4>
<h4>
性別:
<input type="radio" name="gender" value="男">
男
<input type="radio" name="gender" value="女">
女
<br>
</h4>
<h4>
專業:
<input type="text" name="major"></input>
<br>
</h4>
<input type="submit" value="提交" />
</form>
<a href="showInfo.jsp">查詢所有學生資訊</a>
</center>
</body>
</html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
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 'update.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"">
function validate() {
var id = document.forms[0].id.value;
var name = document.forms[0].name.value;
var age = document.forms[0].age.value;
var major = document.forms[0].major.value;
if (id <= 0) {
alert("學號不能為空,請輸入學號!");
return false;
} else if (name.length <= 0) {
alert("姓名不能為空,請輸入姓名!");
return false;
} else if (age <= 0) {
alert("請輸入合法年齡!");
return false;
}
else if (major.length <= 0) {
alert("專業不能為空,請輸入所學專業!");
return false;
}
else {
return true;
}
//document.getElementById("form").submit();
}
function checkName(sName) {
return "李王張劉陳楊趙黃周吳徐孫胡朱高林何郭馬羅樑宋鄭謝韓唐馮於董蕭程曹袁鄧許傅沈曾彭呂蘇盧蔣蔡賈丁魏薛葉閻餘潘杜戴夏鍾汪田任姜範方石姚譚廖鄒熊金陸郝孔白崔康毛邱秦江史顧侯邵孟龍萬段章錢湯尹黎易常武喬賀賴龔文龐樊蘭殷施陶洪翟安顏倪嚴牛溫蘆季俞章魯葛伍韋申尤畢聶叢焦向柳邢路嶽齊沿梅莫莊辛管祝左塗谷祁時舒耿牟卜路詹關苗凌費紀靳盛童歐甄項曲成遊陽裴席衛查屈鮑位覃霍翁隋植甘景薄單包司柏寧柯阮桂閔歐陽解強柴華車冉房"
.indexOf(sName[0])
}
</script>
</head>
<body background="pic/background.jpg">
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jsp";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
rs = stat.executeQuery("select * from student where id=" + id + "");
%>
<br>
<h2>
學生資訊
</h2>
<hr>
<br>
<h3>
要修改的學生資訊如下
</h3>
<table width="450" border="100" cellSpacing=1
style="font-size: 15pt; border: dashed 1pt">
<tr>
<td>
學號
</td>
<td>
姓名
</td>
<td>
年齡
</td>
<td>
性別
</td>
<td>
專業
</td>
</tr>
<%
while (rs.next()) {
out.print("<tr>");
out.print("<td>" + rs.getInt("id") + "</td>");
out.print("<td>" + rs.getString("name") + "</td>");
out.print("<td>" + rs.getInt("age") + "</td>");
out.print("<td>" + rs.getString("gender") + "</td>");
out.print("<td>" + rs.getString("major") + "</td>");
out.print("</tr>");
%>
</table>
<br>
<br>
<h3>
將學生資訊更改為:
</h3>
<form action="updateShow.jsp" method="post"
onSubmit="return validate()">
<h4>
學號:
<input type="text" name="id" value="<%=rs.getInt("id")%>"
title="學號不能改變" readonly="readonly"></input>
<br>
</h4>
<h4>
姓名:
<input type="text" name="name" title="姓名不能為空"
onclick="return checkName(name)"></input>
<br>
</h4>
<h4>
年齡:
<input type="text" name="age" title="年齡不能為空"></input>
<br>
</h4>
<h4>
性別:
<input type="radio" name="gender" value="男">
男
<input type="radio" name="gender" value="女">
女
<br>
</h4>
<h4>
專業:
<input type="text" name="major" title="專業不能為空"></input>
<br>
</h4>
<input type="submit" value="修改" />
</form>
<br>
<h3>
<a href=addStuInfo.jsp>返回新增資訊頁面</a>
</h3>
<h3>
<a href=showInfo.jsp>返回資訊查詢頁面</a>
</h3>
<%
}
%>
<%
if (rs != null) {
rs.close();
rs = null;
}
if (stat != null) {
stat.close();
stat = null;
}
if (conn != null) {
conn.close();
conn = null;
}
%>
</body>
</html>
updateShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
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 'updateShow.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"">
function validate() {
var id = document.forms[0].id.value;
var name = document.forms[0].name.value;
var age = document.forms[0].age.value;
var major = document.forms[0].major.value;
if (id <= 0) {
alert("學號不能為空,請輸入學號!");
return false;
} else if (name.length <= 0) {
alert("姓名不能為空,請輸入姓名!");
return false;
} else if (age <= 0) {
alert("請輸入合法年齡!");
return false;
}
else if (major.length <= 0) {
alert("專業不能為空,請輸入所學專業!");
return false;
}
else {
return true;
}
//document.getElementById("form").submit();
}
function checkName(sName) {
return "李王張劉陳楊趙黃周吳徐孫胡朱高林何郭馬羅樑宋鄭謝韓唐馮於董蕭程曹袁鄧許傅沈曾彭呂蘇盧蔣蔡賈丁魏薛葉閻餘潘杜戴夏鍾汪田任姜範方石姚譚廖鄒熊金陸郝孔白崔康毛邱秦江史顧侯邵孟龍萬段章錢湯尹黎易常武喬賀賴龔文龐樊蘭殷施陶洪翟安顏倪嚴牛溫蘆季俞章魯葛伍韋申尤畢聶叢焦向柳邢路嶽齊沿梅莫莊辛管祝左塗谷祁時舒耿牟卜路詹關苗凌費紀靳盛童歐甄項曲成遊陽裴席衛查屈鮑位覃霍翁隋植甘景薄單包司柏寧柯阮桂閔歐陽解強柴華車冉房"
.indexOf(sName[0])
}
</script>
</head>
<body background="pic/background.jpg">
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jsp";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
rs = stat.executeQuery("select * from student where id=" + id + "");
%>
<br>
<h2>
學生資訊
</h2>
<hr>
<br>
<h3>
要修改的學生資訊如下
</h3>
<table width="450" border="100" cellSpacing=1
style="font-size: 15pt; border: dashed 1pt">
<tr>
<td>
學號
</td>
<td>
姓名
</td>
<td>
年齡
</td>
<td>
性別
</td>
<td>
專業
</td>
</tr>
<%
while (rs.next()) {
out.print("<tr>");
out.print("<td>" + rs.getInt("id") + "</td>");
out.print("<td>" + rs.getString("name") + "</td>");
out.print("<td>" + rs.getInt("age") + "</td>");
out.print("<td>" + rs.getString("gender") + "</td>");
out.print("<td>" + rs.getString("major") + "</td>");
out.print("</tr>");
%>
</table>
<br>
<br>
<h3>
將學生資訊更改為:
</h3>
<form action="updateShow.jsp" method="post"
onSubmit="return validate()">
<h4>
學號:
<input type="text" name="id" value="<%=rs.getInt("id")%>"
title="學號不能改變" readonly="readonly"></input>
<br>
</h4>
<h4>
姓名:
<input type="text" name="name" title="姓名不能為空"
onclick="return checkName(name)"></input>
<br>
</h4>
<h4>
年齡:
<input type="text" name="age" title="年齡不能為空"></input>
<br>
</h4>
<h4>
性別:
<input type="radio" name="gender" value="男">
男
<input type="radio" name="gender" value="女">
女
<br>
</h4>
<h4>
專業:
<input type="text" name="major" title="專業不能為空"></input>
<br>
</h4>
<input type="submit" value="修改" />
</form>
<br>
<h3>
<a href=addStuInfo.jsp>返回新增資訊頁面</a>
</h3>
<h3>
<a href=showInfo.jsp>返回資訊查詢頁面</a>
</h3>
<%
}
%>
<%
if (rs != null) {
rs.close();
rs = null;
}
if (stat != null) {
stat.close();
stat = null;
}
if (conn != null) {
conn.close();
conn = null;
}
%>
</body>
</html>
5個條件查詢jsp大部分相同,貼個ID查詢
select_id.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
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 'select_id.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 background="pic/background.jpg">
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jsp";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
rs = stat.executeQuery("select * from student where id=" + id + "");
%>
<br>
<h3>
符合條件的學生資訊
</h3>
<hr>
<br>
<table width="450" border="100" cellSpacing=1
style="font-size: 15pt; border: dashed 1pt">
<tr>
<td>
學號
</td>
<td>
姓名
</td>
<td>
年齡
</td>
<td>
性別
</td>
<td>
專業
</td>
</tr>
<%
if (rs.next()) {
out.print("<tr>");
out.print("<td>" + rs.getInt("id") + "</td>");
out.print("<td>" + rs.getString("name") + "</td>");
out.print("<td>" + rs.getInt("age") + "</td>");
out.print("<td>" + rs.getString("gender") + "</td>");
out.print("<td>" + rs.getString("major") + "</td>");
%>
<td>
<a href="delete.jsp?id=<%=rs.getInt("id")%>">刪除</a>
</td>
<td>
<a href="update.jsp?id=<%=rs.getInt("id")%>">修改</a>
</td>
<%
out.print("</tr>");
} else {
out.print("<h4>不存在此條件的資訊!</h4>");
}
%>
</table>
<br>
<br>
<h4>
<a href=showInfo.jsp>返回查詢頁面</a>
</h4>
<%
if (rs != null) {
rs.close();
rs = null;
}
if (stat != null) {
stat.close();
stat = null;
}
if (conn != null) {
conn.close();
conn = null;
}
%>
</body>
</html>
這個目前還有相關BUG還沒解決,給點時間再來。