1. 程式人生 > >與mysql,判斷能否登錄帳號密碼

與mysql,判斷能否登錄帳號密碼

cte odin har lose script tin this 4.0 result

  1. <%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP ‘check.jsp‘ starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. request.setCharacterEncoding("utf-8");
  23. String users=request.getParameter("username");
  24. String pass=request.getParameter("pwd");
  25. boolean flag=false;
  26. PreparedStatement sql=null;
  27. ResultSet rs=null;
  28. Connection conn=null;
  29. %>
  30. <%
  31. String driver = "com.mysql.jdbc.Driver";
  32. String url = "jdbc:mysql://127.0.0.1:3307/login";
  33. String use = "root";
  34. String password = "960404";
  35. Class.forName(driver);
  36. conn= DriverManager.getConnection(url,use,password);
  37. sql =conn.prepareStatement("select * from student where username=? and password=?");
  38. sql.setString(1,users);
  39. sql.setString(2,pass);
  40. rs=sql.executeQuery();
  41. if (rs.next()) {
  42. flag=true;
  43. }
  44. rs.close();
  45. sql.close();
  46. conn.close();
  47. %>
  48. <!-- 判斷是否是正確的登錄用戶 -->
  49. <% if (flag==true){ %>
  50. <jsp:forward page="show.jsp"/>
  51. <%}
  52. else
  53. if (flag==false){
  54. %>
  55. <jsp:forward page="login_fali.jsp"/>
  56. <%} %>
  57. </body>
  58. </html>

與mysql,判斷能否登錄帳號密碼