MVC jsp+servlet+javabean 連線Mysql資料庫測試demo
首先我們應該瞭解什麼是MVC:
MVC包含三個部分 :
①View:由各種JSP頁面組成。
②Controller:由各種Servlet組成,Controller就是將View和Model來進行匹配,用什麼Model實現,呼叫哪個View來顯示。
③Model:由各種javabean組成,也是MVC的核心,
以下就是簡單的用MVC架構去實現一個JSP(View)+Servlet(Controller)+javabean(Model)實現MySql查詢功能的Demo。
建立Web Project
1.開啟MyEclipse -> File-> new -> Web Project 建立完後在src下建立包beans和ServletDB最後得到例如以下的檔案結構:
2.編寫beans
在beans包下建立兩個javabeans分別為DAL.java 和 DB.java
DB.java 注意MySqlusername和password是你本機MySql的 username和password與下面的程式碼可能不一樣須要改動。
package beans; import java.sql.*; public class DB { static String driver = "com.mysql.jdbc.Driver"; static String url = "jdbc:mysql://127.0.0.1:3306/test"; static String user = "root";//連線時的username static String password = "root";//連線時的密碼 static Connection aConnection; public static Connection connectDB(){ try{ Class.forName(driver); aConnection = DriverManager.getConnection(url,user,password); if(aConnection.isClosed()) { System.out.print("Succeeded!"); } }catch(ClassNotFoundException e){ System.out.println(e); } catch (SQLException e) { e.printStackTrace(); } return aConnection; } public static ResultSet executeQuery(String sqlx) throws SQLException{ connectDB(); ResultSet rs = null; Statement stmt = aConnection.createStatement(); try { rs = stmt.executeQuery(sqlx); } catch (SQLException e) { e.printStackTrace(); } return rs; } }
DAL.java
package beans; import java.sql.ResultSet; import java.sql.SQLException; public class DAL { public static String sqlx=""; public static String testQueryByID(String ID){ ResultSet rs = null; String result = ""; sqlx = "SELECT * FROM testtable where ID = '"+ID+"'"; try { rs = DB.executeQuery(sqlx); } catch (SQLException e) { e.printStackTrace(); } try { while(rs.next()){ result = result +"ID:"+rs.getString("ID")+" Name:"+rs.getString("name")+"</br>"; } } catch (SQLException e) { e.printStackTrace(); } return result; } }
3.編寫servlet
File-> new -> servlet->確定包名為servletDB然後取名為test即可了
重要的是在本test方法中增加呼叫beans方法的語句。
test.java
package servletDB; 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; import beans.DAL; public class test extends HttpServlet { /** * Constructor of the object. */ public test() { super(); } /** * Destruction of the servlet. <br> */ 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"); String ID = request.getParameter("ID");//獲取傳過來的ID String result = ""; result = DAL.testQueryByID(ID);//呼叫DAL中的testQueryByID方法而且將get過來的ID傳過去 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(result); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(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 POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void init() throws ServletException { // Put your code here } }
4.配置Servlet
進入WebRoot-〉Web-INF -〉web.xml加入例如以下程式碼
<servlet-name>test</servlet-name> <servlet-class>servletDB.test</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/servletDB/test</url-pattern>
寫index.jsp
將body中的程式碼替換為例如以下程式碼
<form action="servletDB/test" method="get"> 請輸入使用者ID:<input type="text" name="ID"><br> <input type="submit" value="提交"> </form>
測試
1.點選Run
2.輸入測試ID 1
成功的獲取到了test資料庫testtable中ID為1的資料。
轉載於MVC jsp+servlet+javabean 連線Mysql資料庫測試demo_weixin_33816300的部落格-CSDN部落格