zoukankan      html  css  js  c++  java
  • Servlet学习-数据库的操作

    servlet就是一个java类,所以连接数据库的原理和普通java一样额

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            
            PrintWriter out = response.getWriter();
            request.setCharacterEncoding("utf-8");
            String id=request.getParameter("username");
            String password=request.getParameter("password");
            
            out.println("id = " +id);
            
            //Servlet操作数据库和普通java类一样
            
            Connection ct= null;
            PreparedStatement ps =null;
            ResultSet rs = null;
            try {
                //1.加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                //2.得到连接
                ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "1");
                //3.创建PreparedStatment 用于传送sql查询语句
                ps=(PreparedStatement) ct.prepareStatement("select * from users where id =? and password=?");
                //给?赋值
                ps.setObject(1, id);
                ps.setObject(2, password);
                
                //4.执行操作
                rs= ps.executeQuery();
                //5.根据结果做处理
                if(rs.next())
                {//合法
                    request.getRequestDispatcher("/MainFrame").forward(request, response);
                }else
                {
                    request.setAttribute("error", "用户名 或者 密码错误!");
                    request.getRequestDispatcher("/LoginServlet").forward(request, response);
                }
                
                
            } catch (Exception e) {
                e.printStackTrace();
                // TODO: handle exception
            }finally
            {
                //关闭资源
                if(rs!=null)
                {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    rs=null;
                }
                if(ps!=null)
                {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    ps=null;
                }
                if(ct!=null)
                {
                    try {
                        ct.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    ct=null;
                }
            }
            
            
            //out.println("username"+username);
            
            
    
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            this.doGet(request, response);
    
        }
  • 相关阅读:
    win10系统打印图片中间空白的解决办法
    DELPHI SOKET 编程--使用TServerSocket和TClientSocket
    因为未启用行移动功能 不能闪回表
    oracle闪退(回退)功能
    查看oracle数据库的数据文件的目录
    Oracle
    Delphi : keydown与keypress的区别,组合键
    Delphi Xe 中如何把日期格式统一处理,玩转 TDatetime
    移除 IIS 的各种头信息
    sql server 通过 sql 查询数据库状态
  • 原文地址:https://www.cnblogs.com/bersaty/p/3204151.html
Copyright © 2011-2022 走看看