简单的连接数据库的Web登录界面
一、需求分析
实现在登录界面输入用户名和密码,连接数据库,与数据库信息进行比对,若用户名和密码相互匹配,则显示登陆成功,若不正确,选择重新输入。
二、工具
1.MySql 2.Tomcat 3.Java EE 4.SQLyog或Navicat等,并配置好环境变量。
三、实现步骤
1.数据库建表
首先,创建数据库,并在数据库中创建数据表,表格如图所示:
2.新建项目
打开Eclipse Jee,File–new–Dynamic Web Project.
3.创建JSP、Class文件
1、新建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>
<center>
<h1 style="color:blue">登录</h1>
<form id="indexform" name="indexForm" action="check.jsp" method="post">
<table border="0">
<tr>
<td>账号:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
</table>
<br>
<input type="submit" value="登录">
</form>
</center>
</body>
</html>
2、新建check.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=utf-8">
<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");//获取login页面输入的用户名和密码
String password=(String)request.getParameter("password");
String sql="select * from users where username="+"'"+username+"'";//定义一个查询语句
ResultSet rs=db.executeQuery(sql);//执行查询语句
if(rs.next())
{
//将输入的密码与数据库密码相比对,执行相应操作
if(password.equals(rs.getObject("password"))){
response.sendRedirect("success.jsp");
}
else{
out.print("<script language='javaScript'> alert('密码错误');</script>");
response.setHeader("refresh", "0;url=login.jsp");
}
}
else
{
out.print("<script language='javaScript'> alert('用户名错误,请重新输入');</script>");
response.setHeader("refresh", "0;url=login.jsp");
}
%>
</body>
</html>
3、新建success.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body style="text-align:center">
<h1>登录成功</h1>
</body>
</html>
4、创建DBBean.java文件
在该项目下,先创建名为Bean的package文件,然后创建DBBean.java文件:
package Bean;
import java.sql.*;
public class DBBean {
private String driverStr = "com.mysql.jdbc.Driver";
private String connStr = "jdbc:mysql://localhost:3306/test";
private String dbusername = "root";
private String dbpassword = "root";
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(ex.getMessage());
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) {
System.out.println("执行插入错误!");
}
}
public void close() {
try {
stmt.close();
conn.close();
} catch (Exception e) {
}
}
}
5、导入Mysql包
将mysql-connector-java-5.1.7-bin.jar拷贝到tomcat下lib目录即可。