Pandas——高效的資料處理Python庫
阿新 • • 發佈:2022-05-05
資料庫test 中建個表 stu(stuid 主鍵 自動增長 ,使用者名稱,密碼,年齡)
1.設計一個註冊頁面,實現使用者註冊功能
2.設計一個登陸頁面,實現使用者名稱密碼登陸
3.兩個頁面可以互相超連結
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 request.setCharacterEncoding("utf-8"); 4 response.setCharacterEncoding("utf-8"); 5 %> 6 7 <!DOCTYPE HTML> 8<html> 9 <head> 10 <title>註冊</title> 11 </head> 12 <body> 13 <h1>註冊</h1> 14 <form action="regin.jsp" method="post"> 15 <table> 16 <tr> 17 <td>使用者名稱</td> 18 <td><input type="text" name="uname"></td> 19</tr> 20 21 <tr> 22 <td>密碼</td> 23 <td><input type="password" name="password"></td> 24 </tr> 25 <tr> 26 <td>年齡</td> 27 <td><input type="number" name="age"></td> 28</tr> 29 <tr> 30 <td><input type="submit" value="註冊"></td> 31 <td><input type="reset" value="重置"></td> 32 </tr> 33 </table> 34 </form> 35 </body> 36 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 request.setCharacterEncoding("utf-8"); 4 response.setCharacterEncoding("utf-8"); 5 %> 6 <!DOCTYPE HTML> 7 <html> 8 <head> 9 <title>登入</title> 10 </head> 11 <body> 12 <h1>歡迎</h1> 13 <form action="main.jsp" method="post"> 14 <table> 15 <tr> 16 <td>使用者名稱</td> 17 <td><input type="text" name="uname"></td> 18 </tr> 19 20 <tr> 21 <td>密碼</td> 22 <td><input type="password" name="password"></td> 23 </tr> 24 <tr> 25 <td><input type="submit" value="登入"></td> 26 <td><a href="register.jsp">註冊</a></td> 27 </tr> 28 </table> 29 </form> 30 31 </body> 32 </html>
1 <%@page import="snippet.StuDao"%> 2 <%@page import="javax.xml.bind.ParseConversionEvent"%> 3 <%@page language="java" import="java.util.*" pageEncoding="utf-8"%> 4 <% 5 request.setCharacterEncoding("utf-8"); 6 response.setCharacterEncoding("utf-8"); 7 %> 8 9 <!DOCTYPE HTML> 10 <html> 11 <head> 12 <title></title> 13 </head> 14 <body> 15 <% 16 String uname = request.getParameter("uname"); 17 String password = request.getParameter("password"); 18 String age = request.getParameter("age"); 19 int age1 = age == null ? -1 : Integer.parseInt(age); 20 StuDao sd=new StuDao(); 21 int i=sd.Register(uname, password, age1); 22 if(i>0){ 23 request.getRequestDispatcher("login.jsp").forward(request, response); 24 } 25 %> 26 </body> 27 </html>
1 <%@page import="javax.xml.bind.ParseConversionEvent"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 <% 4 request.setCharacterEncoding("utf-8"); 5 response.setCharacterEncoding("utf-8"); 6 %> 7 8 <!DOCTYPE HTML> 9 <html> 10 <head> 11 <title></title> 12 </head> 13 <body> 14 <% 15 String uname = request.getParameter("uname"); 16 String password = request.getParameter("password"); 17 snippet.StuDao sd=new snippet.StuDao(); 18 if(sd.Login(uname, password)){ 19 request.getRequestDispatcher("index.jsp").forward(request, response); 20 }else{ 21 out.print("登陸失敗,即將跳回登陸頁....."); 22 response.setHeader("refresh", "5;url=login.jsp"); 23 } 24 %> 25 </body> 26 </html>
1 <%@page import="snippet.StuDao"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 <% 4 request.setCharacterEncoding("utf-8"); 5 response.setCharacterEncoding("utf-8");%> 6 7 <!DOCTYPE HTML> 8 <html> 9 <head> 10 <title></title> 11 </head> 12 <body> 13 <h1>登入成功</h1> 14 </body> 15 </html>
1 package snippet; 2 3 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 10 public class BaseDao { 11 //連線資料庫 12 public Connection getConnection() { 13 Connection con = null; 14 try { 15 Class.forName("com.mysql.jdbc.Driver"); 16 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 return con; 21 } 22 //關閉資料庫 23 protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ 24 try { 25 if(rs != null) 26 rs.close(); 27 if(ps != null) 28 ps.close(); 29 if(con != null) 30 con.close(); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 } 35 36 }
1 package snippet; 2 3 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 10 public class StuDao extends BaseDao { 11 public int Register(String uname, String password, int age) { 12 int i = -1; 13 Connection con = getConnection(); 14 String sql = "insert into stu(uname,password,age)values(?,?,?)"; 15 PreparedStatement pred = null; 16 try { 17 pred = con.prepareStatement(sql); 18 pred.setString(1, uname); 19 pred.setString(2, password); 20 pred.setInt(3, age); 21 i = pred.executeUpdate(); 22 } catch (SQLException e) { 23 e.printStackTrace(); 24 } finally { 25 closeAll(con, pred, null); 26 } 27 return i; 28 } 29 30 public boolean Login(String uname, String password) { 31 boolean f = false; 32 Connection con = getConnection(); 33 String sql = "select * from stu where uname=? and password=?"; 34 PreparedStatement pred = null; 35 ResultSet resultSet = null; 36 try { 37 pred = con.prepareStatement(sql); 38 pred.setString(1, uname); 39 pred.setString(2, password); 40 resultSet = pred.executeQuery(); 41 while (resultSet.next()) { 42 f = true; 43 } 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } finally { 47 closeAll(con, pred, resultSet); 48 } 49 return f; 50 } 51 }