第八次作业

数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)

1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接


package com.db.user;
 
public class User {
     
    private String uname;
    private String upass;
    private int age;
    public User(){}
     
    public User (String uname,String upass,int age){
        this.uname = uname;
        this.upass = upass;
        this.age = age;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpass() {
        return upass;
    }

    public void setUpass(String upass) {
        this.upass = upass;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
     

     
     
 
}

 

package com.db.mydriver;
 
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
public class UserDemo extends DriverDemo{
     
    //注册
        public int AddData(com.db.user.User s){
            int i=0;
            Connection conn=null;  
            PreparedStatement ps = null;
            conn = getConnection();        
            try {
                    String sql = "insert into user(uname,upass,age) values(?,?,?)";
                    ps = conn.prepareStatement(sql);
                    ps.setString(1, s.getUname());
                    ps.setString(2, s.getUpass());
                    ps.setInt(3, s.getAge());
                    i =ps.executeUpdate();             
            }catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                closeAll(conn, ps, null);  
            }
            return i;
        }
         
             
        //登录
        public int dl(com.db.user.User s){
            int i=0;
            Connection conn =null;
            PreparedStatement ps = null;
            ResultSet rs =null;
            try {
                conn = getConnection();
                String sql = "select * from user where uname=? and upass=? and age=?";
                ps =conn.prepareStatement(sql);
                ps.setString(1, s.getUname());
                ps.setString(2, s.getUpass());
                ps.setInt(3, s.getAge());
                rs = ps.executeQuery();
                if(rs.next()){
                    System.out.print("账户登录成功");
                    i=1;
                }else{
                    System.out.print("账户登录失败");
                    i=0;
                }
                 
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                closeAll(conn, ps, rs);
            }  
             
            return i;
        }
}


package com.db.mydriver;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class DriverDemo {

         
        public Connection getConnection(){
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8","root",",root");
            } catch (Exception e) {
                // TODO: handle exception
            }
            return conn;
        }
         
         
        //关闭连接
            public void closeAll(Connection con,PreparedStatement ps,ResultSet rs){    
            try {
                if(rs != null)
                    rs.close();
                if(ps != null)
                    ps.close();
                if(con != null)
                    con.close();
                 
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
             
    }


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
 
<html>
  <head>
    <base href="<%=basePath%>">
     


</head>
<body>
<form action="ZcDemo.jsp" method="post"> 
    <p>
        <span>账号:</span>
        <input type="text" name="zname"   />
    </p>
    <p>
         <span>密码:</span>
         <input type="password" name="zpass" />
    </p>
    <p>
    <span>年龄:</span>
         <input type="number" name="age" />
    </p>
    <p>
        <input type="submit" class="btn01" value="注册"/>
   </p>
</form>
</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.db.user.User" %>
<%@ page import="com.db.mydriver.UserDemo" %>
<%
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 'ZcDemo.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>
    <%
     
        request.setCharacterEncoding("utf-8");
         
        User s = new User();
        UserDemo us = new UserDemo();
         //获取输入的用户名
        String namee = request.getParameter("zname");
        namee.trim();
        s.setUname(namee);
         
         
        String passs = request.getParameter("zpass");  // 密码
        passs.trim();
        s.setUpass(passs);
       

       
        if(us.AddData(s)!=0){
            session.setAttribute("uname",s.getUname());
            response.sendRedirect("Zcshow.jsp");
        }else{
            out.print("注册失败");
        }
         
         
                 
        %>
  </body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
 
<html>
  <head>
    <base href="<%=basePath%>">
     
 
</head>
<body>
<form action="dlDemo.jsp" method="post"> 
    <p>
        <span>账号:</span>
        <input type="text" name="dname" value="admin" class="num"  />
    </p>
    <p>
         <span>密码:</span>
         <input type="password" name="dpass" class="pass"/>
    </p>
    <p>
    <span>年龄:</span>
         <input type="number" name="age" />
    </p>
    <p>
        <input type="submit" class="btn01" value="登录"/>
        <a href="zc.jsp">没有账户?点击注册</a>
   </p>
</form>
</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.db.user.User" %>
<%@ page import="com.db.mydriver.UserDemo" %>
<%
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 'dlDemo.jsp' starting page</title>
     
 
  </head>
   
  <body>
  <%
     request.setCharacterEncoding("utf-8");
         
        User s = new User();
        UserDemo us = new UserDemo();
         
         //获取输入的用户名
        String namees = request.getParameter("dname");
        namees.trim();
        s.setUname(namees);
         
         
        String passss = request.getParameter("dpass");  // 密码
        passss.trim();
        s.setUpass(passss);
         
        if(us.dl(s)!=0){
            session.setAttribute("uname",s.getUname());
            response.sendRedirect("dlshow.jsp");   
            }else{
            response.sendRedirect("index.jsp");

 


        }
         
         
        %>
  </body>
</html>


<%@ 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 'dlshow.jsp' starting page</title>
     
 
  </head>
   
  <body>
   欢迎用户:<%=session.getAttribute("uname")%>登录!!!!
  </body>
</html>

 

posted @ 2022-05-08 04:22  计算机1902张铭麟  阅读(6)  评论(0编辑  收藏  举报