JDBC+MySQL+jsp+sevlet登录案例
Posted 丁帅帅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC+MySQL+jsp+sevlet登录案例相关的知识,希望对你有一定的参考价值。
1 <%-- 2 User: 丁帅帅 3 Date: 21/05/27 4 Time: 14:39 5 To change this template use File | Settings | File Templates. 6 --%> 7 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 8 <html> 9 <head> 10 <title>Title</title> 11 </head> 12 <body> 13 <form action="Yanzhen" method="post"> 14 姓名:<input type="text" name="uname"><br> 15 密码:<input type="password" name="pwd"><br> 16 <input type="submit" value="登录"><br> 17 </form> 18 </body> 19 </html>
1 package com.servlet; 2 3 import com.userdao.UserDAO; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import java.io.IOException; 11 12 /** 13 * @Description TODO 14 * @Author 丁帅帅 15 * @Date 21/05/27 14:43 16 * @Version 1.0 17 */ 18 @WebServlet("/Yanzhen") 19 public class Yanzhen extends HttpServlet { 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 22 request.setCharacterEncoding("utf-8"); 23 String uname = request.getParameter("uname"); 24 int pwd = Integer.parseInt(request.getParameter("pwd")); 25 // System.out.println(pwd); 26 request.setAttribute("name",uname); 27 UserDAO userDAO = new UserDAO(); 28 if (userDAO.login(uname,pwd)){ 29 request.getRequestDispatcher("welcome.jsp").forward(request,response); 30 }else{ 31 request.getRequestDispatcher("error.jsp").forward(request,response); 32 } 33 } 34 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 doPost(request, response); 37 } 38 }
1 url=jdbc:mysql:///db3 2 user=root 3 password=root 4 driver=com.mysql.jdbc.Driver 5 6 7 8 package com.ding.util; 9 10 import java.io.FileReader; 11 import java.io.IOException; 12 import java.net.URL; 13 import java.sql.*; 14 import java.util.Properties; 15 16 17 public class JDBCUtil { 18 private static String url; 19 private static String user; 20 private static String password; 21 private static String driver; 22 /** 23 * 件的读取,只需要读取一次即可拿到这些值。使用静态代码块 24 */ 25 static { 26 27 try { 28 //1. 创建Properties集合类。 29 Properties pro = new Properties(); 30 //获取src路径下的文件的方式--->ClassLoader 类加载器 31 ClassLoader classLoader = JDBCUtil.class.getClassLoader(); 32 URL res = classLoader.getResource("jdbc.properties"); 33 String path=res.getPath(); 34 //2. 加载文件 35 pro.load(new FileReader(path)); 36 //3. 获取数据,赋值 37 url=pro.getProperty("url"); 38 user=pro.getProperty("user"); 39 password=pro.getProperty("password"); 40 driver=pro.getProperty("driver"); 41 //4. 注册驱动 42 43 Class.forName(driver); 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } catch (ClassNotFoundException e) { 47 e.printStackTrace(); 48 } 49 50 } 51 52 /** 53 *获取连接 54 * @return 连接对象 55 * @throws SQLException 56 */ 57 public static Connection getConnection() throws SQLException { 58 return DriverManager.getConnection(url,user,password); 59 } 60 61 public static void close(Statement stmt,Connection conn){ 62 if(stmt!=null){ 63 try { 64 stmt.close(); 65 } catch (SQLException e) { 66 e.printStackTrace(); 67 } 68 } 69 if(conn!=null){ 70 try { 71 conn.close(); 72 } catch (SQLException e) { 73 e.printStackTrace(); 74 } 75 } 76 } 77 78 public static void close(ResultSet rs, Statement stmt, Connection conn){ 79 if(rs!=null){ 80 try { 81 rs.close(); 82 } catch (SQLException e) { 83 e.printStackTrace(); 84 } 85 } 86 87 if(stmt!=null){ 88 try { 89 stmt.close(); 90 } catch (SQLException e) { 91 e.printStackTrace(); 92 } 93 } 94 if(conn!=null){ 95 try { 96 conn.close(); 97 } catch (SQLException e) { 98 e.printStackTrace(); 99 } 100 } 101 } 102 103 }
1 package com.userdao; 2 3 import com.ding.util.JDBCUtil; 4 5 import java.sql.Connection; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 10 /** 11 * @Description TODO 12 * @Author 丁帅帅 13 * @Date 21/05/27 14:53 14 * @Version 1.0 15 */ 16 public class UserDAO { 17 private Connection conn=null; 18 private PreparedStatement pstmt=null; 19 private ResultSet rs=null; 20 21 public boolean login(String name,Integer password){ 22 if(name==null||password==null){ 23 24 return false; 25 } 26 try { 27 conn= JDBCUtil.getConnection(); 28 String sql= "select * from user where name=? and password=?"; 29 pstmt=conn.prepareStatement(sql); 30 pstmt.setString(1,name); 31 pstmt.setInt(2,password); 32 rs=pstmt.executeQuery(); 33 return rs.next(); 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 }finally { 37 JDBCUtil.close(rs,pstmt,conn); 38 } 39 return true; 40 } 41 }
<%-- User: 丁帅帅 Date: 21/05/27 Time: 15:16 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>欢迎${requestScope.name}登录成功!!!</h1> </body> </html>
1 <%-- 2 User: 丁帅帅 3 Date: 21/05/27 4 Time: 15:18 5 To change this template use File | Settings | File Templates. 6 --%> 7 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 8 <html> 9 <head> 10 <title>Title</title> 11 </head> 12 <body> 13 <h1>抱歉,密码或用户错误!!!</h1> 14 </body> 15 </html>
所需JAR包/ WEB-INF/lib/mysql-connector-java-5.1.37-bin.jar
以上是关于JDBC+MySQL+jsp+sevlet登录案例的主要内容,如果未能解决你的问题,请参考以下文章
JSP实现登录注册连接MySql数据库-初学(JSP+JDBC实现)
Jsp+Servlet+MYSQL注册登录案例(界面难看,ε=(´ο`*)))唉)