用MVC模式寫的jsp串連資料庫操作

來源:互聯網
上載者:User

標籤:mvc

首先用一個JavaBean封裝資料庫操作,即mvc中的模型

JdbcBean.java

package data;import java.sql.*;public class JdbcBean {  private String DBDriver="com.mysql.jdbc.Driver";  private String DBUrl="jdbc:mysql://localhost:3306/test";  private String DBUser="root";  private String DBPassword="686175";  public Connection conn=null;  public ResultSet rs=null;  public ResultSet query(String sql)throws Exception{  try{  Class.forName(DBDriver);  conn=DriverManager.getConnection(DBUrl,DBUser,DBPassword);  Statement stmt=conn.createStatement();  rs=stmt.executeQuery(sql);  return rs;  }  catch(SQLException e){  System.out.println(e.getMessage());  }  return null;  }  public void update(String sql)throws Exception{  try{  Class.forName(DBDriver);  conn=DriverManager.getConnection(DBUrl,DBUser,DBPassword);  Statement stmt=conn.createStatement();  stmt.executeUpdate(sql);  stmt.close();  conn.close();  }  catch(SQLException e){  System.out.println(e.getMessage());  }  }}
在src的data包中還有一個控制器Servlet,用於處理串連資料庫的操作,此次工程用的資料庫是MySQL

LoginServlet.java

package data;import java.io.IOException;import java.sql.ResultSet;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginServlet extends HttpServlet {public void doPost(HttpServletRequest request,           HttpServletResponse response)throws ServletException, IOException {String toJSP="";request.setCharacterEncoding("gbk");response.setContentType("text/html;charset=gbk");String username=request.getParameter("username");String password=request.getParameter("password");String sql="select * from user where username='"+username+"'" +   "&&password='"+password+"'";JdbcBean jdbc=new JdbcBean();try{ResultSet rs=jdbc.query(sql);if(rs.next()){toJSP="success.jsp";}else{toJSP="error.jsp";}rs.close();}catch(Exception e){System.out.println("發生查詢異常");}RequestDispatcher rd=request.getRequestDispatcher(toJSP);rd.forward(request, response);} public void doGet(HttpServletRequest request,           HttpServletResponse response)  throws ServletException, IOException {            doPost(request,response);    }}
接下來就是用jsp寫的視圖,顯示頁面操作

登入頁面login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html>  <head>      <title>My JSP 'login.jsp' starting page</title>    <script>            function myFunction(){               if(myForm.username.value==""){                  alert("使用者名稱不可為空");               }               else if(myForm.password.value==""){                  alert("密碼不可為空");               }               else{                  myForm.submit();               }            }    </script>  </head>  <body>  <form action="LoginServlet" method="post" name="myForm">   <center>  <table>     <tr>        <td>使用者名稱</td>        <td><input type="text" name="username"></td>        <td>使用者名稱不可為空</td>     </tr>     <tr>        <td>密碼</td>        <td><input type="password" name="password"></td>        <td>密碼不可為空</td>     </tr>     <tr>        <td></td>        <td><input type="submit" value="提交" onclick="myFunction()">            <input type="reset" value="取消"></td>     </tr>  </table>  </center>  </form>  </body></html>
登入成功頁面success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html>  <head>      <title>My JSP 'success.jsp' starting page</title>  </head>    <body>    <%       out.println("登入成功!");     %>  </body></html>
如果登入的使用者名稱和密碼和存放在資料庫的資料對不上,就顯示登入錯誤頁面error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html>  <head>      <title>My JSP 'error.jsp' starting page</title>  </head>  <body>    <%       out.println("使用者名稱或密碼錯誤,登入失敗!");     %>  </body></html>

輸入使用者名稱ming,密碼ming


輸入使用者名稱lin,密碼li



完整程式碼封裝:http://download.csdn.net/detail/u011731233/8093745


用MVC模式寫的jsp串連資料庫操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.