工具:Myeclipse 10,Tomcat 9,MySQL
首先创建数据库Barca,然后建立所需要的用户表user
类型分别是int varchar varchar varchar,flag判断是否有管理员权限。
页面主要包含注册以及登陆,很简单,直接放代码
welcome
欢迎来到巴塞
罗那俱乐部主页
效果图:
由index页面点击注册按钮跳转而来,主要实现注册功能。需要连接数据库,判断用户的合法性。注册成功跳转登陆页面,失败继续停留在此页面,提供返回登陆界面的按钮。
为了访问数据库方便,直接在将访问数据库操作放在JavaBean中,JavaBean放置目录如下:
代码如下:
package JavaBean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQL {
public Connection getConnection() throws SQLException,
InstantiationException, IllegalAccessException,
ClassNotFoundException {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/Barca?useUnicode=true&characterEncoding=utf-8";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
return conn;
}
/*
* 根据传入的SQL语句向数据库查询一条记录
*/
public ResultSet select(String sql) throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs;
} catch (SQLException sqle) {
throw new SQLException(sqle.getMessage());
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/*
* 根据传入的SQL语句向数据库增加一条记录
*/
public void insert(String sql) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception(sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/*
* 根据传入的SQL语句更新数据库记录
*/
public void update(String sql) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception(sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/*
* 根据传入的SQL语句删除一条数据库记录
*/
public void delete(String sql) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception(sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}
注册页面代码:
欢迎注册
验证注册代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'check.jsp' starting page
<%
Connection con = db.getConnection();
int validate = 0;
String name = null,pwd = null;
request.setCharacterEncoding("utf-8");
name = request.getParameter("username");
pwd = request.getParameter("password");
String ad = request.getParameter("age");
try{
Class.forName("com.mysql.jdbc.Driver");
ResultSet rs=null;
String sql = "select * from user where uname = "+"'"+name+"'";
rs = db.select(sql);
if(rs.next())
{
out.print("");
}
else
{
sql = "insert into user(uname,password,flag) values('"+name+"','"+pwd+"','"+ad+"')";
db.insert(sql);
validate++;
}
}catch(Exception e)
{
e.printStackTrace();
}
if(validate > 0)
{
response.sendRedirect("index.html");
}
else{
out.println("Sorry !出现异常");
}
%>
效果图
注册成功,信息写进数据库,完成登陆功能,将密码跟账号在数据库中进行比对,验证是否可以登陆。登陆成功,跳转主界面。
验证代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'check.jsp' starting page
<%
Connection con = db.getConnection();
int validate = 0;
String name = null,pwd = null;
request.setCharacterEncoding("utf-8");
name = request.getParameter("username");
session.setAttribute("name",name);
pwd = request.getParameter("password");
session.setAttribute("password",pwd);
try{
Class.forName("com.mysql.jdbc.Driver");
String sql = "select * from user where uname = "+"'"+name+"'";
ResultSet rs = db.select(sql);
while(rs.next())
{
String un = rs.getString(2);
String up = rs.getString(3);
if((un.equals(name)) && (up.equals(pwd))){%>
<% }
else
{%>
<%}
}%>
<%}catch(Exception e)
{
e.printStackTrace();
}
%>
以上,注册登录完成。