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

    <%@ 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" 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>
    注册
    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");
            
            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 
                {
                    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+"条数据成功");
                        }
                        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);
        }
    
    }
    关联数据库

  • 相关阅读:
    vue store状态存储管理
    Git分支管理
    oracle事务不能回滚的原因
    vue教程(四)--其他实用用法补充
    vue教程(三)-slotkeep-alive的使用
    vue教程(二)--过滤器和监视改动功能
    vue教程(一)-html使用vue
    Linux后台命令导入MySQL语句
    CentOS6下的ElasticSearch运行步骤
    浅谈JAVA代码优化
  • 原文地址:https://www.cnblogs.com/zxw0004/p/5020780.html
Copyright © 2011-2022 走看看