Myeclipse+tomcat+mysql,B/S实现学生信息的增删改查(实现篇)
2014-08-07 17:06
706 查看
小生第一次用Myeclipse和Mysql做东西,边学边做,处处遇到问题,然后自己搜资料和分析去解决
测试通过!
工程文件目录
index.jsp
showInfo.jsp
insert.jsp
delete.jsp
addStuInfo.jsp
update.jsp
updateShow.jsp
5个条件查询jsp大部分相同,贴个ID查询
select_id.jsp
这个目前还有相关BUG还没解决,给点时间再来。
测试通过!
工程文件目录
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还没解决,给点时间再来。
相关文章推荐
- java(jsp)简单实现数据库学生信息的增删改查案例
- Ubuntu下用C语言连接MySql——实现增删改查排序
- MySQL常用SQL语句(Python实现学生、课程、选课表增删改查)
- 学生信息管理系统--(Java+MySQL实现)
- Mysql的增删改查语句简单实现
- PHP+MYSQL网站信息的增删改查代码
- MySQL用while实现for循环嵌套(插入学生成绩信息功能)
- 学生信息管理系统--(Java+MySQL实现)
- 学生信息记录系统,主要实现增删改查功能,考虑用堆栈、队列来写下
- 学生信息记录系统,主要实现增删改查功能
- 学生管理系统--实现了系统的增删改查~
- 学生成绩信息的增删改查 通过控制台实现
- Java+MyEclipse+Tomcat (六)详解Servlet和DAO数据库增删改查操作
- java连接mysql实现增删改查代码
- myeclipse 实现框架 spring+springmvc+springsecurity+myibatis+mysql用户认证和人员增删改查
- 适易信息管理系统架构、配置实现增删改查中工具类介绍
- MyEclipse+MySQL+Tomcat实现jsp登录
- MyEclipse10+Jdk1.7+OSGI+MySql实现数据库的增删改查
- 【mfc】学生信息管理,实现List控件节点的增删改查
- Unity同时充当移动端和后台的界面+Sevelet+Mysql+Tomcat 学生信息的录入