前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaWeb 例子 JDBC+JSP登陆注册留言板

JavaWeb 例子 JDBC+JSP登陆注册留言板

作者头像
二十三年蝉
发布2018-02-28 10:57:48
3.4K1
发布2018-02-28 10:57:48
举报
文章被收录于专栏:闻道于事闻道于事

注册页面:

代码语言:javascript
复制
<%@ 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>
    <form action="chuli.jsp" method="post">
        <input type="hidden" name="sub_type" value="reg" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="password" name="password1" placeholder="确认密码" />
        <input type="submit" value="注册" /><a href="denglu.jsp">返回登陆</a>
    </form>
</body>
</html>

登录页面:

代码语言:javascript
复制
<%@ 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>
    <form action="chuli.jsp" method="post">
        <input type="hidden" name="sub_type" value="log" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="submit" value="登录" /><a href="zhuce.jsp">注册新用户</a>
    </form>
</body>
</html>

主页-留言板页:

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.util.User,java.util.*,com.test.*,com.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
    
    Object obj1 = session.getAttribute("currentUser");
    if (obj1 == null) {
        response.sendRedirect("denglu.jsp");
    }
%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="chuli.jsp" method="post" >
        <input type="hidden" name="sub_type" value="tex" />
        <textarea rows="4" cols="12" name="text"></textarea>
        <input type="submit" value="提交"> 
    </form>
<%
MethodDal m=new MethodDal();

List<Message> list=m.selectMessage();

if(list!=null){

    for(Message l: list ){
        out.print(l.getUname()+"  |  ");
        out.print(l.getDate()+"<br>");
        out.print(l.getSays()+"<hr>");
    }
}
%>
</body>
</html>

逻辑处理页面:

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.util.User,com.util.Message,java.util.*,com.test.*" errorPage="error.jsp" %>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%
    String sub_type = request.getParameter("sub_type");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String password1 = request.getParameter("password1");
    MethodDal m=new MethodDal();
    Message says=new Message();
    
    if("reg".equals(sub_type)) {
        
        if (!password.equals(password1)) {
            out.print("两次输入的密码不一致 !");
        } else {
            String selectname=m.selectName(username);
            if (!selectname.equals("no")) {
                out.print("用户名已经存在 !");
            } else {
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                
                m.insertData(user);
                out.print("注册成功 !");
            }
        }
        out.print("<br>");
        out.print("<a href='denglu.jsp'>跳转登陆</a>");
    }
    if("log".equals(sub_type)) {
        String selectname=m.selectName(username);
        application.setAttribute("name",username);
        String spwd=m.selectPwd(password);
        
        if(username.equalsIgnoreCase(selectname)&&password.equalsIgnoreCase(spwd)) {
            session.setAttribute("currentUser", 1);
            response.sendRedirect("Maintest.jsp");
            
        } else {
            out.print("用户名或密码不正确 !");
        }
        out.print("<br>");
        out.print("<a href='denglu.jsp'>跳转登陆</a>");
    }
    if("tex".equals(sub_type)){
        String say = request.getParameter("text");
        Object sobj=application.getAttribute("name");//获取用户名
        String sname=(String)sobj;
        Date d=new Date();
        
        
        System.out.println(sname);
        says.setUname(sname);
        says.setSays(say);
        says.setDate(new Date().toLocaleString());
        
        int i=m.insertSays(says);
        
        response.sendRedirect("Maintest.jsp");
    }
    
%>

JDBC设置工具类:

代码语言:javascript
复制
package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.util.JdbcConnectionUtil;

public class JdbcConnectionUtil {
    private static final String USERNAME = "test";
    private static final String PASSWORD = "test";
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
    private static final String DRIVERCLASSNAME = "oracle.jdbc.OracleDriver";

    public static Connection getConnection(){
        Connection conn=null;
        try {
            Class.forName(DRIVERCLASSNAME);
            conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void destroy(Connection conn){
        if(conn!=null){
            try {
                conn.close();
                conn=null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
        }
    }
    public static void main(String[] args) {
        System.out.println(JdbcConnectionUtil.getConnection());
    }
}

数据库增删改查工具类:

代码语言:javascript
复制
package com.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.util.JdbcConnectionUtil;
import com.util.Message;
import com.util.User;

public class MethodDal {
    private Connection con;
    private PreparedStatement pste;
    private ResultSet rs;
    //向表中添加数据
    public int insertData(){
        init();
        int i=-1;
        String sql="insert into puser values('c','c')";
        
        try {
            pste=con.prepareStatement(sql);
            i=pste.executeUpdate();//针对于增删改数据吗,返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    //向表中添加数据
    public int insertData(User user){
        init();
        int i=-1;
        String sql="insert into puser values(?,?)";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,user.getUsername());
            pste.setString(2, user.getPassword());
            i=pste.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    //删除记录
    public int deleteDate(int id){
        
        return 0;
    }
    //查现有姓名
    public String selectName(String name){
        init();
        String sql="select * from puser p where p.pname=?";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,name);
            rs = pste.executeQuery();
            while(rs.next()) {
                String pname=rs.getString("pname");
                if(pname!=null){
                    return pname;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "no";
    }
    //根据姓名查密码
    public String selectPwd(String name){
        init();
        String sql="select * from puser p where p.pname=?";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,name);
            rs = pste.executeQuery();
            while(rs.next()) {
                String pwd=rs.getString("ppassword");
                return pwd;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "wu";
    }
    //根据添加信息
    public int insertSays(Message m){
        init();
        int i=-1;
        String sql="insert into pmessage values(?,?,?)";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,m.getUname());
            pste.setString(3,m.getDate());
            pste.setString(2,m.getSays());
            i=pste.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    
    //查询所有数据
     public List<Message> selectMessage() {
            String sql = "select pname,says,ptime from pmessage order by ptime desc";
            init();
            List<Message> list = new ArrayList<Message>();
            try {
                pste = con.prepareStatement(sql);
                rs = pste.executeQuery();
                while (rs.next()) {

                    Message au = new Message();
                    au.setUname(rs.getString(1));
                    au.setSays(rs.getString(2));
                    au.setDate(rs.getString(3));
                    list.add(au);
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
            return list;
        }
    public void init(){
        con=JdbcConnectionUtil.getConnection();
    }
}

工具类-留言信息:

代码语言:javascript
复制
package com.util;

public class Message {
        private String uname;
        private String date;
        private String says;
        
        
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }
        public String getSays() {
            return says;
        }
        public void setSays(String says) {
            this.says = says;
        }
        @Override
        public String toString() {
            return "Says [uname=" + uname + ", date=" + date + ", says=" + says + "]";
        }
}

工具类-用户信息:

代码语言:javascript
复制
package com.util;

public class User {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档