ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

11

2022-05-15 13:03:51  阅读:110  来源: 互联网

标签:11 ps return String sql import public


package com.yxf.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/test", "root", "root");
            } 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();
        }
    }
     
}
package com.yxf.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.yxf.entity.Msg;
 
public class MsgDao extends BaseDao{
    public List<Msg> getMailByReceiver(String name){
        List<Msg> list=new ArrayList<Msg>();
        Connection con=getConnection();
        String sql="select * from msg where sendto=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, name);
             rs=ps.executeQuery();
            while(rs.next()){
                Msg m=new Msg();
                m.setMsgid(rs.getInt("msgid"));
                m.setUsername(rs.getString("username"));
                m.setTitle(rs.getString("title"));
                m.setState(rs.getInt("state"));
                m.setMsgcontent(rs.getString("msgcontent"));
                m.setSendto(rs.getString("sendto"));
                m.setMsg_create_date(rs.getDate("mgs_create_date"));
                list.add(m);               
            }           
             
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }       
        return list;
    }
     
}
package com.yxf.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 name,String pwd){
        boolean f=false;
        Connection conn=getConnection();
        String sql="select * from users where uname=? and upwd=?";
        PreparedStatement ps;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            ResultSet rs=ps.executeQuery();
            if(rs.next())
                f=true;
            closeAll(conn, ps, rs);
             
             
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
        return f;
    }
    public int reg(String uname, String upwd) {
        Connection conn = getConnection();
        int i=0;
        PreparedStatement ps = null;
        try {
            String sql = "insert into users(uname,upwd) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            i=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
        return i;
    }
}
package com.yxf.entity;
 
import java.util.Date;
 
public class Msg {
    private int msgid;
    private String username;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    private Date msg_create_date;
    public int getMsgid() {
        return msgid;
    }
    public void setMsgid(int msgid) {
        this.msgid = msgid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getMsgcontent() {
        return msgcontent;
    }
    public void setMsgcontent(String msgcontent) {
        this.msgcontent = msgcontent;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public String getSendto() {
        return sendto;
    }
    public void setSendto(String sendto) {
        this.sendto = sendto;
    }
    public Date getMsg_create_date() {
        return msg_create_date;
    }
    public void setMsg_create_date(Date msg_create_date) {
        this.msg_create_date = msg_create_date;
    }
 
}
package com.yxf.entity;
 
public class Users {
    int id;
    String uname;
    String upwd;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
 
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
 
</head>
 
<body>
<script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
</script>
 
    <form name="loginForm" action="dologin.jsp" method="post">
         
    用户名:<input type="text" name="uname"><br>
    密码: <input  type="password" name="upwd">
     
        <input type="button" value="登录" onClick="validate()">   
        <a href="resgit.jsp">注册</a>
    </form>
 
</body>
</html>
<%@page import="com.yxf.dao.MsgDao"%>
<%@page import="com.yxf.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();
    MsgDao md=new MsgDao();
    if(ud.login(uname, upwd)){   
        session.setAttribute("uname", uname);   
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }else{
        out.print("登陆失败,即将跳回登陆页.....");
        response.setHeader("refresh", "5;url=denglu.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>
<script type="text/javascript">
        function validate(){
            if(resForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(resForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            resForm.submit();
        }
    </script>
        <form name="resForm" action="doresgit.jsp" method="post">
            <b>用户名</b>
            <input type="text" name="uname" />
            <br />
            <b>密码</b>
            <input type="text" name="upwd" />
            <br />
            <input type="button" value="注册" onClick="validate()">   
            <a href="denglu.jsp">登录</a>
        </form>
</body>
</html>
<%@page import="com.yxf.entity.Msg"%>
<%@page import="com.yxf.dao.MsgDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 
<html>
<head>
<title>My JSP 'index.jsp' main page</title>
</head>
<body>
<%
    String uname=(String)session.getAttribute("uname");
 %>
 欢迎你 <% out.print(uname); %>
 <table height="20px" width="100%" align="center" border="1">
       <tr>
           <td>发件人</td>
           <td>主题</td>
           <td>状态</td>
           <td>时间</td>
       </tr>
         <%
         MsgDao md=new MsgDao();
         List<Msg> list=md.getMailByReceiver(uname);  
            for(Msg m:list)
            {%>
           <tr>
               <td><%=m.getUsername() %></td>
               <td><%=m.getTitle() %></td>
               <td><%
               int state=m.getState();
               if(state==1){ %>
                   <img src ="image/已读消息.png" />
                   <%;
               }else{%>
                   <img src ="image/未读.png" />
                   <%;
               }
               %></td>
               <td><%=m.getMsg_create_date() %></td>
           </tr>
            <%}
        %>
   </table>
</body>
</html>

 

标签:11,ps,return,String,sql,import,public
来源: https://www.cnblogs.com/longnanjiesb/p/16272896.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有