13

2022/5/29 23:22:57

本文主要是介绍13,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1
package mt;


import java.sql.Connection;


import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
    public Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","990511");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    
    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 mt;

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 glg2.Email;

public class EmailDao extends BaseDao {
    public List<Email> getAll(String uname) {
        List<Email> list = new ArrayList<Email>();
        Connection con = getConnection();
        PreparedStatement pred = null;
        ResultSet resultSet = null;
        String sql = "select * from email where sjr=?";
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, uname);
            resultSet = pred.executeQuery();
            while (resultSet.next()) {
                Email email = new Email();
                email.setId(resultSet.getInt(1));
                email.setFjr(resultSet.getString(2));
                email.setSjr(resultSet.getString(3));
                email.setTitle(resultSet.getString(4));
                email.setContent(resultSet.getString(5));
                email.setTime(resultSet.getDate(6));
                email.setZt(resultSet.getInt(7));
                list.add(email);
            }
        } catch (SQLException e) {

            e.printStackTrace();
        } finally {
            closeAll(con, pred, resultSet);
        }
        return list;
    }

    public void addEmail(Email e) {
        Connection con = getConnection();
        String sql = "insert into email(fjr,sjr,title,content,time,zt) values(?,?,?,?,?,0)";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, e.getFjr());
            pred.setString(2, e.getSjr());
            pred.setString(3, e.getTitle());
            pred.setString(4, e.getContent());
            pred.setDate(5, new java.sql.Date(new Date().getTime()));
            pred.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
    }

    public void del(int id) {
        Connection con = getConnection();
        String sql = "delete from email where id=?";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setInt(1, id);
            pred.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
    }

    public void update(int id) {
        Connection con = getConnection();
        String sql = "update  email set zt=1 where id=?";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setInt(1, id);
            pred.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
    }

    public Email look(int id) {
        Connection con = getConnection();
        String sql = "select id,fjr,sjr,title,content,time from email where id=?";
        PreparedStatement pred = null;
        ResultSet resultSet = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setInt(1, id);
            resultSet = pred.executeQuery();
            while (resultSet.next()) {
                Email email = new Email();
                email.setId(resultSet.getInt(1));
                email.setFjr(resultSet.getString(2));
                email.setSjr(resultSet.getString(3));
                email.setTitle(resultSet.getString(4));
                email.setContent(resultSet.getString(5));
                email.setTime(resultSet.getDate(6));
                return email;
            }

        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, resultSet);
        }
        return null;
    }

}
package mt;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao extends BaseDao{
    public int Register(String uname,String password){
        int i=-1;
        PreparedStatement pred=null;
        Connection con=getConnection();
        String sql="insert into users(uname,password)values(?,?)";
        try {
            pred= con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            i=pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, pred, null);
        }
        return i;
    }
    public boolean Login(String uname,String password){
        boolean f=false;
        PreparedStatement pred=null;
        ResultSet resultSet=null;
        Connection con=getConnection();
        String sql="select * from users where uname=? and password=?";
        try {
            pred=con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            resultSet=pred.executeQuery();
            while(resultSet.next()){
                f=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, pred, resultSet);
        }
        return f;
    }
    
}
package mt;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao extends BaseDao{
    public int Register(String uname,String password){
        int i=-1;
        PreparedStatement pred=null;
        Connection con=getConnection();
        String sql="insert into users(uname,password)values(?,?)";
        try {
            pred= con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            i=pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, pred, null);
        }
        return i;
    }
    public boolean Login(String uname,String password){
        boolean f=false;
        PreparedStatement pred=null;
        ResultSet resultSet=null;
        Connection con=getConnection();
        String sql="select * from users where uname=? and password=?";
        try {
            pred=con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            resultSet=pred.executeQuery();
            while(resultSet.next()){
                f=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, pred, resultSet);
        }
        return f;
    }
    
}
package mt3;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import glg.EmailDao;
@WebServlet("/dodel.do")
public class DoDel extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoDel() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);


    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        EmailDao e=new EmailDao();
        String id=request.getParameter("id");
        int idd=Integer.parseInt(id);
        e.del(idd);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
package mt3;

import java.io.IOException;

import java.io.PrintWriter;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import glg.UserDao;

@WebServlet("/dologin.do")
public class DoLogin extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoLogin() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        UserDao ud = new UserDao();
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        if (ud.Login(uname, password)) {
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request, response);
        } else {
            out.print("鐧诲綍澶辫触锛屽嵆灏嗚烦鍥炵櫥褰曢〉.....");
            response.setHeader("refresh", "2;url=Login.jsp");
        }
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

 



这篇关于13的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程