一個Java系統測試
阿新 • • 發佈:2018-12-14
實驗感受:
本次實驗最大的感受,就是不要改程式碼,自己寫,程式碼改起來真的沒完沒了,不知道會出現什麼問題。還有就是一定要清楚自己要怎麼去寫,流程很重要,一個個功能去實現。
主介面
資料庫
主頁面程式碼
<%@ 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(); } } }