首页 > 基础资料 博客日记
jsp登录注册代码(增删改查+网页+数据库)
2023-07-24 10:54:06基础资料围观285次
文章jsp登录注册代码(增删改查+网页+数据库)分享给大家,欢迎收藏Java资料网,专注分享技术知识
目录
一·登录注册代码以及效果
register.jsp:注册
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
<style>
#a {
width:50%;
height:200px;
border: 1px dashed ;
background-color:lightyellow;
text-align:center;
}
body{
background-color:lightblue;
}
</style>
</head>
<body>
<form action="doregister.jsp" method="post">
用户名:<input type="text" name="uname"><br>
密码:<input type="password" name="upwd"><br>
备注:<textarea rows="" cols="" name="uinfo">
</textarea><br>
<input type="submit" value="注册">
<input type="reset" value="重置">
</form>
</body>
</html>
doregister.jsp:注册信息弹框
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//设置编码-设置请求对象的编号
request.setCharacterEncoding("utf-8");
//接收注册页面传递的数据
String uname = request.getParameter("uname");
String upwd = request.getParameter("upwd");
String uinfo = request.getParameter("uinfo");
//连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
/*
查询到用户的最大编号
最大编号+1,做为新添加的用户的编号
*/
//这里的代码是用来 得到最新编号的
int nextId=1;//做为新用户的编号
PreparedStatement ps = con.prepareStatement("select max(uuid) from T277");
//执行sql语句
ResultSet rs= ps.executeQuery();
if(rs.next()){
//查询到最大的编号,加1,就是新数据的编号
nextId = rs.getInt(1)+1;
}
//这里的代码 开始执行添加用户操作
ps = con.prepareStatement("insert into T277 values(?,?,?,?)");
ps.setInt(1, nextId);
ps.setString(2, uname);
ps.setString(3, upwd);
ps.setString(4, uinfo);
//执行sql语句
int i = ps.executeUpdate();
if(i>0){
out.print("<script>alert('注册成功');location.href='login.jsp'</script>");
}else{
out.print("<script>alert('注册失败');location.href='register.jsp'</script>");
}
%>
login.jsp:登录
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
#a {
width:50%;
height:200px;
border: 1px dashed ;
background-color:lightyellow;
text-align:center;
}
body{
background-color:lightblue;
}
</style>
</head>
<body>
<form action="dologin.jsp">
用户名:<input type="text" name="uname"><br>
密码:<input type="password" name="upwd"><br>
<input type="submit" value="登录">
<input type="button" value="注册" onclick="javascript:location.href='register.jsp'">
</form>
</body>
</html>
dologin.jsp:与数据库相连、存放登陆的用户
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//接收login页面传递的数据
String uname = request.getParameter("uname");
String upwd = request.getParameter("upwd");
//连接数据库
//注册驱动类
//OracleDriver
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接数据库
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
//创建对象
PreparedStatement ps = con.prepareStatement("select * from T277 where uname=? and upwd=?");
//给占位符赋值
ps.setString(1, uname);
ps.setString(2, upwd);
//执行sql语句
ResultSet rs = ps.executeQuery();
//处理结果
if(rs.next()){
//转发
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
//失败
out.print("<script>alert('用户名或密码错误,请重新登录');location.href='login.jsp'</script>");
}
//关闭连接
%>
index.jsp:主界面
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>主页面</title>
<style>
#a {
width:50%;
height:200px;
border: 1px dashed ;
background-color:lightyellow;
text-align:center;
}
body{
background-color:lightblue;
}
</style>
</head>
<body>
<table border="1" align="center" width="80%">
<tr>
<td>编号</td>
<td>用户名</td>
<td>密码</td>
<td>备注</td>
<td>操作</td>
</tr>
<%
//连接数据库 进行查询所有数据
//注册驱动类
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接数据库
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
//创建Pre对象
PreparedStatement ps = con.prepareStatement("select * from T277 order by uuid");
//执行sql语句
ResultSet rs = ps.executeQuery();
//处理结果-遍历结果集
while(rs.next()){
%>
<tr align="center">
<td><%=rs.getInt(1) %></td>
<td><%=rs.getString("uname") %></td>
<td><%=rs.getString("upwd") %></td>
<td><%=rs.getString(4) %></td>
<td>
<a href="doup.jsp?uuid=<%=rs.getInt(1)%>">修改</a>
<a href="info.jsp?uuid=<%=rs.getInt(1)%>">详情</a>
<a href="dodel.jsp?uuid=<%=rs.getInt(1)%>">删除</a>
</td>
</tr>
<%} %>
</table>
</body>
</html>
update.jsp:修改
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//设置编码-设置请求对象的编号
request.setCharacterEncoding("utf-8");
//接收修改页面传递的数据
String uname = request.getParameter("uname");
String upwd = request.getParameter("upwd");
String uinfo = request.getParameter("uinfo");
//获取要修改的用户编号
String id = request.getParameter("uuid");
//把字符串转换为整数编号
int uuid = Integer.valueOf(id);
//JDBC 删除
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
PreparedStatement ps = con.prepareStatement("update T277 set uname=?,upwd=?, uinfo=? where uuid="+uuid);
ps.setString(1, uname);
ps.setString(2, upwd);
ps.setString(3, uinfo);
int i = ps.executeUpdate();
if(i>0){
out.print("<script>alert('修改成功');location.href='index.jsp'</script>");
}else{
out.print("<script>alert('修改失败');location.href='index.jsp'</script>");
}
%>
doup.jsp:修改页面(帮助)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//获取要修改的用户编号
String id = request.getParameter("uuid");
//把字符串转换为整数编号
int uuid = Integer.valueOf(id);
%>
<form action="update.jsp?uuid=<%=uuid %>" method="post">
用户名:<input type="text" name="uname"><br>
密码:<input type="password" name="upwd"><br>
备注:<textarea rows="" cols="" name="uinfo">
</textarea><br>
<input type="submit" value="修改">
<input type="reset" value="重置">
</form>
</body>
</html>
info.jsp:详情
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获取要修改的用户编号
String id = request.getParameter("uuid");
//把字符串转换为整数编号
int uuid = Integer.valueOf(id);
//JDBC 删除
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
PreparedStatement ps = con.prepareStatement("select * from T277 where uuid="+uuid);
ResultSet rs= ps.executeQuery();
if(rs.next()){
%>
<form>
用户名:<input type="text" name="uname" value="<%=rs.getString("uname") %>"><br>
密码:<input type="password" name="upwd" value="<%=rs.getString("upwd") %>"><br>
备注:<textarea rows="" cols="" name="uinfo" >
<%=rs.getString(4) %>
</textarea><br>
<a href="index.jsp">返回</a>
</form>
<%} %>
dodel.jsp:删除界面
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获取要删除的用户编号
String id = request.getParameter("uuid");
//把字符串转换为整数编号
int uuid = Integer.valueOf(id);
//JDBC 删除
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
PreparedStatement ps = con.prepareStatement("delete T277 where uuid="+uuid);
int i = ps.executeUpdate();
if(i>0){
out.print("<script>alert('删除成功');location.href='index.jsp'</script>");
}else{
out.print("<script>alert('删除失败');location.href='index.jsp'</script>");
}
%>
二·页面跳转的方式以及他们的区别
1、跳转路径并传递数据
//假设法:假设我的用户名为admin 密码为123
if("admin".equals(name)&&"123".equals(pwd)){
//说明登录成功 跳转到success.jsp
//1.重定向 将页面跳转 地址栏发生了改变
//不能将值传递到下一个界面 可以跳转到任意资源 在客户端发挥作用
response.sendRedirect("http://www.baidu.com");
//2.转发 将页面跳转 可以将值传递到下一个界面
//但是地址栏不发生改变 而是停留在了之前的页面 只能转发到当前项目内资源 在服务器端发挥作用
//request.getRequestDispatcher("http://www.baidu.com").forward(request, response);
}
else{
//说明登录失败 提示用户并返回登录界面login.jsp js的跳转属于重定向 地址栏发生了改变 login-->dologin-->login
out.print("<script>alert('用户名或者密码有误');location.href='login.jsp';</script>");
}
2、页面跳转的两种方式和区别
重定向:地址栏发生改变 不能传值 可以跳转到任意资源 在客户端发挥作用
转发:地址栏不发生改变 能传值 只能跳转到当前项目内资源 在服务器端发挥作用语法:
重定向:response.sendR()转发:request.getreq .for(request,response)
三·其他
1、jdbc连接Oracle
URL:jdbc:oracle:thin:@localhost:1521:orcl
2、乱码
request.setCharacterEncoding("utf-8");
3、添加数据时,获取最新编号
//这里的代码是用来 得到最新编号的
int nextId=1;//做为新用户的编号
PreparedStatement ps = con.prepareStatement("select max(uuid) from T277");
//执行sql语句
ResultSet rs= ps.executeQuery();
if(rs.next()){
//查询到最大的编号,加1,就是新数据的编号
nextId = rs.getInt(1)+1;
}
以上就是今天的分享,谢谢大家的观赏!!!
文章来源:https://blog.csdn.net/m0_62246061/article/details/123744495
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: