标签:数据库连接 NPU src cto readonly int 实验 重要 支持
实验感受:
本次实验最大的感受,就是不要改代码,自己写,代码改起来真的没完没了,不知道会出现什么问题。还有就是一定要清楚自己要怎么去写,流程很重要,一个个功能去实现。
主界面
数据库
主页面代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 %> <% String path = request.getContextPath(); //相对Path设置 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //相对Path设置 %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%--文档声明--%> <html> <head> <title>库存物资信息录入 </title> <%--页面标题--%> <body> <div id="Layer1" style="position:absolute; width:100%; height:100%; z-index:-1"> <img src="C:\Users\FuHeishi826\Desktop\课程实验\Extreme\background.jpg" height="100%" width="100%"/> </div> <script type="text/javascript" language="JavaScript"> //JS function validate() { var name = document.forms[0].name.value; //创建变量name var factory = document.forms[0].factory.value; //创建变量teacher var id = document.forms[0].id.value; var standard = document.forms[0].standard.value; //创建变量address if(name.length <= 0){ //判断姓名位数,必填 alert("名称不能为空,请输入名称!"); return false; } else if(factory.length <= 0){ //判断年龄,必填 alert("请输入合法生产工厂!"); return false; } else if(id.length <= 0){ //判断学号位数,必填 alert("型号不能为空,请输入型号!"); return false; } else if(standard.length <= 0){ //专业为必填 alert("规格不能为空,请输入商品规格!"); return false; } else{ return true; } //document.getElementById("form").submit(); } </script> </head> <body> <div id="Layer1" style="position:absolute; width:100%; height:100%; z-index:-1"> <img src="C:\Users\FuHeishi826\Desktop\课程实验\Extreme\background.jpg" height="100%" width="100%"/> </div> <br><%--换行--%> <center> <h2>库存商品信息录入</h2><hr size="1" noshade color="#000000"><%--横线--%> <form action="insert.jsp" method="post" id="form" onSubmit="return validate()" ><%--跳转到insert.jsp文件,方法为post--%> <table width="800" border="0" align="center"> <%--表格的宽为800,居中对齐--%> <tr> <td>库存商品名称: <input type="text" name="name"></td> </tr> <tr> <td>生产工厂: <input type="text" name="factory"></td> </tr> <tr> <td>型号: <input type="text" name="id"> </td> </tr> <tr> <td>规格: <input type="text" name="standard"> </td> </tr> <tr> <td> <input name="submit" type="submit" value="保存"/></td> </tr> </table> <p> </p> </form> <a href="show.jsp">查询所有库存商品信息</a> <%--链接到学生信息查询页面--%> </center> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <%@ page import="java.sql.*"%> <%--导入java中的sql包--%> <%@page import="com.javao.msg.DBUtil"%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 %> <% String path = request.getContextPath(); //相对Path设置 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //相对Path设置 %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%--文档声明--%> <html> <head> <base href="<%=basePath%>"> <%--设置基础路径--%> <title>库存商品信息删除界面</title> <%--页面标题--%> </head> <body> <div id="Layer1" style="position:absolute; width:100%; height:100%; z-index:-1"> <img src="C:\Users\FuHeishi826\Desktop\课程实验\Extreme\background.jpg" height="100%" width="100%"/> </div> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 String name = request.getParameter("name"); Connection conn = null; //定义静态数据库连接 //定义静态数据库连接 Statement stat = null; ResultSet rs = null; //将rs滞空。 conn = DBUtil.getConnection(); stat = conn.createStatement(); stat.executeUpdate("delete from good where name = ‘" + name + "‘"); //删除data表中的name字段 rs = stat.executeQuery("select * from good"); //查找data表 if(rs.next()) //判断结果集 { out.print("<center><br><br><h3>删除成功!</h3></center>"); } else{ out.print("<center><h3>删除失败!</h3></center>"); } %> <br> <br> <center><a href=add.jsp>返回添加库存商品信息页面</a><br/><br/><a href=show.jsp>返回库存商品信息查询页面</a></center> <%--设置居中--%> <% if(rs != null) { rs.close(); //关闭结果集,但是rs还是有null值。 rs = null; //将rs滞空。 } if(stat != null) { stat.close(); //关闭stat。 stat = null; //滞空stat。 } if(conn != null) { conn.close(); //关闭数据库连接 conn = null; } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <%@ page import="java.sql.*"%> <%--导入java中的sql包--%> <%@page import="com.javao.msg.DBUtil"%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 %> <% String path = request.getContextPath(); //相对Path设置 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //相对Path设置 %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%--文档声明--%> <html> <head> <title>库存商品信息</title> <%--页面标题--%> <body> <div id="Layer1" style="position:absolute; width:100%; height:100%; z-index:-1"> <img src="C:\Users\FuHeishi826\Desktop\课程实验\Extreme\background.jpg" height="100%" width="100%"/> </div> <%--JS--%> <script type="text/javascript""> function validate() { var name = document.forms[0].name.value; var factory = document.forms[0].factory.value; var id = document.forms[0].id.value; var standard = document.forms[0].standard.value; if(name.length <= 0){ //判断姓名位数,必填 alert("名称不能为空,请输入名称!"); return false; } else if(factory.length <= 0){ //判断年龄,必填 alert("请输入合法生产工厂!"); return false; } else if(id <= 0){ //判断学号位数,必填 alert("型号不能为空,请输入型号!"); return false; } else if(standard.length <= 0){ //专业为必填 alert("规格不能为空,请输入商品规格!"); return false; } else{ return true; } //document.getElementById("form").submit(); } </script> </head> <body background="img/background.jpg"> <% response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 String name = request.getParameter("name"); String factory = request.getParameter("factory"); String id = request.getParameter("id"); String standard = request.getParameter("standard"); Connection conn = null; //定义静态数据库连接 Statement stat = null; //滞空stat。 ResultSet rs = null; //将rs滞空。 conn = DBUtil.getConnection(); stat = conn.createStatement(); rs = stat.executeQuery("select * from good where name=‘" + name + "‘"); //查找data表id字段 %> <br> <h2>库存商品信息</h2> <hr noshade> <br> <h3>要修改的库存商品信息如下</h3> <table width="450" border="0" cellpadding="1" cellSpacing=1 style="font-size:15pt;border:dashed 1pt"> <tr align="center"> <td>库存商品名称</td> <td>库存商品生产工厂</td> <td>库存商品型号</td> <td>库存商品规格</td> </tr> <% while(rs.next()) { out.print("<tr>"); out.print("<td>" + rs.getString("name") + "</td>"); //输出name内容 out.print("<td>" + rs.getString("factory") + "</td>"); //输出gender内容 out.print("<td>" + rs.getString("id") + "</td>"); out.print("<td>" + rs.getString("standard") + "</td>"); //输出major内容 out.print("</tr>"); %> </table> <br> <br> <h3>将库存商品信息更改为:</h3> <form action="updateShow.jsp" method="post" onSubmit="return validate()"> <h4>库存商品名称:<input type="text" name="name" value="<%=rs.getString("name") %>" title="库存商品名称不能改变" onClick="return checkName(name)"readonly="readonly"></input><br></h4> <h4>库存商品生产工厂:<input type="text" name="factory" title="库存商品生产工厂不能为空"></input><br></h4> <h4>库存商品型号:<input type="text" name="id" title="库存商品型号不能为空"></input><br></h4> <h4>库存商品规格:<input type="text" name="standard" title="库存商品规格不能为空"></input><br></h4> <input type="submit" value="修改"/> </form> <a href=add.jsp>返回库存商品添加信息页面</a><br/><a href=show.jsp>返回库存商品信息查询页面</a> <% } %> <% if(rs != null) { rs.close(); //关闭结果集,但是rs还是有null值。 rs = null; //将rs滞空。 } if(stat != null) { stat.close(); //关闭stat。 stat = null; //滞空stat。 } if(conn != null) { conn.close(); //关闭数据库连接 conn = null; } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <%@ page import="java.sql.*"%> <%--导入java中的sql包--%> <%@page import="com.javao.msg.DBUtil"%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 %> <% String path = request.getContextPath(); //相对Path设置 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //相对Path设置 %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%--文档声明--%> <html> <head> <base href="<%=basePath%>"> <title>修改界面</title> </head> <body> <div id="Layer1" style="position:absolute; width:100%; height:100%; z-index:-1"> <img src="C:\Users\FuHeishi826\Desktop\课程实验\Extreme\background.jpg" height="100%" width="100%"/> </div> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 String name1 = request.getParameter("name"); String factory1 = request.getParameter("factory"); String id1 = request.getParameter("id"); String standard1 = request.getParameter("standard"); Connection conn = null; //定义静态数据库连接 Statement stat = null; conn = DBUtil.getConnection(); stat = conn.createStatement(); stat.execute("update good set name=‘" + name1 + "‘ ,factory=‘" + factory1 + "‘ ,id=‘" + id1 + "‘ ,standard=‘" + standard1+"‘ where name=‘" + name1 + "‘"); ResultSet rs = stat.executeQuery("select * from good where name=‘" + name1 + "‘"); //查找data表id字段 %> <br> <h3>修改成功!</h3> <%--标题样式3--%> <br> <h3>修改后的库存商品信息为:</h3> <%--标题样式3--%> <hr noshade> <br> <br> <table width="450" border="100" cellSpacing=1 style="font-size:15pt;border:dashed 1pt"> <%--表格宽度450--%> <tr> <td>库存商品名称</td> <td>库存商品生产工厂</td> <td>库存商品型号</td> <td>库存商品规格</td> </tr> <% while(rs.next()) { out.print("<tr>"); out.print("<td>" + rs.getString("name") + "</td>"); //输出name内容 out.print("<td>" + rs.getString("factory") + "</td>"); //输出gender内容 out.print("<td>" + rs.getString("id") + "</td>"); out.print("<td>" + rs.getString("standard") + "</td>"); //输出major内容 out.print("</tr>"); } %> </table> <br> <br> <a href=add.jsp>返回库存商品添加信息页面</a><br/><a href=show.jsp>返回库存商品信息查询页面</a> <% if(rs != null) { rs.close(); //关闭结果集,但是rs还是有null值。 rs = null; //将rs滞空。 } if(stat != null) { stat.close(); //关闭stat。 stat = null; //滞空stat。 } if(conn != null) { conn.close(); //关闭数据库连接 conn = null; } %> </body> </html>
package com.javao.msg; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { try { //1 鍔犺浇椹卞姩 Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "root"; String password = "123456"; String url = "jdbc:mysql://localhost:3306/inventory?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"; Connection connection = null; try { //2 鍒涘缓閾炬帴瀵硅薄connection connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } //鍏抽棴璧勬簮鐨勬柟娉? public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:数据库连接 NPU src cto readonly int 实验 重要 支持
原文地址:https://www.cnblogs.com/zhoulonghai/p/10117203.html