• JSP第七次作业


    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@page import="java.sql.SQLException"%>
    <%@page import="com.gd.dao.BaseDao"%>
    <%@page import="com.gd.entity.Msg"%>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.ResultSet"%>
    
    <%@page import="java.sql.Connection"%>
    <body>
        <%
            String uname = request.getParameter("uname");
            session.setAttribute("uname", uname);
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                //加载驱动 
    
                String url = "jdbc:mysql://localhost:3306/user";
                String user = "root";
                String password = "itcast";
                conn = DriverManager.getConnection(url, user, password);
                ps = conn.prepareStatement("select * from msg where username=?");
                ps.setString(1, uname);
                rs = ps.executeQuery();
        %>
        <table>
            <tr>
            <td>msgid</td>      
            <td>username</td>
                <td>title</td>
                <td>msgcontent</td>
                <td>state</td>
                <td>sendto</td>
                <td>msg_create_date</td>
            </tr>
            <%
                while (rs.next()) {
            %>
            <tr>
                <td><%=rs.getString("username")%></td>
                <td><%=rs.getString("title")%></td>
                <td><%=rs.getString("msgcontent")%></td>
    
                <td>
                    <%
                        if (rs.getString("state").equals("1")) {
                    %> <input type="button" value="点击查看"
                    onclick="window.location.href='show.jsp';" /> <%
         } else {
     %>
                    <div align="center">
                        <%
                            out.print("已查看");
                        %>
                    </div> <%
         }
     %> <%
    
         String title=rs.getString("title");
         session.setAttribute("title", title);
         
     %>
                </td>
                <td><%=rs.getString("sendto")%></td>
                <td><%=rs.getString("msg_create_date")%></td>
            </tr>
            <%
                }
            %>
        </table>
        <br>
    
        <%
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) { // ignore }
                        rs = null;
                    }
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) { // ignore }
                        ps = null;
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) { // ignore }
                        rs = null;
                    }
                }
            }
        %>
    
    </body>
    </html>
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
       <form action="dologin.jsp" method="post">
    用户名:<input type="text" name="uname" value="kitty" /><Br>
    密码 :<input type="password" name="upwd" value="777"/><br>
    
    <input type="submit" value="登录">
    
    </form>
      </body>
    </html>
    <%@page import="com.gd.dao.UsersDao"%>
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud = new UsersDao();
        if (ud.login(uname, upwd))
            request.getRequestDispatcher("main.jsp").forward(request, response);
        else
            response.sendRedirect("index.jsp");
    %>
    package com.gd.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class UsersDao extends BaseDao {
        // 登录功能
        public boolean login(String uname, String upwd) throws SQLException {
            // 获取连接
            Connection conn = getConnection();
            // 编写sql语句
            String sql = "select * from users where username=? and password=?";
            // 执行sql语句
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                closeAll(conn, ps, rs);
                return true;
            } else {
                closeAll(conn, ps, rs);
                return false;
    
            }
        }
    //    public static void main(String[] args) {
    //        UsersDao ud=new UsersDao();
    //        try {
    //            System.out.println(ud.login("tom", "456"));
    //        } catch (SQLException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //    }
    
    }
    package com.gd.dao;
    
    public class MsgDao {
        //发送,回复---insert操作
        
        //邮件列表  --select * from  msg where username=....
        
        
        //根据标题查内容   select 
        
        //阅读状态改变,,,未读 已读   update
        
        //删除邮件  delete 
        
    
    }
    package com.gd.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    public class BaseDao {
    
    
        //获取连接
        protected Connection getConnection(){
            Connection conn=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    // 2.建立连接
                    conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/user", "root", "itcast");
                } catch (Exception e) {
                    e.printStackTrace();
                } 
                return conn;
        }    
        
    
        
        
        //关闭连接
        protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
        try {
            if(rs != null)
                rs.close();
            if(ps != null)
                ps.close();
            if(con != null)
                con.close();
            
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
    }

  • 相关阅读:
    C语言的swap函数的易错点
    C语言结构体指针,及其错误示范
    操作系统--问答题
    数据库-存储过程、触发器、视图-考研笔记
    数据库知识-恢复子系统、数据库恢复的基本技术、转储方法、日志文件、undo/redo操作、数据库镜像
    c语言,快排序找出第k小/大的数
    STM32的Flash读写保护,SWD引脚锁的各种解决办法汇总(2020-03-10)
    uCOS全家桶PDF文档整理汇总贴,提供论坛,百度云和腾讯云下载(2020-03-09)
    分享下之前做的STM32嵌入式Web完整设计教程和案例html,Ajax,Javacript,XML,cgi等
    【STM32H7教程】第71章 STM32H7的内部Flash应用之模拟EEPROM
  • 原文地址:https://www.cnblogs.com/zq542960954/p/12813837.html
Copyright © 2020-2023  润新知