首页 / JAVA / 练习JavaWeb连接数据库
练习JavaWeb连接数据库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了练习JavaWeb连接数据库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4536字,纯文字阅读大概需要7分钟。
内容图文
![练习JavaWeb连接数据库](/upload/InfoBanner/zyjiaocheng/473/7ad7c6ef7c094f9d84233ccb0a769a03.jpg)
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连接数据库
标签:print ora string acl private 程序 test web line
本文系统来源:http://www.cnblogs.com/miracle-0807/p/6010757.html
内容总结
以上是互联网集市为您收集整理的练习JavaWeb连接数据库全部内容,希望文章能够帮你解决练习JavaWeb连接数据库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。