資源連接:javascript
連接:https://pan.baidu.com/s/1vl7HZ2TJnt3_bbWfjch7xg
提取碼:irey (PS:由於文件有覆蓋,因此在該隨筆以前的連接可能失效)css
界面:html
mysql截圖:java
出現的問題v1.0:鏈接數據庫的時候com.mysql加載出錯:mysql
解決方法:sql
導入mysql-connector-java-8.0.15.jar包點擊確認就ok了數據庫
出現的問題v1.1:鏈接數據庫的時候出現異常url
以下:spa
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone
解決辦法:code
在鏈接數據庫的語句中添加「serverTimezone=UTC」
html代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>登陸界面</title> <script type="text/javascript" src="script/login.js"></script> <link href="css/login.css" type="text/css" rel="stylesheet"> </head> <body> <div id="page"> <div id="page_head"> <div id="logo"> <img src="rsc\login_logo.png"/> </div> </div> <div id="page_body"> <div id="login"> <div id="loginTitle"> <b>帳號登陸</b> </div> <form action="TestServlet" id="loginForm"> <div id="tip">請填寫用戶名</div> <div class="textitem_count"> <input style="width:320px;height:30px" name="input_user" type="text" placeholder="用戶名"> </div> <div class="textitem_pwd"> <input style="width:320px;height:30px" name="input_password" type="password" placeholder="密碼"> </div> <div style="position:absolute;left:30px;top:220px;width:320px"> <sqan style="color:red;float:left;">學生選擇@stu.swpu.edu.cn</sqan> <a href="#" style="float:right;">忘記密碼</a> </div> <div style="position: absolute; left:30px; top:260px;width: 320px"> <input onclick="fnLogin()" style="float:right;background:url(rsc/login_btn.jpg);" class="btn" type="submit" value="登 錄" /> </div> </form> </div> </div> <div id="page_foot">西南石油大學</div> </div> </body> </html>
servlet代碼:
package swpu.com; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; String sql = null; Connection conn = null; Statement stmt = null; ResultSet rs = null; boolean isLoing=false; /** * @see HttpServlet#HttpServlet(g) */ public TestServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String userName = request.getParameter("input_user"); String userPsw = request.getParameter("input_password"); //鏈接數據庫檢測用戶名和密碼 try { //鏈接數據庫 Class.forName("com.mysql.jdbc.Driver"); DriverManager.registerDriver(new com.mysql.jdbc.Driver()); conn=DriverManager.getConnection("jdbc:mysql://localhost/workdb?user=root&password=root&serverTimezone=UTC"); stmt=conn.createStatement(); sql="select userPWD from login where userID='"+userName+"'"; rs=stmt.executeQuery(sql); if(rs.next()) { //獲取輸入用戶名的密碼進行檢驗,若與輸入的一致則isLogin置爲true,反之置false String pw=rs.getString("userPWD"); if (pw.equals(userPsw)) { isLoing=true; }else { isLoing=false; } }else { //若未查詢到用戶的存在也置爲false isLoing=false; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { try { if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(conn!=null) conn.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } if(isLoing){ response.getWriter().write("登錄成功"); response.getWriter().write("</br>"); response.getWriter().write("用戶名:" + userName); response.getWriter().write("</br>"); response.getWriter().write("密碼:" + userPsw); } else{ response.getWriter().write("登陸失敗!"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }