java第十三週測試記錄
阿新 • • 發佈:2018-12-13
今天課上遇到了問題,在我的上一篇隨筆,這個阻礙了我很長時間,而且上一次也是這個問題,真的吃一塹不長一智,這次我應該就記住了,嗯。
設計思路:
倆個庫:
1.一個庫存商品,商品的基本屬性和商品的數量(數量這個資料與後面的庫裡面資料會結合)被稱為庫存數量。
2.一個庫存商品出入庫資訊,有取貨和送貨的資訊。
①我用一個Int型(flag)來區分,flag=0為出庫,flag=1為入庫。
②也有一個數量,是挪動數量,這個數量與前面那個庫的數量有一定的關聯,就是取出貨後,那個庫存數量會減少,當存入貨後,那個庫存數量會增加。
③剩下的屬性全用字串型別。
我採用的是jsp+severlet+dao+bean模式:
程式碼:
jsp:
主要函式表的幾個jsp:
main.jsp:
<%@ 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> <style> body{ text-align:center} /* css註釋:為了觀察效果設定寬度 邊框 高度等樣式 */ </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>main</title> </head> <body> <br><br><br><br><br><br><br>//格式好看一點,下面同理 <div> <input type="button" onclick="test1()" value="ru_ku" style="width:200px;height:50px" /> <script > function test1(){ var url = "ru_ku.jsp"; window.location.href= url; } </script> </div> <br><br><br><br><br> <div> <input type="button" onclick="test2()" value="chu_ku" style="width:200px;height:50px"/> <script > function test2(){ var url = "chu_ku.jsp"; window.location.href= url; } </script> </div> <br><br><br><br><br> <div> <input type="button" onclick="test3()" value="cha_ku" style="width:200px;height:50px"/> <script > function test3(){ var url = "cha_ku.jsp"; window.location.href= url; } </script> </div> <br><br><br><br><br> <div> <input type="button" onclick="test4()" value="ku" style="width:200px;height:50px"/> <script > function test4(){ var url = "ku.jsp"; window.location.href= url; } </script> </div> </body> </html>
chuku.jsp:
<%@ 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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>add</title> </head> <body> <div align="center"> <h1 style="color: red;">出庫資訊填表</h1> <a href="main.jsp">返回主頁</a> <form action="TableServlet?method=chu_ku" method="post"> <div class="a"> 商品名<input type="text" id="name" name="name"/> </div> <div class="a"> 廠家名<input type="text" id="changjia" name="changjia" /> </div> <div class="a"> 型號<input type="text" id="xinghao" name="xinghao" /> </div> <div class="a"> 規格<input type="text" id="guige" name="guige" /> </div> <div class="a"> 數量<input type="text" id="shuliang" name="shuliang" /> </div> <div class="a"> 日期<input type="text" id="date" name="date" /> </div> <div class="a"> 單位<input type="text" id="danwei" name="danwei" /> </div> <div class="a"> 提貨人<input type="text" id="renming" name="renming" /> </div> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> </body> </html>
ru_ku.jsp:
<%@ 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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>add</title> </head> <body> <div align="center"> <h1 style="color: red;">入庫資訊填表</h1> <a href="main.jsp">返回主頁</a> <form method="post" action="TableServlet?action=in" > <div class="a"> 商品名<input type="text" id="name" name="name"/> </div> <div class="a"> 廠家名<input type="text" id="changjia" name="changjia" /> </div> <div class="a"> 型號<input type="text" id="xinghao" name="xinghao" /> </div> <div class="a"> 規格<input type="text" id="guige" name="guige" /> </div> <div class="a"> 數量<input type="text" id="shuliang" name="shuliang" /> </div> <div class="a"> 日期<input type="text" id="date" name="date" /> </div> <div class="a"> 單位<input type="text" id="danwei" name="danwei" /> </div> <div class="a"> 送貨人<input type="text" id="renming" name="renming" /> </div> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> </body> </html>
接下來是操作商品庫的jsp
ku.jsp:
<%@ 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> <style> body{ text-align:center} /* css註釋:為了觀察效果設定寬度 邊框 高度等樣式 */ </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>cang_ku</title> </head> <body> <body> <br><br><br><br><br><br><br> <div> <input type="button" onclick="test1()" value="cha_ku" style="width:200px;height:50px" /> <script > function test1(){ var url = "cha_ku.jsp"; window.location.href= url; } </script> </div> <br><br><br><br><br> <div> <input type="button" onclick="test2()" value="add_ku" style="width:200px;height:50px"/> <script > function test2(){ var url = "add_ku.jsp"; window.location.href= url; } </script> </div> <br><br><br><br><br> <div> <input type="button" onclick="test3()" value="delete_ku" style="width:200px;height:50px"/> <script > function test3(){ var url = "delete_ku.jsp"; window.location.href= url; } </script> </div> <br><br><br><br><br> </body> </html>
add_ku.jsp:
<%@ 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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>add_ku</title> </head> <body> <div align="center"> <h1 style="color: red;">庫內物品錄入</h1> <a href="ku.jsp">返回主頁</a> <form action="GoodServlet?action=add" method="post" > <div class="a"> 商品名<input type="text" id="name" name="name"/> </div> <div class="a"> 廠家名<input type="text" id="changjia" name="changjia" /> </div> <div class="a"> 型號<input type="text" id="xinghao" name="xinghao" /> </div> <div class="a"> 規格<input type="text" id="guige" name="guige" /> </div> <div class="a"> 數量<input type="text" id="number" name="number" /> </div> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> </body> </html>
delete.jsp:
<%@ 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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>delete_ku</title> </head> <body> <div align="center"> <h1 style="color: red;">庫內物品刪除</h1> <a href="ku.jsp">返回主頁</a> <form action="GoodServlet?action=delete" method="post" > <div class="a"> 商品名<input type="text" id="name" name="name"/> </div> <div class="a"> 廠家名<input type="text" id="changjia" name="changjia" /> </div> <div class="a"> 型號<input type="text" id="xinghao" name="xinghao" /> </div> <div class="a"> 規格<input type="text" id="guige" name="guige" /> </div> </form> </div> </body> </html>
cha_ku.jsp:
<%@ 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>chao_zhao</title> </head> <body> <div align="center"> <h1 style="color: red;">庫內物品查詢</h1> <a href="ku.jsp">返回主頁</a> <form action="GoodServlet?action=cha_xun" method="post" > <br><br><br><br><br><br><br><br><br> <br><br><br><br> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> </body> </html>
java程式碼:
Bean:
package com.jsp.bean; public class Good_Bean { int id; String name; String xinghao; String guige; String changjia; int number; public Good_Bean() {} public Good_Bean(int id, String name,String changjia ,String xinghao, String guige, int number) { // TODO 自動生成的建構函式存根 this.id=id; this.xinghao=xinghao; this.guige=guige; this.changjia=changjia; this.number=number; } public Good_Bean(String name, String changjia,String xinghao, String guige, int number) { // TODO 自動生成的建構函式存根 this.xinghao=xinghao; this.guige=guige; this.changjia=changjia; this.number=number; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public String getGuige() { return guige; } public void setGuige(String guige) { this.guige = guige; } public String getChangjia() { return changjia; } public void setChangjia(String changjia) { this.changjia = changjia; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } }
package com.jsp.bean; public class Table_Bean { int id; String name; String xinghao; String guige; String changjia; int shuliang; String date; String danwei; String renming; int flag; public Table_Bean(int id, String name, String changjia, String xinghao, String guige, String date, String danwei, String renming, int shuliang, int flag) { this.id=id; this.name=name; this.changjia=changjia; this.xinghao=xinghao; this.guige=guige; this.date=date; this.danwei=danwei; this.renming=renming; this.shuliang=shuliang; this.flag=flag; // TODO 自動生成的建構函式存根 } public Table_Bean( String name, String changjia, String xinghao, String guige, String date, String danwei, String renming, int shuliang, int flag) { this.name=name; this.changjia=changjia; this.xinghao=xinghao; this.guige=guige; this.date=date; this.danwei=danwei; this.renming=renming; this.shuliang=shuliang; this.flag=flag; // TODO 自動生成的建構函式存根 } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public String getGuige() { return guige; } public void setGuige(String guige) { this.guige = guige; } public String getChangjia() { return changjia; } public void setChangjia(String changjia) { this.changjia = changjia; } public int getShuliang() { return shuliang; } public void setShuliang(int shuliang) { this.shuliang = shuliang; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getDanwei() { return danwei; } public void setDanwei(String danwei) { this.danwei = danwei; } public String getRenming() { return renming; } public void setRenming(String renming) { this.renming = renming; } public int getFlag() { return flag; } public void setFlag(int flag) { this.flag = flag; } }
Dao:
package com.jsp.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.jsp.bean.Good_Bean; import com.jsp.util.*; public class Good_Dao { /** * * 檢視資料是否存在 * @param name * @param xinghao * @param guige * @param changjia * @return */ public boolean cha_xun (String name,String xinghao,String guige,String changjia) { DButil DB= new DButil(); int i =0; Connection con =DB.getConn("ku"); try { Statement state = con.createStatement(); ResultSet rs = state.executeQuery("select * from goods where name='" + name + "'"); if (rs.next()) { if(rs.getString("guige").equals(guige)) { i++; } if(rs.getString("changjia").equals(changjia)) { i++; } if(rs.getString("xinghao").equals(xinghao)) { i++; } } } catch (SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } if(i==3) { return true; } else { return false; } } public int cha__xun(String name,String xinghao,String guige,String changjia) { DButil DB= new DButil(); int i =0; int id=0; Connection con =DB.getConn("ku"); try { Statement state = con.createStatement(); ResultSet rs = state.executeQuery("select * from goods where name='" + name + "'"); if (rs.next()) { if(rs.getString("guige").equals(guige)) { i++; } if(rs.getString("changjia").equals(changjia)) { i++; } if(rs.getString("xinghao").equals(xinghao)) { i++; } if(i==3) { id=rs.getInt("id"); } } } catch (SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } return id; } /** * 全顯示商品資料 * @return */ public List<Good_Bean> list() { DButil DB =new DButil(); String sql = "select * from goods"; List<Good_Bean> list = new ArrayList<>(); Connection conn = DB.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Good_Bean good_bean = null; while (rs.next()) { int id2 = rs.getInt("id"); String name2 = rs.getString("name"); String changjia2 = rs.getString("changjia"); String xinghao2 = rs.getString("xinghao"); String guige2 = rs.getString("guige"); int number2 =rs.getInt("number"); good_bean = new Good_Bean(); good_bean.setId(id2); good_bean.setName(name2); good_bean.setChangjia(changjia2); good_bean.setXinghao(xinghao2); good_bean.setGuige(guige2); good_bean.setNumber(number2); list.add(good_bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(rs, state, conn); } return list; } /** * 增加 * @param good_bean * @return */ public boolean add(Good_Bean good_bean) { DButil DB =new DButil(); String sql = "insert into goods(name, changjia, xinghao,guige,number) values('" + good_bean.getName() + "','" + good_bean.getChangjia() + "','" + good_bean.getXinghao() + "','" + good_bean.getGuige()+"','" + good_bean.getNumber()+"')"; Connection conn = DB.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a=state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DB.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 刪除 * @param * @return */ public boolean delete (String name) { boolean f = false; String sql = "delete from goods where name='" + name + "'"; DButil DB =new DButil(); Connection conn = DB.getConn("goods"); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(state, conn); } if (a > 0) { f = true; } return f; } }
package com.jsp.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.jsp.bean.Table_Bean; import com.jsp.util.*; public class Table_Dao { public List<Table_Bean> list() { DButil DB =new DButil(); String sql = "select * from tab"; List<Table_Bean> list = new ArrayList<>(); Connection conn = DB.getConn("ku"); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Table_Bean table_bean = null; while (rs.next()) { int id2 = rs.getInt("id"); String name2 = rs.getString("name"); String xinghao2 = rs.getString("xinghao"); String changjia2 = rs.getString("changjia"); String guige2 = rs.getString("guige"); String date2 = rs.getString("date"); String danwei2 = rs.getString("danwei"); String renming2 = rs.getString("renming"); int flag =rs.getInt("flag"); int shuliang2 =rs.getInt("shuliang"); table_bean = new Table_Bean(id2, name2, changjia2,xinghao2,guige2,date2,danwei2,renming2,shuliang2,flag); list.add(table_bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(rs, state, conn); } return list; } public boolean _ku(Table_Bean table_bean) throws SQLException { DButil DB =new DButil(); Connection conn = DB.getConn(); Statement state = null; String sql = "insert into tab(name,changjia,xinghao,guige,date,danwei,renming,shuliang,flag) values('" + table_bean.getName() + "','" + table_bean.getChangjia() + "','" + table_bean.getXinghao() + "','" + table_bean.getGuige()+"','" + table_bean.getDate()+"','"+table_bean.getDanwei()+"','" + table_bean.getRenming()+"','" +table_bean.getShuliang()+"','" + table_bean.getFlag()+ "')"; boolean f = false; int a=0; try { state = conn.createStatement(); state.execute(sql); a=1; } catch (Exception e) { e.printStackTrace(); } finally { DB.close(state,conn); } DB.close(state,conn); if (a > 0) { f = true; } return f; } }
Severlet:
package com.jsp.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.ArrayList; 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 com.jsp.bean.Good_Bean; import com.jsp.bean.Table_Bean; import com.jsp.dao.Good_Dao; import com.jsp.dao.Table_Dao; /** * Servlet implementation class GoodServlet */ @WebServlet("/GoodServlet") public class GoodServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GoodServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charsetUTF-8"); response.setCharacterEncoding("UTF-8"); String action=request.getParameter("action"); if (action.equals("add")) { add( request, response); } else if (action.equals("delete")) { delete( request, response); } else if (action.equals("cha_xun")) { cha_xun( request, response); } else {} } private void cha_xun(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // TODO 自動生成的方法存根 Good_Dao good_dao =new Good_Dao(); List<Good_Bean> list = new ArrayList<>(); request.setAttribute("goods", list); list =good_dao.list(); PrintWriter out= response.getWriter(); out = response.getWriter(); out.println("倉庫詳情"); for(int i=0;i<list.size();i++) { out.print(" "); out.print(list.get(i).getId()+" "); out.print(list.get(i).getChangjia()+" "); out.print(list.get(i).getXinghao()+" "); out.print(list.get(i).getGuige()+" "); out.print(list.get(i).getNumber()+" "); out.println(); out.println(); } //request.getRequestDispatcher("cha_ku.jsp").forward(request,response); } private void delete(HttpServletRequest request, HttpServletResponse response) { // TODO 自動生成的方法存根 Good_Dao good_dao =new Good_Dao(); String name = request.getParameter("name"); String changjia = request.getParameter("changjia"); String xinghao = request.getParameter("xinghao"); String guige = request.getParameter("guige"); if(good_dao.cha_xun(name, xinghao, guige, changjia)) { good_dao.delete(name); } } private void add(HttpServletRequest request, HttpServletResponse response) { // TODO 自動生成的方法存根 Good_Dao good_dao =new Good_Dao(); String name = request.getParameter("name"); String changjia = request.getParameter("changjia"); String xinghao = request.getParameter("xinghao"); String guige = request.getParameter("guige"); String number = request.getParameter("number"); int i_number=Integer.parseInt(number); Good_Bean good_bean= new Good_Bean(name, changjia,xinghao, guige,i_number ); good_dao.add(good_bean); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.jsp.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.ArrayList; 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 com.jsp.bean.Good_Bean; import com.jsp.bean.Table_Bean; import com.jsp.dao.Good_Dao; import com.jsp.dao.Table_Dao; /** * Servlet implementation class TableServlet */ @WebServlet("/TableServlet") public class TableServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TableServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charsetUTF-8"); response.setCharacterEncoding("UTF-8"); String action=request.getParameter("action"); xian_shi(request,response); if (action.equals("in")) { try { ru_ku(request, response); } catch (SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } else if (action.equals("out")) { try { chu_ku(request, response); } catch (SQLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } else {} response.getWriter().append("Served at: ").append(request.getContextPath()); } private void xian_shi(HttpServletRequest request, HttpServletResponse response) throws IOException { // TODO 自動生成的方法存根 Table_Dao table_dao =new Table_Dao(); List<Table_Bean> list = new ArrayList<>(); request.setAttribute("goods", list); list =table_dao.list(); PrintWriter out= response.getWriter(); out = response.getWriter(); out.println("倉庫搬運詳情"); for(int i=0;i<list.size();i++) { out.print(" "); out.print(list.get(i).getId()+" "); out.print(list.get(i).getChangjia()+" "); out.print(list.get(i).getXinghao()+" "); out.print(list.get(i).getGuige()+" "); out.print(list.get(i).getDate()+" "); out.print(list.get(i).getDanwei()+" "); out.print(list.get(i).getRenming()+" "); out.print(list.get(i).getShuliang()+" "); out.print(list.get(i).getFlag()+" "); out.println(); out.println(); } } /** * flag=0是出庫 * @param request * @param response * @throws SQLException */ private void chu_ku(HttpServletRequest request, HttpServletResponse response) throws SQLException { // TODO 自動生成的方法存根 Table_Dao table_dao =new Table_Dao(); String name = request.getParameter("name"); String changjia = request.getParameter("changjia"); String xinghao = request.getParameter("xinghao"); String guige = request.getParameter("guige"); String danwei = request.getParameter("danwei"); String renming = request.getParameter("renming"); String shuliang = request.getParameter("shuliang"); String date = request.getParameter("date"); int i_shuliang=Integer.parseInt(shuliang); int flag =0; Table_Bean table_bean= new Table_Bean(name, changjia,xinghao, guige, date, danwei, renming, i_shuliang, flag ); table_dao._ku(table_bean); } /** * flag=1是入庫 * @param request * @param response * @throws IOException * @throws ServletException * @throws SQLException */ private void ru_ku(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SQLException { // TODO 自動生成的方法存根 Table_Dao table_dao =new Table_Dao(); String name = request.getParameter("name"); String changjia = request.getParameter("changjia"); String xinghao = request.getParameter("xinghao"); String guige = request.getParameter("guige"); String danwei = request.getParameter("danwei"); String renming = request.getParameter("renming"); String shuliang = request.getParameter("shuliang"); String date = request.getParameter("date"); int i_shuliang=Integer.parseInt(shuliang); int flag =1; PrintWriter out= response.getWriter(); out = response.getWriter(); out.println("入庫成功"); Table_Bean table_bean= new Table_Bean(name, changjia,xinghao, guige, date, danwei, renming, i_shuliang, flag ); table_bean.setShuliang(i_shuliang); table_dao._ku(table_bean); out.println(table_bean.getName()); request.getRequestDispatcher("ru_ku.jsp").forward(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
DB:
package com.jsp.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 資料庫的工具類 * * @author 達達超可愛 * */ public final class DButil { /** * 資料庫的連線函式 * 庫名 賬號 密碼 * @return 資料庫的連線 * */ public Connection getConn(String date_Base,String root,String password) { Connection connection =null; // student 為資料庫名稱 ? 後面加的是條件 String db_url1="jdbc:mysql://localhost:3306/"; String db_url2="?serverTimezone=UTC"; String db_url=db_url1+date_Base+db_url2; try { Class.forName("com.mysql.cj.jdbc.Driver"); connection = DriverManager.getConnection(db_url, root, password); // System.out.println("Success connect MySql server!"); } catch (Exception e) { } return connection; } public Connection getConn(String date_Base) { Connection connection =null; // student 為資料庫名稱 ? 後面加的是條件 String db_url1="jdbc:mysql://localhost:3306/"; String db_url2="?serverTimezone=UTC"; String db_url=db_url1+date_Base+db_url2; try { Class.forName("com.mysql.cj.jdbc.Driver"); connection = DriverManager.getConnection(db_url, "root", "123456"); // System.out.println("Success connect MySql server!"); } catch (Exception e) { } return connection; } /** * 資料庫的連線函式 * 可修改的 * * @return 資料庫的連線 */ public Connection getConn() { Connection connection =null; // student 為資料庫名稱 ? 後面加的是條件 String db_url="jdbc:mysql://localhost:3306/ku?serverTimezone=UTC"; try { Class.forName("com.mysql.cj.jdbc.Driver"); connection = DriverManager.getConnection(db_url, "root", "123456"); // System.out.println("Success connect MySql server!"); } catch (Exception e) { } return connection; } public void close(Connection con) { if(con!=null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void close(Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void close(ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
遇到的問題:
1.我上面說的問題
2.如何做到倆個庫裡面數量的關係
3.查詢理解錯誤:我是把所有資訊輸出出來,而不是按照相關資料查詢,我會在之後改正。
4.tomacat8080佔用問題:開啟工作管理員,把正在用的但是不給顯示介面的關掉。
5.時間輸入問題,我是把時間按照String輸入,在網上查了怎樣 把Date改成String。
執行截圖:
顯示全部資料: