专栏首页闻道于事JavaWeb 例子 JDBC+JSP登陆注册留言板

JavaWeb 例子 JDBC+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>
    <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>

登录页面:

<%@ 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>

主页-留言板页:

<%@ 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>

逻辑处理页面:

<%@ 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设置工具类:

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());
    }
}

数据库增删改查工具类:

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();
    }
}

工具类-留言信息:

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 + "]";
        }
}

工具类-用户信息:

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;
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 上传图片接口

    /**  *上传图片  * @param savePath  * @return  * @throws Exception  */ public S...

    用户1220053
  • myeclipse去除jsp校验错误

    选择你的工程右击菜单 >> 点击MyEclipse菜单项的Exclude From Validation,如果你的工程里还 有其它需要它检验其合法性建议可以针对...

    用户1220053
  • MD5加密代码(工具类)

    package com.test.system.util; import java.security.MessageDigest; import java.s...

    用户1220053
  • Request对象的主要方法

    Request对象的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值 getAttrib...

    用户1220053
  • 数据库连接(直接用)

    package com.sanqing.util; import java.sql.Connection; import java.sql.DriverMa...

    用户1220053
  • jsp中的类似 if - else 语句 的语法

    <c:choose>    <c:when test="${requestScope.newFlag== '1' || requestScope.newFlag...

    用户1220053
  • 时间控件设置开始时间不大于结束时间

    <div class="form-horizontal fl"> <div class="control-group"> <label...

    用户1220053
  • JSP+ajax+springMVC+MayBatis处理excel上传导入

    jsp <div class="subtext1"> <div cla...

    用户1220053
  • 接口测试类(http,post请求)参数 json格式

    package RMI; import java.io.BufferedReader; import java.io.DataOutputStream; im...

    用户1220053
  • Java Beans

    JavaBeans事实上有三层含义。首先,JavaBeans是一种规范,一种在Java(包括JSP)中使用可重复使用的Java组件的技术规范,也可以说成我们常...

    Steve Wang

扫码关注云+社区

领取腾讯云代金券