zoukankan      html  css  js  c++  java
  • 1207整理

    <%@ 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>用户注册</title>
    <script type="text/javascript">
    
    //判断注册时输入的用户信息是否为空
    function check()
    {
        var uid = document.getElementById("userid");
        if (uid.value == "")
            {
            alert("用户代码不能为空");
            
            return false;
        
            }
        if(uform.password.value == "")
            {
             alert("密码不能为空");
            
            return false;
            }
        if(uform.password.value != uform.queren.value)
            {
             alert("两次输入密码不一致");
        
             return false;
            }
        return true;
        }
    </script>
    </head>
    <body>
    
    
    <form id="uform" action="SaveUser" method="get" onSubmit="return check()">
    
    用户代码:<input id="userid" type="text" name="userid" width=30/>
    <br>
    用户名称:<input id="username" type="text" name="username" width=30/>
    <br>
    登录密码:<input id="password" type="password" name="password" width=30/>
    <br>
    确认密码:<input id="queren" type="password" name="queren" width=30/>
    <br>
    <input type="submit" value="提交"/>
    
    </form>
    </body>
    </html>
    zhuce
    package com.hanqi;
    
    import java.io.IOException;
    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 java.sql.*;
    /**
     * Servlet implementation class saveUser
     */
    @WebServlet("/SaveUser")
    public class SaveUser extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public SaveUser() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            request.setCharacterEncoding("UTF-8");
                
            response.setContentType("text/html; charset=UTF-8");
            
            response.setCharacterEncoding("UTF-8");
            
            String userid =  request.getParameter("userid");
    
            String username =  request.getParameter("username");
    
            String pw =  request.getParameter("password");
            
            if(userid == null ||userid.trim().length() == 0)
            {
                response.getWriter().append("用户代码不能为空");
            }
            else if(username == null ||username.trim().length() == 0)
            {
                response.getWriter().append("用户名称不能为空");    
            }
            else
            {
                try 
                {
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    
                    String url = "jdbc:oracle:thin:@localhost:1521:ORCL" ;
                    
                    Connection conn = DriverManager.getConnection(url, "test2", "1234");
                    
                    if(conn != null)
                    {
                        //操作数据库
                        String sql = "insert into t_user (user_id,user_name,password)" + "values(?,?,?)" ;
                        
                        // 执行SQL语句的类
                        PreparedStatement pps = conn.prepareStatement(sql);
                        
                        username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
                        
                        pps.setString(1, userid);
                        pps.setString(2, username);
                        pps.setString(3, pw);
                        
                        int row = pps.executeUpdate();//执行SQL语句并返回数据行数
                        
                        if(row > 0)
                        {
                            //response.getWriter().append("保存"+row+"条数据成功");
                            
                            response.sendRedirect("FindUserList");
                        }
                        else
                        {
                            response.getWriter().append("保存数据失败");
                        }
                        
                        //释放资源
                        
                        pps.close();
                        
                        conn.close();
                    }
                    else
                    {
                        response.getWriter().append("连接数据库失败");
                    }
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                    
                    response.getWriter().append(e.getMessage());
                }
            }
    
            
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    SaveUser
    package com.hanqi;
    
    import java.io.IOException;
    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 java.sql.*;
    import java.util.*;
    
    /**
     * Servlet implementation class FindUserList
     */
    @WebServlet("/FindUserList")
    public class FindUserList extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FindUserList() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            
            response.setContentType("text/html; charset=UTF-8");
            
            response.setCharacterEncoding("UTF-8");
            
                try 
                {
                    //注册数据库驱动
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    
                    //连接URL
                    String url = "jdbc:oracle:thin:@localhost:1521:ORCL" ;
                    
                    // 驱动管理器  工厂模式
                    Connection conn = DriverManager.getConnection(url, "test2", "1234");
                    
                    if(conn != null)
                    {
                        //操作数据库
                        String sql = "select * from t_user";
                        
                        // 执行SQL语句的类
                        Statement stm = conn.createStatement();
                        
                        //执行查询,返回结果集
                        ResultSet rs = stm.executeQuery(sql);
                        
                        //装载和传递
                        ArrayList<User> array = new ArrayList<User>();
                        
                        
                        
                        if(rs != null)
                        {
                            //遍历结果集
                            while(rs.next())
                            {
                                User u = new User();
                                
                                u.setUserID(rs.getString("user_id"));//取值
                                u.setUserName(rs.getString("user_name"));
                                u.setPassword(rs.getString("password"));
                                array.add(u);
                                
                                response.getWriter().append("user_id = "+ rs.getString("user_id")+";");
                                response.getWriter().append("user_name = "+ rs.getString("user_name")+";");
                                response.getWriter().append("password = "+ rs.getString("password")+";");
                            }
                            request.setAttribute("userlist",array);
                            
                            //释放
                            rs.close();
                        }
                        else
                        {
                            response.getWriter().append("结果集为空");
                        }
                        
                        //response.getWriter().append("array = "+ array.size());
                        //释放资源
                        
                        stm.close();
                        
                        conn.close();
                    }
                    else
                    {
                        response.getWriter().append("连接数据库失败");
                    }
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                    
                    response.getWriter().append(e.getMessage());
                }
            request.getRequestDispatcher("ShowUserLiset.jsp").forward(request,response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    FindUserList
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@ page import="java.util.*" %>
        <%@ page import="com.hanqi.*" %>
    <!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>用户列表</title>
    </head>
    <body>
    <% 
    Object obj = request.getAttribute("userlist");
    
    if(obj != null)
    {
        ArrayList<User> array = (ArrayList<User>)obj;
        
        //遍历
        for(User u:array)
        {
            out.print(u.getUserID() + "	" + u.getUserName() + "	" + u.getPassword() +"	 <a href='DeleteUser?userid="+u.getUserID()+"'>删除</a>"
                    +"<form action='EditUser' method='post'>"
                    +"<input type='hidden' name='userid' value='"+u.getUserID()+"'/>"
                    +"用户名称:<input type='text' name='username' value='"+u.getUserName()+"'/>" 
                    +"密码:<input type='password' name='password' value='"+u.getPassword()+"'/>"
                    +"<input type='submit' value='提交'/></form>"
                    + "<br>");
        }
    }
    else
    {
        out.append("用户列表为空");
    }
    
    
    
    %>
    
    
    
    <br><br>
    <a href = "zhuce.jsp">添加新用户</a>
    
    </body>
    </html>
    ShowUserLiset
    package com.hanqi;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    
    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 com.hanqi.common.DBHelper;
    
    /**
     * Servlet implementation class EditUser
     */
    @WebServlet("/EditUser")
    public class EditUser extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public EditUser() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            request.setCharacterEncoding("UTF-8");
            
            response.setContentType("text/html; charset=UTF-8");
            
            response.setCharacterEncoding("UTF-8");
            
            String userid =  request.getParameter("userid");
    
            String username =  request.getParameter("username");
    
            String pw =  request.getParameter("password");
            
            if(userid == null ||userid.trim().length() == 0)
            {
                response.getWriter().append("用户ID不能为空");
            }
            else if(username == null ||username.trim().length() == 0)
            {
                response.getWriter().append("用户名称不能为空");    
            }
            else
            {
                try 
                {
                    Connection conn = DBHelper.getConnection();
                    
                    if(conn != null)
                    {
                        //操作数据库
                        String sql = "update t_user set user_name= ? ,password = ?" + "where user_id=?" ;
                        
                        // 执行SQL语句的类
                        PreparedStatement pps = conn.prepareStatement(sql);
                        
                        //username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
                        
                        pps.setString(1, username);
                        pps.setString(2, pw);
                        pps.setString(3, userid);
                        
                        int row = pps.executeUpdate();//执行SQL语句并返回数据行数
                        
                        //释放资源
                        
                        pps.close();
                        
                        conn.close();
                        
                        response.sendRedirect("FindUserList");
                        
                        /*if(row > 0)
                        {
                            response.getWriter().append("保存"+row+"条数据成功");
                        }
                        else
                        {
                            response.getWriter().append("保存数据失败");
                        }
                        */
                        
                    }
                    
                    else
                    {
                        response.getWriter().append("连接数据库失败");
                    }
                    
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                    
                    response.getWriter().append(e.getMessage());
                }
            }
            
            
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    EditUser
    package com.hanqi.common;
    
    import java.sql.*;
    
    public class DBHelper 
    {
        public static Connection getConnection() throws Exception
        {
            //注册数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            
            //连接URL
            String url = "jdbc:oracle:thin:@localhost:1521:ORCL" ;
            
            // 驱动管理器  工厂模式
            Connection conn = DriverManager.getConnection(url, "test2", "1234");
            
            return conn;
        }
        
    }
    DBHelper
  • 相关阅读:
    多线程之异步操作
    AppDomain
    多线程之线程同步
    webServices与Web服务
    webParts与Web部件
    xhtmlConformance与xhtml脚本呈现
    webControls与客户端脚本路径
    urlMappings与URL映射
    trace与代码跟踪服务
    siteMap与站点导航
  • 原文地址:https://www.cnblogs.com/zxw0004/p/5027399.html
Copyright © 2011-2022 走看看