20151204--JDBC
<%@ 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的更多相关文章
- Java数据库连接技术——JDBC
大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...
- 玩转spring boot——结合AngularJs和JDBC
参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...
- JDBC MySQL 多表关联查询查询
public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...
- JDBC增加删除修改
一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...
- JDBC简介
jdbc连接数据库的四个对象 DriverManager 驱动类 DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...
- JDBC Tutorials: Commit or Rollback transaction in finally block
http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...
- FineReport如何用JDBC连接阿里云ADS数据库
在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...
- JDBC基础
今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...
- Spring学习记录(十四)---JDBC基本操作
先看一些定义: 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1.core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJ ...
随机推荐
- OpenCms创建站点过程图解——献给OpenCms的刚開始学习的人们
非常多人都听说了OpenCms,知道了它的强大,索性的下载安装了,最终见到了久违OpenCms,看到了它简洁的界面,欣喜过后却不免一脸茫然,这个东西怎么用,我怎么用它来建站,从哪開始,无从下手,找资料 ...
- 【收藏】十大Webserver漏洞扫描工具
如今有很多消息令我们感到Web的危急性,因此,当前怎样构建一个安全的Web环境成为网管员和安全管理员们义不容辞的责任.可是巧妇难为无米之炊,该选择哪些安全工具呢? 扫描程序能够在帮助造我们造就安全的W ...
- linux-telnet服务配置
Telnet服务的配置:一.安装telnet软件包(通常要两个)1. telnet-client (或 telnet),这个软件包提供的是 telnet 客户端程序: 2. telnet-server ...
- LeetCode Day2
Power of Two /** * LeetCode: Power of Two * Given an integer, write a function to determine if it is ...
- 只获取linux ip的命令
ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'
- 05JS高级 方法没有块级作用域
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 移动端影像解决方案Adobe Creative SDK for ios
移动端影像解决方案Adobe Creative SDK for ios 2015-12-20 分类:整理 阅读(390) 评论(0) 老牌影像界泰斗不甘落寞,正式推出了Adobe Creative ...
- (转载)iscroll.js的使用
入门 Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化.每个页面上的iScroll实例数目在设备的CPU和内存能承受的范围内是没有限制的. 尽可能保持DOM结构的简洁.iScroll使用 ...
- ZRender源码分析5:Shape绘图详解
回顾 上一篇说到:ZRender源码分析4:Painter(View层)-中,这次,来补充一下具体的shape 关于热区的边框 以圆形为例: document.addEventListener('DO ...
- mongo设计(二)
原文:http://blog.mongodb.org/post/87892923503/6-rules-of-thumb-for-mongodb-schema-design-part-2 By Wil ...