用MVC模式写的jsp连接数据库操作

首先用一个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


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。