练习JavaWeb连接数据库
1.添加jar包。
建立动态java项目,在Web-INF文件夹下的lib文件夹里添加jar包。
区别:java中添加的jar包需要添加构建路径,而javaWeb中添加的jar不需要构建路径,自动添加。
2.创建请求页面
<%@ 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>Insert title here</title>
</head>
<body>
<form action="Check.jsp" method="post">
卡号:<input type="text" name="cardid"><br>
密码:<input type="password" name="password"><br>
按钮:<input type="submit" value="登录">
</form>
</body>
</html>
3.创建接收页面
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
//接收验证
String cardid = request.getParameter("cardid");
String password = request.getParameter("password");
//数据验证
if(cardid.equals("")||password.equals(""))
{
out.write("请正确登录系统");
}
else if(cardid==null||password==null)
{
out.write("请正确登录系统");
}
else
{
out.write(cardid);
out.write(password); //连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test0816", "laoer123");
PreparedStatement ps = conn.prepareStatement("select * from t_balance where card_id=? and password=? ");
ps.setString(1, cardid);
ps.setString(2,password);
ResultSet rs =ps.executeQuery();
//数据验证
if(rs.next())
{
out.write("用户名和密码正确");
}
else
{
out.write("用户名或密码错误");
}
rs.close();
ps.close();
conn.close();
} %>
</body>
</html>
4.通过连接池连接数据库
(1)添加jar包,同上。
(2)在JavaResources - src- 创建java包 -创建类 ,并将配置文件放在src下
package com.hanqi.web; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource;
//单例模式
public class CardDAO {
//构建连接池对象并配置
private ComboPooledDataSource cpds =new ComboPooledDataSource("helloc3p0"); public boolean checklogin(String cardid,String password)
{
boolean rtn =false;
try {
Connection conn = cpds.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from t_balance where card_id=? and password=? ");
ps.setString(1, cardid);
ps.setString(2,password);
ResultSet rs =ps.executeQuery(); rtn=rs.next();
rs.close();
ps.close(); conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rtn;
} }
请求信息页面
<%@ 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>Insert title here</title>
</head>
<body>
<form action="Check2.jsp" method="post">
卡号:<input type="text" name="cardid"><br>
密码:<input type="password" name="password"><br>
按钮:<input type="submit" value="登录">
</form>
</body>
</html>
接收信息的页面
<%@page import="com.hanqi.web.CardDAO"%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
// 设置不缓存页面
response.setHeader("Cache-Control", "no-cache");
//定时跳转
//response.setHeader("refresh", "2;URL=http://www.baidu.com");
// String cardid = request.getParameter("cardid");
String password =request.getParameter("password");
if(cardid==null||password==null||
cardid.equals("")||password.equals(""))
{
out.write("请正确登录");
}
else
{
//检查登录信息
CardDAO cd = new CardDAO();
if(cd.checklogin(cardid, password))
{
//out.write("登录成功!"); response.getWriter().write("验证成功!");
//页面跳转
//response.sendRedirect("Main.jsp");
response.sendRedirect("http://www.baidu.com");
}
else
{
out.write("登录失败!");
//跳回登录页面
response.setHeader("refresh", "2;URL=loginin.jsp");
}
} %> </body>
</html>
附原本的图,不是以上程序




练习JavaWeb连接数据库的更多相关文章
- javaweb连接数据库并完成增删改查
一.连接数据库 1.mysql数据库的安装和配置 在网上找到了篇关于mysql的安装详细说明,供读者自己学习 https://www.jb51.net/article/23876.htm 2.mysq ...
- 用javaweb连接数据库用javabean、severlet实现增删改查
样 很重要的一点是建立数据库的连接 数据库是一切操作的前提 不管是增加 删除 修改 查询 都需要调用数据库连接程序 再就是java的类的编写 写完类后需要对其进行增删改查方法的 编写 这是dao层的 ...
- Javaweb连接数据库
在JSP中使用JDBC驱动连接mysql数据库. 1: 下载mysql的Java连接程序 2: 解压目录下的mysql-connector-java-5.0.24-bin.jar文件就是连接MySql ...
- psp 周计划2
日期\时间 开始时间 结束时间 中断时间 净时间 活动 备注 12/3 9:00 11:30 10:30 120分钟 自习,练习 教室 14:00 16:30 15:30 80分钟 练习 中午休息 1 ...
- 传智播客JavaWeb day10-jdbc操作mysql、连接数据库六大步骤
第十天主要讲了jdbc操作mysql数据库,包括连接数据库六大步骤(注册数据库驱动.获得连接对象connetion.生成传输器stament.执行查询获得ResultSet.遍历结果集.关闭资源).介 ...
- javaweb之连接数据库
最近做完了一个图书系统的增删改查,想着来总结一下这几个月的所学内容. 一.首先你需要在电脑上安装上mysql或者sql server(本文以mysql为例) mysql官网:MySQL :: Begi ...
- javaWeb 数据库连接池连接数据库
需要的公共jar包 mysql-connector-java-5.0.8-bin.jar(mysql数据库) ojdbc14.jar(oracle数据库) A.DBCP 需要jar包: commo ...
- javaweb登录界面连接数据库
实验关键截图 数据库界面 建表 2.登录界面 登陆失败 5 注册页面 5 注册成功 数据库截图
- JavaWeb 08_JSP+Dao+Bean+Servlet 实现登录注册(连接数据库,验证码登录,两周内免登陆等功能)
一.数据库db_01 表usert 字段username,password 二. 目录 三. 配置信息 四. 代码 index.jsp <script type="text/j ...
随机推荐
- CentOS 6.5 安装CodeBlocks
CentOS 6.5 安装CodeBlocks 经历了无数痛苦的折磨,Linux上的库依赖真是能把人逼疯,终于在Google上找到了办法,勉强将CodeBlocks安装成功. 1.为了避免最新版本的C ...
- CodeBlocks 中fopen函数不支持命令 “r”
//codeblocks #include<stdio.h> #include<stdlib.h> void main(void) { FILE *fp=NULL; if((f ...
- HDU 1796 容斥原理
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- ssh隧道(端口转发)
本地转发: ssh -Nf -L [bind_address:]port:host:hostport sshServer -Nf 后台运行 -L 本地转发 [bind_address] 绑定本地地址, ...
- C语言 gets()和scanf()函数的区别
scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用 gets 函数. gets可以接收空格:而sc ...
- supersocket+controller+action
public class MasterServer : SuperSocket.SocketBase.AppServer<MasterSession> { } public class M ...
- 7、java实现的两种单例模式
/* 两种单例模式的演示 */ //饿汉式 class Signal { private Signal(){} private Signal s = new Signal(); public stat ...
- 虚拟机Ubuntu16,caffe环境搭建
虚拟机下的Ubuntu16.04+caffe+onlycup 官网的step很重要,要跟着官网,的步骤来:http://caffe.berkeleyvision.org/installation.ht ...
- ImageLoader介绍2
Universal Image Loader 是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示.所以,如果你的程序里需要这个功能的话,那么不妨试试它.他本来是 ...
- Linux中cp和scp命令的使用方法
Linux为我们提供了两个用于文件copy的命令,一个是cp,一个是scp,但是他们略有不同. cp --- 主要是用于在同一台电脑上,在不同的目录之间来回copy文件 scp --- 主要是在不同的 ...