10

package dao;

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

public class BaseDao {

    // 获取连接
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.建立连接
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    // 关闭连接
    public 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();
        }
    }

}
package com.jdbc;

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

public class StuDao extends BaseDao {
    public int Register(String uname, String password, int age) {
        int i = -1;
        Connection con = getConnection();
        String sql = "insert into stu(uname,password,age)values(?,?,?)";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            pred.setInt(3, age);
            i = pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
        return i;
    }

    public boolean Login(String uname, String password) {
        boolean f = false;
        Connection con = getConnection();
        String sql = "select * from stu where uname=? and password=?";
        PreparedStatement pred = null;
        ResultSet resultSet = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            resultSet = pred.executeQuery();
            while (resultSet.next()) {
                f = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, pred, resultSet);
        }
        return f;
    }
}
package dao;

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

public class stuDao {

    public static void main(String[] args) {
        stuDao s=new stuDao();
        String sname="张三";
        int pwd=12;
        System.out.println(s.login(sname, pwd));
    }
    public student login(String sname, int pwd) {
        Connection conn = BaseDao.getConnection();
        student s=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String sql = "select * from stu where sname=? and pwd=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, sname);
            ps.setInt(2, pwd);
            rs = ps.executeQuery();
            if (rs.next()) {
                s=new student();
                String name=rs.getString("sname");
                s.setSname(name);
                return s;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(conn, ps, rs);
        }
        return s;
    }

    public int reg(String sname, int pwd, int age) {
        Connection conn = BaseDao.getConnection();
        PreparedStatement ps = null;
        int r = -1;
        try {
            String sql = "insert into stu(sname,pwd,age) values(?,?,?)"; // 2个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, sname);
            ps.setInt(2, pwd);
            ps.setInt(3, age);
            r = ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(conn, ps, null);
        }
        return r;

    }

}
package dao;

public class student {
    String sname;
    int pwd;
    int age;
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public int getPwd() {
        return pwd;
    }
    public void setPwd(int pwd) {
        this.pwd = pwd;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "student [sname=" + sname + ", pwd=" + pwd + ", age=" + age
                + "]";
    }
    
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML>
<html>
<head>
<title>注册</title>
</head>
<body>
    <h1>注册</h1>
    <form action="doregister.jsp" method="post">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname"></td>
            </tr>

            <tr>
                <td>密码</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td>年龄</td>
                <td><input type="number" name="age"></td>
            </tr>
            <tr>
                <td><input type="submit" value="注册"></td>
                <td><input type="reset" value="重置"></td>
            </tr>
        </table>
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>

<title></title>

</head>
<%@ page import="dao.stuDao"%>
<%
    String name = request.getParameter("sname");
    String spwd = request.getParameter("spwd");
    String sname = new String(name.getBytes("ISO-8859-1"), "utf-8");
    try {
        int pwd = Integer.parseInt(spwd);
        stuDao studao = new stuDao();
        if (studao.login(sname, pwd) != null) {
            out.println("登录成功!");
        } else {
            out.println("登陆失败!");
            out.println("三秒后跳转到主页面...");
            response.setHeader("refresh", "3;url=student.jsp");
        }
    } catch (Exception e) {
        out.println("异常!!");
        out.println("三秒后跳转到主页面...");
        response.setHeader("refresh", "3;url=student.jsp");
    }
%>
<body>
    <a href="student.jsp">返回</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");%>

<!DOCTYPE HTML>
<html>
  <head>
    <title></title>
  </head>
  <body>
  <h1>登录成功</h1>
  </body>
</html>

 

posted @ 2022-05-08 10:38  咸鱼大佬  阅读(14)  评论(0编辑  收藏  举报