无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了jsp+jdbc实现连接数据库的方法。分享给大家供大家参考。具体如下:
初次尝试JSP+jdbc,按照书上的例子折腾了半天,就是连不上数据库。于是在网上找材料,终于发现,老的jar包与新版数据库直接不兼容。于是下了新的数据库jdbc包,试了一下,果然搞定。这里,把这个程序跟大家共享下,程序实现了网页登录界面上提取用户名与密码,然后与数据库中用户名密码对应,从而决定程序是否通过登录。
inc.jsp文件:
<%@ page import="java.sql.Connection"%> <%@ page import="java.sql.DriverManager"%> <%@ page import="java.sql.Statement"%> <%@ page import="java.sql.ResultSet"%> <%@ page import="java.sql.ResultSetMetaData"%> <% String drv = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/demo"; String usr = "nari"; String pwd = "nari"; %>
welcome.jsp文件:
<html> <body> welcome<br> </body> </html>
login_action.jsp文件:
<%@ include file="inc.jsp" %> <% String username = request.getParameter("username"); String password = request.getParameter("password"); if(username == null || password == null){ response.sendRedirect("index.jsp"); } boolean isValid = false; String sql = "select * from user where username='"+username+"'and password='"+password+"'"; out.println("===>"+sql); try{ Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url, usr,pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(rs.next())isValid = true; rs.close(); stm.close(); conn.close(); }catch(Exception e){ e.printStackTrace(); out.println(e); } if(isValid){ response.sendRedirect("welcome.jsp"); }else response.sendRedirect("index.jsp"); %> <% /* if(username.endsWith("a"))response.sendRedirect("welcome.jsp"); else response.sendRedirect("index.jsp"); */%>
index.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath %>"/> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="description" content="This is my page"> </head> <body> miThis is my JSP page.<br> </body> </html> <form name="form1" action="login_action.jsp" method="post"> <table width="200" border="1"> <tr> <td colspan="2">登录窗口</td> </tr> <tr> <td>用户名</td> <td><input type="text" name="username" size="10"/></td> </tr> <tr> <td>密码</td> <td><input type ="password" name="password" size="10"/></td> </tr> <tr> <td colspan="2"><input type="submit" name="submit" value="登录"> <a href="register.jsp">注册新用户</a></td> </tr> </table> </form>
程序使用tomcat进行发布,myeclipse进行编辑和调试
希望本文所述对大家的jsp程序设计有所帮助。
标签:
jsp,jdbc,连接,数据库
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 群星.2022-一不小心喵上你原声大碟【TME影音】【FLAC分轨】
- 《战锤40K星际战士2》卡在正在加入服务器解决方法
- 《勇者斗恶龙怪物仙境3》玩法内容介绍
- 《幸福工厂》无限报错解决方法
- 交错战线原始交易所推荐角色一览
- 战锤40K星际战士2战术职业介绍|战术职业技能效果一览
- 战锤40K星际战士2突击职业介绍|突击职业技能效果一览
- [妙音金曲]群星《悲情咖啡屋》(黑胶)2CD[DTS-WAV]
- 阿兰·达瓦卓玛《A-Lan阿兰唯美歌姬》2CD[DTS-WAV]
- 【小提琴】陈立新《思乡曲》2004[FLAC+CUE]
- 《战地》新作明年初大规模测试!EA已内部测试一年
- 《GTAOL》PC版时隔多年更新反作弊!小助手宣布跑路
- EA称AI是其业务核心!能提高开发效率、节约成本
- 卫华.1990-太阳升【BMG】【WAV+CUE】
- 呼吸乐队.1992-THEBREATHING【深飞】【WAV+CUE】