用Eclipse连接MySql数据库实现登陆的功能。
功能分析
1.MySql数据库的连接
2.判断输入的内容是否为空
3.判断输入的内容是否与数据库的内容相同
4.重定向的使用
效果演示
原始样式
当账户或者密码任何一个是空的时候点击登录会弹框
一 MySql数据库
我的数据库名school
我的数据表名login
二 程序详解
在开始我们的程序之前一定注意:
导入mysql-connector-java-5.1.39-bin.jar架包和jquery.jar架包。
我们有必要先看一下目录结构
代码展示
LoginServlet.java
package com.zsh.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.zsh.util.DBUtil;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");String name = request.getParameter("name");String pwd = request.getParameter("pwd");boolean flag = false;Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from login where name = ? and pwd = ?";try {ps = conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, pwd);rs = ps.executeQuery();if(rs.next()){flag = true;}else{flag = false;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}if(flag){response.sendRedirect("success.jsp");}else{response.sendRedirect("login.jsp");}}}
DBUtil.java
package com.zsh.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class DBUtil {public static Connection getConn(){Connection conn = null;String url = "jdbc:mysql://localhost:3306/school";String user = "root";String pwd = "111";try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url, user, pwd);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void closeConn(Connection conn, PreparedStatement ps, ResultSet rs){if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(rs!=null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
JSP页面
login.jsp
<%@ 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><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script src="jquery.js"></script></head><style>#all{width: 300px;height: 200px;margin-top: 100px;}input{margin-top: 20px;}</style><body><center><div id="all"><form action="login" method="post">账号:<input type="text" name="name" id="name"><br>密码:<input type="password" name="pwd" id="pwd"><br><input type="submit" value="登录"></form></div></center></body><script>$("form").submit(function(){var name = $("#name").val();var pwd = $("#pwd").val();var nameFlag = false;var pwdFlag = false;if(name == "" || name == "undefined"){nameFlag = false;alert("账户不能为空!");}else{nameFlag = true;}if(pwd == "" || pwd == "undefined"){pwdFlag = false;alert("密码不能为空!");}else{pwdFlag = true;}if(nameFlag == false || pwdFlag == false){return false;}else{return true;}})</script>
</html>
success.jsp
<%@ 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 'success.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>欢迎成功登录!!!</h1></body>
</html>
了解更多关注我呦!!!