加载中…
正文 字体大小:

JAVA通过servlet对Mysql连接、插入、修改、删除操作

(2009-08-27 21:29:31)
标签:

杂谈

分类: JAVA-初级

通过servlet来实现对Mysql进行连接、插入、修改、删除操作


package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import src.Logg;
import src.MysData;

 

public class UserServlet extends HttpServlet {
   Logg log=new Logg();
   MysData usr=new MysData();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
   //java.sql.Connection conn=log.ConnectMysql();不能在这里这样写。具体在操作函数里连接,见后面。
    usr.setName(request.getParameter("username"));
    usr.setPassword(request.getParameter("password"));
    usr.setEmail(request.getParameter("email"));
    String de=request.getParameter("eee");//从不同的form过来的隐藏属性字段“eee”,通过对其值的判断就知道说哪个form,然后执行不同的操作
    if(de.equals("qq")){
            log.InsertSql(usr);
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
            out.println("wewewe");
            out.println("<br>");
            out.println(request.getAttributeNames());
            out.println("注册成功!");
            out.println(de);
            out.println("</body>");
            out.println("</html>");
    }else if(de.equals("cha")){
        String password= request.getParameter("password");
        //String email= request.getParameter("email");
        //String userid=request.getParameter("userid");
        //下面的5是随便设置的值,很明显,这个值应该是当然登陆的用户的值,那么这个值如何获得呢?明天研究。//后来研究了下,通过session来弄的。后面有用到session,但是只是简单弄了下。
        String upsql="update user set password="+password+" where userid=5";

        out.print(upsql);
       
        log.UpdateSql(upsql);
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
            out.println("wewewe");
            out.println("<br>");
            out.println(request.getAttributeNames());
            out.println("修改成功!");
            out.println(de);
            out.println("</body>");
            out.println("</html>");

    }else if(de.equals("logg")){
                    HttpSession session=request.getSession(true);
                    Logg logg=new Logg();

                    String username=request.getParameter("username");
                    String password=request.getParameter("password");

                    String sql="select * from user where username="+username+" and password="+password;
                   
                    ResultSet rs=logg.SelectSql(sql);

                   

下面这段代码之所有有问题,是因为不是根据主键来获取的。导致获取的数据可能很多条相同的。回头再研究下主键。
                       session.setAttribute("username", username);

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
            out.println("登录成功!");
            out.println(de);
            out.println("<br>");
            out.println(session.getAttribute("username")+"");
            out.println(sql);
        //    out.println(username2);
            out.println("<br>");
            out.println(request.getAttributeNames());
            out.println();
            out.println("</body>");
            out.println("</html>");
            out.flush();
    }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(UserServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("weeeeeeeeeeeeeeeeee");
       
    }

   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(UserServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

 

***********************************************************************************

//把上次的那个logg类里的查询操作的返回值改了下。
         public ResultSet SelectSql(String sql){
             try{
                 conn=this.ConnectMysql();//见上面。在操作之前链接。
                 Statement statement=conn.createStatement();
                 rs=statement.executeQuery(sql);
                 while(rs.next()){
                     name=rs.getString("username");
                     System.out.println(rs.getString("userid")+name+rs.getString("email"));
                  }
             }catch(Exception e){
                 e.printStackTrace();
             }
            return rs;
         }

 

******************************************************************************************

 


 

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page555</title>
    </head>
    <body>
        <h1>Hello World!22222</h1>
        <form method="POST" action="/ServLogg/UserServlet">

//下面这个“eee”就是上面提到的隐藏域。也可以写到 <form method="POST" action="/ServLogg/UserServlet?eee=qq">在servlet后面加问号,然后再设定隐藏域参数及其值,和下面使用的hidden隐藏域方法一样的效果,但下面的方法可以设置多个隐藏域的值


             <input type="hidden" name="eee" value="qq">
            用户名:<input type="text" name="username"><br>
            密码:<input type="text" name="password"><br>
            邮箱<input type="text" name="email"><br><br>

            <input type="submit" name="submit" value="确定">
            <input type="reset" name="reset" value="重置">

        </form>
    </body>
</html>

 

**************************************************

 

 

阅读 收藏 转载 喜欢 打印举报
已投稿到:
  

新浪BLOG意见反馈留言板 不良信息反馈 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

新浪公司 版权所有