第十周作业

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

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

主页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
    <form action="sLog.jsp">
                 用户名:<input type="text" name="sname" /> 密码:<input type="password"
            name="spas" /> 年龄:<input type="text" name="sage" />
            <input type="submit" value="登录" />
    </form>
    <form action="sLogon.jsp">
        <input type="submit" value="注册" />
    </form>
  </body>
</html>
登陆
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="stu.student" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
        <%
        String sname = request.getParameter("sname");
        String spas = request.getParameter("spas");
        try {
            if (sname!=null&&spas!=null) {
                out.println("登录成功!");
            } else {
                out.println("登陆失败!");
                response.setHeader("refresh", "2;url=main.jsp");
            }
        } catch (Exception e) {
            out.println("异常!!");
            response.setHeader("refresh", "2;url=main.jsp");
        }
      %>
      返回主页面
  </body>
</html>
注册
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
    
  </head>
  
  <body>
    <form action="logontest.jsp">
           姓名:<input type="text" name="sname" /> 密码:<input type="password"
            name="spas" /> 年龄:<input type="text" name="sage" /> <input
            type="submit" value="注册" />
    </form>
  </body>
</html>

logontest.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="stu.student"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
    <%
    String name = request.getParameter("sname");
    String pas = request.getParameter("spas");
    String age = request.getParameter("sage");
    if(name!=null&&pas!=null){
        out.println("注册成功!");
        response.setHeader("refresh", "5;url=main.jsp");
    }else {
        out.println("注册失败!");
        response.setHeader("refresh", "5;url=main.jsp");
    }
    %>
    返回主页面
  </body>
</html>
public class student {

    String sname;
    String spas;
    int sage;
    public student(String name, String pas, int age) {
        super();
        sname = name;
        this.spas = pas;
        sage = age;
    }
    public String getName() {
        return sname;
    }
    public void setName(String name) {
        sname = name;
    }
    public String getPas() {
        return spas;
    }
    public void setPas(String pas) {
        this.spas = pas;
    }
    public int getAge() {
        return sage;
    }
    public void setAge(int age) {
        sage = age;
    }
package stu;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class stuj extends stuTest{

    /**
     * @param args
     * 
     */
    public boolean login(String Name, String pas) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where uname=? and upwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, Name);
            ps.setString(2, pas);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }
    public static void main(String Name, String pas,int Age) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        Scanner sc=new Scanner(System.in);
        try {
            String sql = "insert into student(sname,spas,sage) values(?,?,?)"; 
            // 4.执行SQL语句
            System.out.println("请输名");
            String sname=sc.next();
            System.out.println("请输入密码");
            String spas=sc.next();
            System.out.println("请输入年龄");
            int sage=sc.nextInt();
            ps = conn.prepareStatement(sql);
            ps.setString(1, sname);
            ps.setString(2, spas);
            ps.setInt(3, sage);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }
}
package stu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class stuTest {

    /**
     * @param args
     */
    protected static Connection getConnection(){
        Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 2.建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test", "root", "jth12345");
            } catch (Exception e) {
                e.printStackTrace();
            } 
            return conn;
    }    
    //关闭连接
    protected static 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();
            }
        }

}

 

posted @ 2022-05-07 23:04  五谷鸡爪🥳  阅读(8)  评论(0编辑  收藏  举报