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+"\n");
		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+"\n");
		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.进行程序执行,最后登录成功!

posted @ 2017-02-27 00:13  蘑菇蘑菇终于开花了  阅读(8425)  评论(1编辑  收藏  举报