码迷,mamicode.com
首页 > 数据库 > 详细

20151204--JDBC

时间:2015-12-04 23:00:19      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
<%@ 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="post"   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>
注册页面
技术分享
package com.hanqi;

import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 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");
        
        //获取内容
        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");
                //连接URL
                String url = "jdbc:oracle:thin:@localhost:1521:orcl";
                //获取connection对象 (个人理解为登陆数据库 并将数据关联)
                Connection conn = DriverManager.getConnection(url,"hanlin","123654789");    //url,名称,密码
                //判断是否有数据
                if(conn != null)
                {
                    //操纵数据库
                    String sql = "insert into t_user (user_id,user_name,password) "
                            + "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");
                    //将操纵数据库中数据类名编下顺序便于输入
                    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
                {
                    
                }
                
            }
            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());
            }
            
            
            
        }
    }

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

}
关联数据库

技术分享

20151204--JDBC

标签:

原文地址:http://www.cnblogs.com/name-hanlin/p/5020647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!