一段较为完整的程序

<%@ 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>用户列表   先启动FindUserList</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() + "\t  " + u.getUserName() + "\t " + u.getPassword() + "\t " +u.getUserTime()+
                "\t <a href=‘DeleteUser?userid="+u.getUserID()+"‘>删除</a>"
        +"<form action=‘EditUser‘ method=‘get‘ >"+
                "<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>");
    }
    //DeleteUser?  ?是要删除的userid

}
else
{
    out.append("用户列表为空");
}

%>

<br>
<br>
<br>

<a href="zhuce.jsp">注册新用户</a>
</body>
</html>

ShowUserList

package com.hanqi;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class 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");
            //数据库连接字符串
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            //获取connection对象 (个人理解为登陆数据库 并将数据关联)
            Connection conn = DriverManager.getConnection(url,"test","123654789");    //url,名称,密码
            //判断是否有数据
            if(conn != null)
            {
                //操纵数据库
                String sql = "select * from t_user";

                //执行sql语句的类
/*                 PreparedStatement pps = conn.prepareStatement(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"));
                        u.setUserTime(rs.getString("USER_TIME"));

                        /*
                        //取值 要按照相应类型来取
                        rs.getString("");        // 按字段名来取
                        rs.getString(0);        // 括号内 columnIndex 是 列的序号 字段序号是从  1  开始算的
                        rs.getDouble(0);        //注意要取值的类型
                        */

                        //将装载的数据传输
                        array.add(u);
                        response.getWriter().append("USER_ID = "+ rs.getString("USER_ID")+";<br>");
                        response.getWriter().append("USER_NAME = "+ rs.getString("USER_NAME")+";<br>");
                        response.getWriter().append("PASSWORD = "+ rs.getString("PASSWORD")+";<br>");
                        response.getWriter().append("USER_TIME = "+ rs.getString("USER_TIME")+";<br>");
                    }
                    request.setAttribute("userlist", array);

                    //释放
                    rs.close();
                }
                else
                {
                    response.getWriter().append("结果集为空");
                }
                //关闭  释放资源
                stm.close();

                conn.close();

            }
            else
            {

            }

        }
        catch (ClassNotFoundException e)        //仅捕获一类特定异常
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response.getWriter().append(e.getMessage());
        }
        catch (Exception e)                     //捕获所有异常
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response.getWriter().append(e.getMessage());
        }
        //页面跳转
        request.getRequestDispatcher("ShowUserList.jsp").forward(request,response);

    }

    private void zhuanma() {
        // TODO Auto-generated method stub

    }

    /**
     * @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

package com.hanqi.common;

import java.sql.*;

public class DBHelper {

    //获取connection对象   static 静态方法 不需要调用他的实例化
    public static Connection getConnection() throws Exception    //工具类中异常不要用try catch
    {

        //加载数据库驱动,并注册到驱动管理器
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //数据库连接字符串
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        //获取connection对象 (个人理解为登陆数据库 并将数据关联)
        Connection conn = DriverManager.getConnection(url,"test","123654789");    //url,名称,密码

        return conn;

    }

}

--封装类--

package com.hanqi;

import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hanqi.*;
import java.util.*;
import java.util.Date;
/**
 * Servlet implementation class 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");    

        //获取当前时间
        Date dt = new Date();
        //格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sj = sdf.format(dt);

        //获取内容
        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对象 (个人理解为登陆数据库 并将数据关联)
                Connection conn = DriverManager.getConnection(url,"test","123654789");    //url,名称,密码

                //判断是否有数据
                if(conn != null)
                {
                    //操纵数据库
                    String sql = "insert into t_user (user_id,user_name,password,user_time) values (?,?,?,?)";

                    //另外一种方法:
                    //String sql = "insert into t_user (user_id,user_name,password) values (‘" + userID + "‘)";

                    //执行sql语句的类
                    PreparedStatement pps = conn.prepareStatement(sql);

                    //转码
                    userName = new String(userName.getBytes("ISO-8859-1"),"UTF-8");
                    //post 提交方式不用转码  get方式需要转码

                    //将操纵数据库中数据类名编下顺序便于输入
                    pps.setString(1, userID);
                    pps.setString(2, userName);
                    pps.setString(3, pw);
                    pps.setString(4, sj);

                    int row = pps.executeUpdate();     //执行sql语句并返回所影响的行数

                    if(row > 0)
                    {
                        //response.getWriter().append("保存"+ row +"条数据成功");
                        response.sendRedirect("FindUserList");        //直接跳转到用户列表
                    }
                    else
                    {
                        response.getWriter().append("保存数据失败");
                    }
                    //关闭  释放资源
                    pps.close();

                    conn.close();

                }
                else
                {

                }

            }
            catch (Exception e)        //仅捕获一类特定异常
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                response.getWriter().append(e.getMessage());
            }

        }
    }

    private void zhuanma() {
        // TODO Auto-generated method stub

    }

    /**
     * @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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hanqi.common.DBHelper;

import java.sql.*;

/**
 * Servlet implementation class DeleteUser
 */
public class DeleteUser extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteUser() {
        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");
                if(userid != null && userid.trim().length() > 0)
                {
                    try
                    {
                        Connection conn = DBHelper.getConnection();        //DBHelper 静态方法 不需要实例化
                        if(conn != null)
                        {
                        //    String sql = "delete from t_user where user_id = ‘"+userid+"‘";    //直接组合
                            String sql = "delete from t_user where user_id = ?";            //使用占位符

                            PreparedStatement pst = conn.prepareStatement(sql);

                            pst.setString(1, userid);
                            int row = pst.executeUpdate();        //返回数值  数值代表删除的行数
                            response.sendRedirect("FindUserList");
                            pst.close();
                            conn.close();
                        }

                    } 

                    catch (Exception e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else
                {
                    response.getWriter().append("userid不能为空!");
                }

    }

    /**
     * @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);
    }

}

DeleteUser

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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hanqi.common.DBHelper;

/**
 * Servlet implementation class 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("用户代码不能为空");
                }
                else if(userName == null || userName.trim().length() == 0)
                {
                    response.getWriter().append("用户姓名不能为空");
                }
                else
                {
                    try
                    {
                        Connection conn = DBHelper.getConnection();
                        if(conn != null)
                        {
                            //操纵数据库        select sysdate from dual;
                            String sql = "update t_user set user_name = ?, password = ? where user_id = ?";

                            //另外一种方法:
                            //String sql = "insert into t_user (user_id,user_name,password) values (‘" + userID + "‘)";

                            //执行sql语句的类
                            PreparedStatement pps = conn.prepareStatement(sql);

                            //转码
                            userName = new String(userName.getBytes("ISO-8859-1"),"UTF-8");
                            //post 提交方式不用转码  get方式需要转码

                            //将操纵数据库中数据类名编下顺序便于输入
                            pps.setString(3, userID);
                            pps.setString(1, userName);
                            pps.setString(2, pw);

                            int row = pps.executeUpdate();     //执行sql语句并返回所影响的行数

                            //关闭  释放资源

                            pps.close();

                            conn.close();

                            response.sendRedirect("FindUserList");
                            /*
                            if(row > 0)
                            {
                                response.getWriter().append("保存"+ row +"条数据成功");
                            }
                            else
                            {
                                response.getWriter().append("保存数据失败");
                            }

                            */

                        }
                        else
                        {

                        }

                    }
                    catch (Exception e)        //仅捕获一类特定异常
                    {
                        // TODO Auto-generated catch block
                        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;

    //关于用户的实体类
public class User
{
    //用户代码
    private String UserID;

    public String getUserID()
    {
        return UserID;
    }
    public void setUserID(String userID)
    {
        this.UserID = userID;
    }
    //用户名称
    private String UserName;

    public String getUserName() {
        return UserName;
    }
    public void setUserName(String userName) {
        UserName = userName;
    }

    //密码
    private String Password;

    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }
    //时间
    private String UserTime;

    public String getUserTime() {
        return UserTime;
    }
    public void setUserTime(String userTime) {
        UserTime = userTime;
    }

}

User

<%@ 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 yanz()
{
    if  (zhugan.userid.value == "")
        {
            alert("用户代码不能为空!");
            return false;
        }
    if(zhugan.username.value == "")
        {
            alert("用户名称不能为空!");
            return false;
        }
    if(zhugan.password.value == "")
        {
        alert("登陆密码不能为空!");
        return false;
        }
    /*
    if(zhugan.passwore.value == "")
        {
            alert("确认密码不能为空!");
            return false;
            }
    */
    if(zhugan.password.value != zhugan.passwore.value)
    {
        alert("登陆密码与确认密码不相同!");
        return false;
    }
return true;
}
</script>
</head>
<body>
<form id="zhugan" action="SaveUser"  method="get"  onSubmit="return yanz();">
用户代码:<input id="userid" type="text" name="userid" width=30 />
<br><br>
用户名称:<input id="username" type="text" name="username" width=30 />
<br><br>
登录密码:<input id="password" type="password" name="password" width=30 />
<br><br>
确认密码:<input id="passwore" type="password" name="pueren" width=30 />
<br><br>
<input type="submit" value="提交" />
</form>
</body>
</html>

注册页面

时间: 2024-10-14 21:17:36

一段较为完整的程序的相关文章

完成一个较为完整的用户注册页面

实验目的: 要求能够熟练掌握javascript的一系列知识要点,并能够熟练的应用到实际项目中,能够通过javascript语法.对象.事件.函数等来完成页面上的脚本代码. 实验内容: 要求: 完成一个较为完整的用户注册页面. 1:要求用户输入用户名时只能够输入英文.数字和下划线 2:要求用户输入的密码和确认密码必须一致 3:要求用户上传本地磁盘中的一个图片文件作为头像 4:要求用户输入验证邮箱,通过javascript代码验证邮箱格式是否正确 5:要求页面实现验证码功能,点击"注册"

完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能

#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能'''try: a=int(raw_input("please input a number:"))except ValueError: print("第一个运算数字输入非数字") try: b=int(raw_input("please input another number:"))except Val

9段高效率开发PHP程序的代码

php是世界上最好的语言 在php网站开发中,大家都希望能够快速的进行程序开发,如果有能直接使用的代码片段,提高开发效率,那将是起飞的感觉.今天由杭州php工程师送出福利来了,以下9段高效率开发PHP程序的代码可节省大量开发时间的php代码片段. 一.查看邮件是否已被阅读 当发送邮件后,我们都会想知道到底这个邮件是否已被对方阅读,监控邮件的打开率,这个时候我们就可以使用下面的这段代码显示对方IP地址记录阅读的实际日期和时间. view sourceprint? error_reporting(0

WS彩票游戏完整商业程序源码下载

WS彩票游戏完整商业程序源码下载:http://www.ttkmwl.com/thread-640-1-1.html 源码介绍:完美架设不会架设的不要乱叫程序不完整   不能架设我头给你当凳子坐   求银票换点其他的程序 1.在IIS6.0  安装NET4.O 在IIS里面设置NET4.0版本 首页设置INDEX.ASPX   2.添加.htm  .auth   映射为aspx.dll3.安装SQL2008 R2 还原数据库 BAK 4.修改配置文件webconfig <add name="

2.4、一个完整的程序

整个 hello.py程序脚本就是把前面介绍的三部分合并到一个文件中. hello.py:一个完整的 Flask 程序 #!/usr/bin/env python #_*_ coding:utf-8 _*_ from flask import Flask app = Flask(__name__) @app.route('/') def index(): return '<h1>Hello World!</h1>' if __name__ == '__main__': app.ru

一段有意思的fork()程序

献上代码,不成敬意: #include <unistd.h> #include <stdio.h> int main() { int i = 0; if(fork()) i++; if(fork()) i++; if(fork()) i++; printf("i = %d\n",i); return 0; } 一段看似简单但实际上却很难一下说出结果的程序,最后的输出会是什么呢? 首先说一下fork的定义: fork函数将运行着的程序分成两个(几乎)完全一样的进程

【Todo】【转载】Java中如何写一段内存泄露的程序

可以参考这段文章: link

与MQ通讯的完整JAVA程序

这份程序的原始文档是来自于互联网,不过不知道作者是谁,在些先谢了,我增加了个人的理解在里面,增加注释,这样便于阅读与理解. 该程序实现了发送消息与读取消息的功能,见其中的send***与get***方法.这只适合于测试,因为环境中的程序还需要对此有稍微的更改,在真实的环境中肯定是在while(true){...}的无限循环中去调用其中的get方法,如果有值,那就执行对消息的处理操作,如果没有值就继续循环,在get方法中有等待的时间. 这个程序就其本身来说还是比较理解的: 1.首先设置一些相关的环

较为完整的meta

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- 页面默认使用360极速核.360兼容内核.360标准内核 --> <meta name="renderer" content="webkit|ie-comp|ie-stand"> <!-- 优先使用ie最新版和Chr