<%@ 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的更多相关文章

  1. Java数据库连接技术——JDBC

    大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...

  2. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  3. [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...

  4. JDBC MySQL 多表关联查询查询

    public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...

  5. JDBC增加删除修改

    一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...

  6. JDBC简介

    jdbc连接数据库的四个对象 DriverManager  驱动类   DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...

  7. JDBC Tutorials: Commit or Rollback transaction in finally block

    http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...

  8. FineReport如何用JDBC连接阿里云ADS数据库

    在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...

  9. JDBC基础

    今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...

  10. Spring学习记录(十四)---JDBC基本操作

    先看一些定义: 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1.core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJ ...

随机推荐

  1. C#中Linq延迟执行问题

    本文来自:http://msdn.microsoft.com/zh-cn/library/bb399393(v=vs.110).aspx http://www.cnblogs.com/zhanglin ...

  2. jquery第三期:js与jquery对象转换

    我们开始进入jquery的学习了,jquery的学习就不那么中规中矩了,我们来看一个和javascript有所区别的地方. <!DOCTYPE html PUBLIC "-//W3C/ ...

  3. hdu1814 Peaceful Commission,2-sat

    题目大意:一国有n个党派.每一个党派在议会中都有2个代表,现要组建和平委员会,要从每一个党派在议会的代表中选出1人,一共n人组成和平委员会.已知有一些代表之间存在仇恨,也就是说他们不能同一时候被选为和 ...

  4. Hortonworks HDP Sandbox定制(配置)开机启动服务(组件)

    定制Hortonworks HDP开机启动服务能够这样做:本文原文出处: http://blog.csdn.net/bluishglc/article/details/42109253 严禁不论什么形 ...

  5. java SecurityManager

    ---- 众所周知,Java语言具有完善的安全框架,从编程语言,编译器.解释程序到Java虚拟机,都能确保Java系统不被无效的代码或敌对的编译器暗中破坏,基本上,它们保证了Java代码按预定的规则运 ...

  6. javascript设计模式——Publish/Subscribe

    推荐阅读http://dev.housetrip.com/2014/09/15/decoupling-javascript-apps-using-pub-sub-pattern/ 我们先引出问题的所在 ...

  7. python 多线程爬虫

    最近,一直在做网络爬虫相关的东西. 看了一下开源C++写的larbin爬虫,仔细阅读了里面的设计思想和一些关键技术的实现. 1.larbin的URL去重用的很高效的bloom filter算法: 2. ...

  8. Scala基础入门-3

    学习Scala——映射和元组 映射和和元组,也就是Maps和Tuples.Map这东西应该都挺明白的,就是键值对的集合.而元组,tuple,这东西并不是每个语言都有(Python中是有的,不过当时学的 ...

  9. 【转】MUD教程--巫师入门教程4

    我们再次复习一下clean_up()函数返回1的含义,如果clean_up()函数返回1,则MUDOS在这一次的调用时不会做其的任何举动,但到了下一次想调用的时间里,还将再次调用这个对象的clean_ ...

  10. In Depth : Android Shutdown Sequence

    What happened when I long press power button ?What is shutdown sequence ?How is it different from de ...