• javaee登录界面


    首先在此之前我们应该正确安装数据库,以及eclipse(javaee)文件包。

    1.首先在javaee中建立新的项目

    2.右键点击WebContent-New-JSP File,新建jsp(动态)文件。(想在哪个文件夹里添加jsp文件,就直接右击文件夹新建,注意一定不要展开文件夹,否则容易出现路径不正确的问题)

    3.进行页面的编写

        login.jsp

        logincheck.jsp

    <%@ page import="java.sql.*" 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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <jsp:useBean id="db" class="Bean.DBBean" scope="page" />
    <%
        request.setCharacterEncoding("UTF-8");
        String username=(String)request.getParameter("username");
        String password=(String)request.getParameter("password");//取出login.jsp的值
        
        //下面是数据库操作
        String sql="select * from Table_1 where name="+"'"+username+"'";//定义一个查询语句
        ResultSet rs=db.executeQuery(sql);//运行上面的语句
        if(rs.next())
        {
            /* if(password.equals(rs.getString(2)))
            {
                
            } */
            if(password.equals(rs.getObject("password"))){
                response.sendRedirect("loginsuccess.jsp");
            }
            else{
                out.print("<script language='javaScript'> alert('密码错误');</script>");
                response.setHeader("refresh", "0;url=login.jsp");
            }
        }
        else 
        {
            out.print("<script language='javaScript'> alert('账号错误——else');</script>");
            response.setHeader("refresh", "0;url=login.jsp");
        }
        
    %>
    </body>
    </html>

    loginsuccess.jsp

    <%@ page import="java.sql.*" 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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>登录成功 </h1>
    </body>
    </html>

    创建DBBean.java,连接数据库

    package Bean;
    import java.sql.*;
    public class DBBean {
    	private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    	private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=z";
    	private String dbusername = "ZH";
    	private String dbpassword = "123";
    	private Connection conn = null;
    	private Statement stmt = null;
    
    	public DBBean()
    	{
    		try
    		{
    			Class.forName(driverStr);
    			conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
    			stmt = conn.createStatement();
    		} 
    		catch (Exception ex) {
    			System.out.println("数据连接失败!");
    		} 
    		
    	}
    
    	public int executeUpdate(String s) {
    		int result = 0;
    		System.out.println("--更新语句:"+s+"
    ");
    		try {
    			result = stmt.executeUpdate(s);
    		} catch (Exception ex) {
    			System.out.println("执行更新错误!");
    		}
    		return result;
    	}
    
    	public ResultSet executeQuery(String s) {
    		ResultSet rs = null;
    		System.out.print("--查询语句:"+s+"
    ");
    		try {
    			rs = stmt.executeQuery(s);
    		} catch (Exception ex) {
    			System.out.println("ִ执行查询错误!");
    		}
    		return rs;
    	}
    	public void execQuery(String s){
    		try {
    			stmt.executeUpdate(s);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			System.out.println("执行插入错误!");
    		}
    	}
    
    	public void close() {
    		try {
    			stmt.close();
    			conn.close();
    		} catch (Exception e) {
    		}
    	}
    }
    

    4.进行程序执行,最后登录成功!

  • 相关阅读:
    第九届蓝桥杯B组决赛-调手表
    第九届蓝桥杯B组决赛-搭积木
    洛谷P2680(树上差分+二分)
    线段树+扫描线+离散化
    hdu3911(线段树区间异或+区间和并+查询最区间大连续1的个数)
    线段树与位运算
    计蒜客Distance on the tree(主席树+LCA)
    洛谷P4742(tarjan缩点+拓扑DP)
    出题人的手环
    SP263 PERIOD
  • 原文地址:https://www.cnblogs.com/zhangjiabei/p/6464147.html
Copyright © 2020-2023  润新知