zoukankan      html  css  js  c++  java
  • jsp第7次作业

     mysql表

    create table abc(
    no int primary key auto_increment,
    name varchar(20),
    pwd varchar(20)
    );

    login.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="doLogin.jsp" method = "post">
            登录名:<input type = "text" name = "name"/><br/>
            密码:<input type = "password" name = "pwd"/><br/>
            
            <input type = "submit" value = "注册"/><br/>
        </form>
    </body>
    </html>

    weclome.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        注册成功
    </body>
    </html>

    doLogin.jsp

    <%@page import="com.student.dao.LoginDao"%>
    <%@page import="com.school.work.Login"%>
    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
    </head>
    <body>
    <%
    request.setCharacterEncoding("utf-8");
    Login login = new Login();

    String uname = request.getParameter("name");
    String upwd = request.getParameter("pwd");

    login.setName(uname);
    login.setPwd(upwd);
    LoginDao dao = new LoginDao();
    if (dao.register(login) > 0) {
    response.sendRedirect("welcome.jsp");
    }
    %>
    </body>
    </html>

    LoginDao.java

    package com.student.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.school.work.Login;
    
    public class LoginDao {
        private static final String URL = "jdbc:mysql://localhost:3306/jsp";
        private static final String NAME = "root";
        private static final String PASSWORD = "root";
        private static java.sql.PreparedStatement pstmt = null;
        private static Connection connection = null;
    
        // 注册
    
        public int register(Login login) {
            int count = 0;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(URL, NAME, PASSWORD);
                String sql = "insert into abc(name,pwd) values(?,?)";
                pstmt = connection.prepareStatement(sql);
    
                pstmt.setString(1, login.getName());
                pstmt.setString(2, login.getPwd());
                count = pstmt.executeUpdate();
    
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (pstmt != null)
                        pstmt.close();
                    if (connection != null)
                        connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
            return count;
    
        }
    
    }

    Login.java

    package com.school.work;
    
    public class Login {
        private int id;
        private  String name;
        private String pwd;
        
        public Login() {
    
            
        }
    
        
        public Login(String name, String pwd) {
    
            this.name = name;
            this.pwd = pwd;
        }
    
        public Login(int id, String name, String pwd) {
    
            this.id = id;
            this.name = name;
            this.pwd = pwd;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
    }





    
    
    


    
    
    
     
  • 相关阅读:
    SharePoint 2010 User Profile Sync Service自动停止
    如何区别多个svchost.exe?
    Log Parser分析IIS log的一个简单例子
    Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
    Windows中右键点击文件夹, 结果找不到共享选项卡, 怎么办?
    介绍SOS中的SaveModule命令
    SharePoint中Draft版本的文档不会收到document added的Alert Email
    和我一起学Windows Workflow Foundation(1)创建和调试一个WF实例
    门户网站
    C#基础—— check、lock、using语句归纳
  • 原文地址:https://www.cnblogs.com/hyonf/p/14674208.html
Copyright © 2011-2022 走看看