Java+MyEclipse+Tomcat (四)Servlet提交表单和数据库操作
生活随笔
收集整理的這篇文章主要介紹了
Java+MyEclipse+Tomcat (四)Servlet提交表单和数据库操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ? ? 前面三篇文章講述了如何配置MyEclipse和Tomcat開發(fā)JSP網(wǎng)站、如何配置Servlet簡單實現(xiàn)表單提交、如何配置MySQL實現(xiàn)JSP數(shù)據(jù)庫查詢。
? ? ? ? 這篇文章主要講述Servlet表單的提交、Java中實現(xiàn)數(shù)據(jù)庫的查詢操作和自己遇到的瓶頸及理解。Java Web基礎(chǔ)性文章,希望對大家有所幫助~
? ? ? ??Java+MyEclipse+Tomcat (一)配置過程及jsp網(wǎng)站開發(fā)入門
? ? ? ??Java+MyEclipse+Tomcat (二)配置Servlet及簡單實現(xiàn)表單提交
? ? ? ??Java+MyEclipse+Tomcat (三)配置MySQL及查詢數(shù)據(jù)顯示在JSP網(wǎng)頁中
? ? ? ? 兩個項目的免費下載地址(希望對你有所幫助):
? ? ? ??http://download.csdn.net/detail/eastmount/8701707
? ? ? ? 核心代碼:
? ? ? ??<form action="/TestServlet01/servlet/PostServlet" method="POST">
? ? ? ? ? ? 出發(fā)地:<input type="text" id="start" name="start" style='font-size:18px'/>
? ? ? ? ? ??<input type="submit" name="Select" value="提交信息"/>
? ? ? ? </form>
? ? ? ? 然后再src中右鍵添加Package,包名為servlet;再添加Servlet文件,文件名PostServlet.java。選擇圖標(biāo)。前面文章講述過Servlet的手動配置過程,包括servlet類、映射等,現(xiàn)在它自動生成的WebRoot/WEB-INF/web.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>PostServlet</servlet-name><servlet-class>servlet.PostServlet</servlet-class></servlet><servlet-mapping><servlet-name>PostServlet</servlet-name><url-pattern>/servlet/PostServlet</url-pattern></servlet-mapping> <welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>? ? ? ? 同時修改src/servlet/PostServlet.java文件,采用POST方法顯示表單數(shù)據(jù):
package servlet;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class PostServlet extends HttpServlet {public PostServlet() {super();}public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setCharacterEncoding("UTF-8"); //設(shè)置輸出編碼request.setCharacterEncoding("UTF-8");String startName = request.getParameter("start"); //獲取出發(fā)地String endName = request.getParameter("end"); //獲取到達地String sex = request.getParameter("sex"); //獲取性別String [] interest = request.getParameterValues("interesting"); //獲取興趣String seat = request.getParameter("seat"); //獲取座位String info = request.getParameter("description"); //獲取備注信息response.setContentType("text/html"); //設(shè)置輸出類型PrintWriter out = response.getWriter(); //獲取out對象out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println("<H2>出發(fā)地:"+ startName +"</H2>");out.println("<H2>到達地:"+ endName +"</H2>");out.println("<H2>性別:"+ sex +"</H2>");out.println("<H2>興趣");for(String str:interest) {out.println(str+" ");}out.println("</H2><H2>座位類型:"+ seat +"</H2>");out.println("<H2>備注信息:"+ info +"</H2>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}? ? ? ? 運行效果如下圖所示:
? ? ? ??
<%@ 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><style>body, div, td, input {font-size:18px; margin:0px; }.line {margin:2px; }</style></head><body><form action="/TestServlet01/servlet/PostServlet" method="POST"><div align="center"><br/><fieldset style='width:60%'><legend>填寫用戶信息</legend><br/><div class='line'><div align="left">出發(fā)地:<input type="text" id="start" name="start" style='font-size:18px' width=200/></div></div><div class='line'><div align="left"><br/><input type="submit" name="Select" value="提交信息" style='font-size:18px'/><br/></div></div></fieldset> </div> </form> </body> </html>? ? ? ? 修改的PostServlet.java代碼如下: package servlet;import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class PostServlet extends HttpServlet {//自定義變量private Connection connection = null; //定義數(shù)據(jù)庫連接對象private String driverName = "com.mysql.jdbc.Driver"; //數(shù)據(jù)庫驅(qū)動器private String userName = "root"; //數(shù)據(jù)庫用戶名private String userPasswd = "123456"; //密碼private String dbName = "test01"; //數(shù)據(jù)庫名稱private String tableName = "Train"; //表明//連接字符串 數(shù)據(jù)庫地址URL MySQL數(shù)據(jù)庫端口3306private String url = "jdbc:mysql://localhost:3306/" + dbName + "?user=" + userName + "&password=" + userPasswd;//初始化方法public void init(ServletConfig config) throws ServletException{super.init(config);}public PostServlet() {super();}//處理GET請求方法public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{response.setCharacterEncoding("UTF-8"); //設(shè)置輸出編碼request.setCharacterEncoding("UTF-8");response.setContentType("text/html"); //設(shè)置輸出類型PrintWriter out = response.getWriter(); //獲取out對象try {//數(shù)據(jù)庫操作Class.forName(driverName).newInstance();connection = DriverManager.getConnection(url);Statement statement = connection.createStatement();String startName = request.getParameter("start"); //獲取出發(fā)地//注意:startName需要加單引號 否則報錯 ——錯誤:Unknown column 'BeiJing' in 'where clause'String sql = "SELECT * FROM " + tableName +" WHERE startname='" + startName+"';";if(startName=="") {sql = "SELECT * FROM " + tableName;}ResultSet rs = statement.executeQuery(sql); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println(" <fieldset style='width:60%' ><legend>搜索結(jié)果</legend><br />");out.println(" <TABLE align='center'border='1' cellspacing='1' cellpadding='1'>");out.println(" <TR><TH>車號</TH><TH>出發(fā)地</TH><TH>到達地</TH></TR>");//循環(huán)輸出查詢結(jié)果while(rs.next()) {out.println(" <TR><TD>" + rs.getString(1) + "</TD>");out.println(" <TD>" + rs.getString(2) + "</TD>");out.println(" <TD>" + rs.getString(3) + "</TD></TR>");}out.println(" </TABLE>");out.println(" </fieldset>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();rs.close(); // 關(guān)閉記錄集statement.close(); // 關(guān)閉聲明} catch(Exception e) {System.out.println("錯誤:"+e.getMessage());response.sendRedirect("index.jsp");}}//處理POST請求方法public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { doGet(request,response);}//銷毀方法public void destroy() {super.destroy(); // Just puts "destroy" string in logtry {connection.close(); // 關(guān)閉連接對象}catch(Exception e) {System.out.println("關(guān)閉數(shù)據(jù)庫錯誤:"+e.getMessage());}} }? ? ? ? 同時WebRoot/WEB-INF/web.xml文件Servlet映射都沒有變化,需要在WebRoot/WEB-INF/lib中添加mysql-connector-java-5.1.15-bin.jar,否則會報錯“com.mysql.jdbc.Driver錯誤”。
? ? ? ? 運行效果如下圖所示:
? ? ? ? 寫到此處我產(chǎn)生了一個疑問,當(dāng)表單提交信息時,獲取數(shù)據(jù)庫的結(jié)果有兩種方法:
? ? ? ? 1.第一種是上一篇博客中寫到的,在JSP中通過<% ....%>調(diào)用Java代碼實現(xiàn)連接數(shù)據(jù)庫,獲取MySQL表中數(shù)據(jù)并顯示;
? ? ? ? 2.第二種就是這篇博客中寫到的,在JSP中通過Post方法提交表單Form,在Java中通過Servlet獲取請求/響應(yīng),再通過Java中out.println("<HTML>...")輸出數(shù)據(jù)庫中值。
? ? ? ? 就這兩種方法而言,我想實現(xiàn)的功能是:JSP就賦值布局,顯示界面;Java就負(fù)責(zé)連接數(shù)據(jù)庫、數(shù)據(jù)庫增刪改查,處理結(jié)果再返回給JSP中顯示,而不是相互嵌套的。換句話說:JSP中點擊“提交”按鈕,TextBox中傳遞出發(fā)地,Java中介紹請求,數(shù)據(jù)庫查詢,得到的結(jié)果再返回給JSP中顯示。
? ? ? ? 那怎么實現(xiàn)呢?后面的文章可能會講到。
? ? ? ? DAO和Java Bean是對JDBC進行分層、模塊化的最有效兩個方法。DAO(數(shù)據(jù)庫操作對象,Database Access Object)是JDBC下常用模式,DAO出現(xiàn)之前,操作數(shù)據(jù)庫的代碼與業(yè)務(wù)代碼都出現(xiàn)在Servlet或者JSP中,不利用業(yè)務(wù)代碼的分離。DAO出現(xiàn)后,所有與數(shù)據(jù)庫相關(guān)的操作全被拿到了DAO層實現(xiàn),Servlet或JSP只操作Java Bean或者DAP層,而DAO層值操作數(shù)據(jù)庫。
? ? ? ? PS:非常高興我自己通過實際項目找到了這個難點,然后又找到了解決方法。雖然才學(xué)習(xí)Java Web一周時間,還是學(xué)到很多東西的。個人感覺DAO類似于中間件的東西吧!最后希望文章對你有所幫助,這篇文章是講述Servlet連接MySQL數(shù)據(jù)庫及表單交互之間的知識。如果文章有不足或錯誤的地方,還請海涵!下一篇文章講講Session和一個典型簡單的界面布局等相關(guān)知識吧!
? ? ? ??(By:Eastmount 2015-5-15 半夜1點 ??http://blog.csdn.net/eastmount/)
? ? ? ? 這篇文章主要講述Servlet表單的提交、Java中實現(xiàn)數(shù)據(jù)庫的查詢操作和自己遇到的瓶頸及理解。Java Web基礎(chǔ)性文章,希望對大家有所幫助~
? ? ? ??Java+MyEclipse+Tomcat (一)配置過程及jsp網(wǎng)站開發(fā)入門
? ? ? ??Java+MyEclipse+Tomcat (二)配置Servlet及簡單實現(xiàn)表單提交
? ? ? ??Java+MyEclipse+Tomcat (三)配置MySQL及查詢數(shù)據(jù)顯示在JSP網(wǎng)頁中
? ? ? ? 兩個項目的免費下載地址(希望對你有所幫助):
? ? ? ??http://download.csdn.net/detail/eastmount/8701707
一. Servlet表單提交
? ? ? ? 新建Web Project,項目名稱為TestServlet01。項目結(jié)構(gòu)如下圖所示:
? ? ? ? 核心代碼:
? ? ? ??<form action="/TestServlet01/servlet/PostServlet" method="POST">
? ? ? ? ? ? 出發(fā)地:<input type="text" id="start" name="start" style='font-size:18px'/>
? ? ? ? ? ??<input type="submit" name="Select" value="提交信息"/>
? ? ? ? </form>
? ? ? ? 然后再src中右鍵添加Package,包名為servlet;再添加Servlet文件,文件名PostServlet.java。選擇圖標(biāo)。前面文章講述過Servlet的手動配置過程,包括servlet類、映射等,現(xiàn)在它自動生成的WebRoot/WEB-INF/web.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>PostServlet</servlet-name><servlet-class>servlet.PostServlet</servlet-class></servlet><servlet-mapping><servlet-name>PostServlet</servlet-name><url-pattern>/servlet/PostServlet</url-pattern></servlet-mapping> <welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>? ? ? ? 同時修改src/servlet/PostServlet.java文件,采用POST方法顯示表單數(shù)據(jù):
package servlet;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class PostServlet extends HttpServlet {public PostServlet() {super();}public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setCharacterEncoding("UTF-8"); //設(shè)置輸出編碼request.setCharacterEncoding("UTF-8");String startName = request.getParameter("start"); //獲取出發(fā)地String endName = request.getParameter("end"); //獲取到達地String sex = request.getParameter("sex"); //獲取性別String [] interest = request.getParameterValues("interesting"); //獲取興趣String seat = request.getParameter("seat"); //獲取座位String info = request.getParameter("description"); //獲取備注信息response.setContentType("text/html"); //設(shè)置輸出類型PrintWriter out = response.getWriter(); //獲取out對象out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println("<H2>出發(fā)地:"+ startName +"</H2>");out.println("<H2>到達地:"+ endName +"</H2>");out.println("<H2>性別:"+ sex +"</H2>");out.println("<H2>興趣");for(String str:interest) {out.println(str+" ");}out.println("</H2><H2>座位類型:"+ seat +"</H2>");out.println("<H2>備注信息:"+ info +"</H2>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}? ? ? ? 運行效果如下圖所示:
? ? ? ??
二. Servlet數(shù)據(jù)庫查詢
? ? ? ? 還是使用上面的項目進行修改,實現(xiàn)Servlet數(shù)據(jù)庫查詢操作。數(shù)據(jù)庫配置可以參照上一篇博客配置MySQL的過程,我新建數(shù)據(jù)庫test01,插入表Train,表中數(shù)據(jù)如下圖:
<%@ 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><style>body, div, td, input {font-size:18px; margin:0px; }.line {margin:2px; }</style></head><body><form action="/TestServlet01/servlet/PostServlet" method="POST"><div align="center"><br/><fieldset style='width:60%'><legend>填寫用戶信息</legend><br/><div class='line'><div align="left">出發(fā)地:<input type="text" id="start" name="start" style='font-size:18px' width=200/></div></div><div class='line'><div align="left"><br/><input type="submit" name="Select" value="提交信息" style='font-size:18px'/><br/></div></div></fieldset> </div> </form> </body> </html>? ? ? ? 修改的PostServlet.java代碼如下: package servlet;import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class PostServlet extends HttpServlet {//自定義變量private Connection connection = null; //定義數(shù)據(jù)庫連接對象private String driverName = "com.mysql.jdbc.Driver"; //數(shù)據(jù)庫驅(qū)動器private String userName = "root"; //數(shù)據(jù)庫用戶名private String userPasswd = "123456"; //密碼private String dbName = "test01"; //數(shù)據(jù)庫名稱private String tableName = "Train"; //表明//連接字符串 數(shù)據(jù)庫地址URL MySQL數(shù)據(jù)庫端口3306private String url = "jdbc:mysql://localhost:3306/" + dbName + "?user=" + userName + "&password=" + userPasswd;//初始化方法public void init(ServletConfig config) throws ServletException{super.init(config);}public PostServlet() {super();}//處理GET請求方法public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{response.setCharacterEncoding("UTF-8"); //設(shè)置輸出編碼request.setCharacterEncoding("UTF-8");response.setContentType("text/html"); //設(shè)置輸出類型PrintWriter out = response.getWriter(); //獲取out對象try {//數(shù)據(jù)庫操作Class.forName(driverName).newInstance();connection = DriverManager.getConnection(url);Statement statement = connection.createStatement();String startName = request.getParameter("start"); //獲取出發(fā)地//注意:startName需要加單引號 否則報錯 ——錯誤:Unknown column 'BeiJing' in 'where clause'String sql = "SELECT * FROM " + tableName +" WHERE startname='" + startName+"';";if(startName=="") {sql = "SELECT * FROM " + tableName;}ResultSet rs = statement.executeQuery(sql); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println(" <fieldset style='width:60%' ><legend>搜索結(jié)果</legend><br />");out.println(" <TABLE align='center'border='1' cellspacing='1' cellpadding='1'>");out.println(" <TR><TH>車號</TH><TH>出發(fā)地</TH><TH>到達地</TH></TR>");//循環(huán)輸出查詢結(jié)果while(rs.next()) {out.println(" <TR><TD>" + rs.getString(1) + "</TD>");out.println(" <TD>" + rs.getString(2) + "</TD>");out.println(" <TD>" + rs.getString(3) + "</TD></TR>");}out.println(" </TABLE>");out.println(" </fieldset>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();rs.close(); // 關(guān)閉記錄集statement.close(); // 關(guān)閉聲明} catch(Exception e) {System.out.println("錯誤:"+e.getMessage());response.sendRedirect("index.jsp");}}//處理POST請求方法public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { doGet(request,response);}//銷毀方法public void destroy() {super.destroy(); // Just puts "destroy" string in logtry {connection.close(); // 關(guān)閉連接對象}catch(Exception e) {System.out.println("關(guān)閉數(shù)據(jù)庫錯誤:"+e.getMessage());}} }? ? ? ? 同時WebRoot/WEB-INF/web.xml文件Servlet映射都沒有變化,需要在WebRoot/WEB-INF/lib中添加mysql-connector-java-5.1.15-bin.jar,否則會報錯“com.mysql.jdbc.Driver錯誤”。
? ? ? ? 運行效果如下圖所示:
? ? ? ? 寫到此處我產(chǎn)生了一個疑問,當(dāng)表單提交信息時,獲取數(shù)據(jù)庫的結(jié)果有兩種方法:
? ? ? ? 1.第一種是上一篇博客中寫到的,在JSP中通過<% ....%>調(diào)用Java代碼實現(xiàn)連接數(shù)據(jù)庫,獲取MySQL表中數(shù)據(jù)并顯示;
? ? ? ? 2.第二種就是這篇博客中寫到的,在JSP中通過Post方法提交表單Form,在Java中通過Servlet獲取請求/響應(yīng),再通過Java中out.println("<HTML>...")輸出數(shù)據(jù)庫中值。
? ? ? ? 就這兩種方法而言,我想實現(xiàn)的功能是:JSP就賦值布局,顯示界面;Java就負(fù)責(zé)連接數(shù)據(jù)庫、數(shù)據(jù)庫增刪改查,處理結(jié)果再返回給JSP中顯示,而不是相互嵌套的。換句話說:JSP中點擊“提交”按鈕,TextBox中傳遞出發(fā)地,Java中介紹請求,數(shù)據(jù)庫查詢,得到的結(jié)果再返回給JSP中顯示。
? ? ? ? 那怎么實現(xiàn)呢?后面的文章可能會講到。
? ? ? ? DAO和Java Bean是對JDBC進行分層、模塊化的最有效兩個方法。DAO(數(shù)據(jù)庫操作對象,Database Access Object)是JDBC下常用模式,DAO出現(xiàn)之前,操作數(shù)據(jù)庫的代碼與業(yè)務(wù)代碼都出現(xiàn)在Servlet或者JSP中,不利用業(yè)務(wù)代碼的分離。DAO出現(xiàn)后,所有與數(shù)據(jù)庫相關(guān)的操作全被拿到了DAO層實現(xiàn),Servlet或JSP只操作Java Bean或者DAP層,而DAO層值操作數(shù)據(jù)庫。
? ? ? ? PS:非常高興我自己通過實際項目找到了這個難點,然后又找到了解決方法。雖然才學(xué)習(xí)Java Web一周時間,還是學(xué)到很多東西的。個人感覺DAO類似于中間件的東西吧!最后希望文章對你有所幫助,這篇文章是講述Servlet連接MySQL數(shù)據(jù)庫及表單交互之間的知識。如果文章有不足或錯誤的地方,還請海涵!下一篇文章講講Session和一個典型簡單的界面布局等相關(guān)知識吧!
? ? ? ??(By:Eastmount 2015-5-15 半夜1點 ??http://blog.csdn.net/eastmount/)
總結(jié)
以上是生活随笔為你收集整理的Java+MyEclipse+Tomcat (四)Servlet提交表单和数据库操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java+MyEclipse+Tomca
- 下一篇: Java+MyEclipse+Tomca