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() + "\t" + u.getUserName() + "\t" + u.getPassword() +"\t <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

时间: 2024-10-10 16:34:17

1207整理的相关文章

WPF笔记整理 - Bitmap和BitmapImage

项目中有图片处理的逻辑,因此要用到Bitmap.而WPF加载的一般都是BitmapImage.这里就需要将BitmapImage转成Bitmap 1. 图片的路径要用这样的,假设图片在project下的Images目录,文件名XXImage.png. pack://application:,,,/xxx;component/Images/XXImage.png 2. 代码: Bitmap bmp = null; var image = new BitmapImage(new Uri(this.X

linux 命令 及学习进度综合整理

linux  命令  及学习进度综合整理 pwd 查看当前所在位置 cd  跳转到什么什么目录 ls  显示所有文件和目录 ls -l  显示目录详细信息 cd ..  返回上一级 vi  lnany.txt  创建一个文件 vi  .lnany.txt    创建一个隐藏文件 vim 是 vi 的升级版 功能更多 出现 -bash: vim: command not found 的解决办法 i. 那么如何安裝 vim 呢?输入rpm -qa|grep vim 命令, 如果 vim 已经正确安裝

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

夏令营讲课内容整理Day 0.

今年没有发纸质讲义是最气的.还好我留了点课件. 第一次用这个估计也不怎么会用,但尝试一下新事物总是好的. 前四天gty哥哥讲的内容和去年差不多,后三天zhn大佬讲的内容有点难,努力去理解吧. 毕竟知识还是需要消化的. 这里我只整理知识点,每天上午评测的题目我会单独处理. 嗯大概就是这样了. 写完后我就会考虑发到博客园里.

linux基本命令整理(三):进程和vim

linux基本命令整理(三) -----------进程和vim 一.进程 1.查看进程 ps:将某个时间点的程序运行的状况截取下来 a:所有的进程 x:后台进程 u:有效的使用者相关的进程(常用组合aux) -IA:也能观察系统所有的数据 axjf:连同部分的程序树状态 -I:今查看和自己bash相关的程序 top:动态的观察进程的变化 -d:后面接描述,就是整个页面刷新的时间:默认是5秒 -b:以批次的方式执行top -n:与-b搭配使用,意义是需要进行几次top的输出结果 如:top -b

检测和整理索引碎片

索引碎片的检测和整理 存储数据是为了查找数据,存储结构影响数据查找的性能.对无序数据进行查找,最快的查找算法是哈希查找:对有序数据进行查找,最快的查找算法是平衡树查找.在传统的关系型数据库中,聚集索引和非聚集索引都是平衡树(B-Tree)类型的存储结构,用于顺序存储数据,便于实现数据的快速查找.除了提升数据查找的性能之外,索引还能减少硬盘IO和内存消耗.通常情况下,硬盘IO是查找性能的瓶颈,由于索引是数据表的列的子集,这意味着,索引只存储部分列的数据,占用的硬盘空间比全部列少了很多,因此,数据库

新awk整理

总感觉上一篇awk的总结几乎是照着man翻译过来的,惨不忍睹 无意间在互联网上有找到了宝贵的资料 感觉整理的很好,想着照着这个来重新写下,对照新的man更新下吧,只是总是在改变的 一.awk简介二.awk工作流程三.awk程序结构四.awk基本语法 awk命令行模式 awk程序文件 awk标准选项五.awk内置变量 标准awk变量 awk特定变量(即专有变量)六.awk操作符 算数运算符 关系运算符 逻辑运算符 三元运算符 一元运算符 字符串连接操作符(即空格符) 数组成员操作符(in) 正则表

Linux运维(十)-2016-12-9整理

也有段时间没有整理面试题目了,这几天呢,完成了2场实习生和1场校招,十分郁闷,为什么金融运维一点都不关注技术,在乎我的学校成绩,我尼玛想说,学校那么水的课程能说明什么,跟技术有一丁丁点关系吗?我学业成绩不好,就能否定我的专业岗位基础不行吗?唉,劳资想说,你如果质疑我的能力,你就问我,劳资要是被你虐倒是我算我输,学艺不精我就服,看过去有球用. 这辈子职业生涯都不想进外包和IDC这两种类型的公司,就是TM打杂的,薪资还低,关键是不厚道,比如垃圾胜蓝.这回校招那家是我之前就提到的那家,大多数人对我是认

Web网站服务及知识整理(二)

Web网站服务及知识整理(二)