900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Jsp连接Mysql数据库:利用Servlet监听器【MySQL】

Jsp连接Mysql数据库:利用Servlet监听器【MySQL】

时间:2019-04-24 01:56:44

相关推荐

Jsp连接Mysql数据库:利用Servlet监听器【MySQL】

数据库|mysql教程

官方网站,数据库,运行环境

数据库-mysql教程

本例程所使用的运行环境及资源:

1. Tomcat 5.5

2. Mysql 4.1

3.mysql jdbc驱动:3.1.13

(以上程序可以直接到相关官方网站下载到)

step1:首先创建一个监听器servlet,这个监听器用来初始化共享资源,本例中主要用来在应用中创建数据源,把这个类置于WEB-INF/classes/kinglong/jmediasoft/servlets/目录下,代码如下:

/** */ /**

* @(#)ResourceManagerListener.java

*

* 资源初始化监听器

*

packagekinglong.jmediasoft.servlets;

importjavax.servlet. * ;

importjavax.servlet.http. * ;

importcom.mysql.jdbc.jdbc2.optional. * ;

public classResourceManagerListenerimplementsServletContextListener{

privateDataSource ds = null ;

public voidcontextInitialized(ServletContextEvent sce){

ServletContext application = sce.getServletContext();

String jdbcurl = application.getInitParameter( ” jdbcURL ” );

String user = application.getInitParameter( ” user ” );

String password = application.getInitParameter( ” password ” );

try{

ds = newMysqlConnectionPoolDataSource();

ds.setUrl(jdbcurl);

ds.setUser(user);

ds.setPassword(password);

} catch (Exception e){

application.log( ” 无法创建数据源: ” + e.getMessage());

return ;

}

application.setAttribute( ” dataSource ” ,ds);//将数据源变量置于应用作用域中

}

public voidcontextDestroyed(ServletContextEvent sce){

ServletContext application = sce.getServletContext();

application.removeAttribute( ” dataSource ” );

ds = null ;

}

}

step2:配置web.xml相关代码,代码如下:

jdbcURL

jdbc:mysql://localhost:3306/comic

user

root

password

root

kinglong.jmediasoft.servlets.ResourceManagerListener

step3:建立测试页面,测试页面代码如下:

< sql:queryvar ="comicinfo"dataSource ="$ { dataSource}"

sql =”SELECT * FROM comicinfo” />

数据库测试页面<p><table><p><tr><p><td>$ { fn:escapeXml(column.key)}:<p></td><td>$ { fn:escapeXml(column.value)}<p>step4:重新启动Tomcat应用,用http://localhost:8080/test/test.jsp的链接来执行此页面,我的输出如下。这是我的comicinfo表中的唯一一条记录。黑体是表的字段,后面是对应的字段值。</p><p>红客论坛源码,vscode 光标错位,ubuntu execl,npm和tomcat,sqlite是什么牌子,ecshop 配送方式插件,新手引导界面前端框架,爬虫怎么获取cookie值,php 阶乘,seo黑帽,赌博兼职网站源码,网页监控代码,ecstore模板开发lzw</p><p></p></p> </div> <div class="keywords"> </div> <div class="tips">本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。</div> </div> <script>news_one();</script> <dl class="plbox" id="comments"> <dt>网友评论</dt> <dd> <textarea class="plcc">文明上网理性发言</textarea> <button class="sendpl">发表评论</button> <div class="pl_tips">网友评论仅供其表达个人看法,并不表明网站立场。</div> <div class="b10"></div> <ol class="pllist"> </ol> </dd> </dl> <script> $(".plcc").focus(function(){ if($('.plcc').val()=='文明上网理性发言') $('.plcc').val(''); }); $(".sendpl").click(function(){ var plcc=$('.plcc').val(),plcs=$('<p>'+plcc+'</p>').text(); if(plcs!='' && plcs!='文明上网理性发言'){ $('.pllist').prepend('<li><img src="https://900zi.500zi.com/statics/images/member/avatar.jpg" /><span class="adtime">2024-08-15 15:21</span><span class="plname">匿名59077468</span><span class="ipadd">[网友]<i>0.0.0.0</i></span><div class="pldata">'+plcs+' <font color="gray">(审核中)</font></div><div class="support"><span class="s1">顶0</span><span class="s2">踩0</span></div><div class="clear"></div></li>'); $.post("https://900zi.500zi.com/comment.php",{"commentid":"content_1-67806815-1","title":"Jsp%E8%BF%9E%E6%8E%A5Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%EF%BC%9A%E5%88%A9%E7%94%A8Servlet%E7%9B%91%E5%90%AC%E5%99%A8%E3%80%90MySQL%E3%80%91","content":plcs,"token":"8c3872a8bcc023c474c0b859bbf188e5"}); } }); </script> <script>show(3);</script> <div class="list mt20"> <div class="listTit"><span></span>相关阅读</div> <dl> <dt><a href="https://900zi.500zi.com/art/15/883/PnxmZqjjN3.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/15/883/36f7a0895954094e4f0a6e242a6d3da0.jpg" alt="c mysql 工具类_Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/art/15/883/PnxmZqjjN3.html" target="_blank">c mysql 工具类_Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类</a></p> <p class="des"></p> <p class="key"> <span>2019-09-20</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/art/14/837/1LzW3owRny.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/14/837/da7f7468ac2768b02595e62fd3496bac.jpg" alt="[JSP暑假实训] 三.MySQL数据库基本操作及Servlet网站连接显示数据库信息" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/art/14/837/1LzW3owRny.html" target="_blank">[JSP暑假实训] 三.MySQL数据库基本操作及Servlet网站连接显示数据库信息</a></p> <p class="des"></p> <p class="key"> <span>2021-12-25</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/art/14/559/8nroAwm5NM.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/14/559/e002d2f000ff7f33d8be4aa5fc7ce9ec.jpg" alt="JSP_实现Servlet登陆 注册 链接Mysql数据库。【MySQL】" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/art/14/559/8nroAwm5NM.html" target="_blank">JSP_实现Servlet登陆 注册 链接Mysql数据库。【MySQL】</a></p> <p class="des"></p> <p class="key"> <span>2019-03-16</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/art/15/928/1pPMeVXDL4.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/15/928/469fa02539d663568caee376a9058120.jpg" alt="利用jsp和servlet MySQL实现简易报表" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/art/15/928/1pPMeVXDL4.html" target="_blank">利用jsp和servlet MySQL实现简易报表</a></p> <p class="des"></p> <p class="key"> <span>2019-02-05</span> <span></span> </p> </dd> </dl> </div> </div> <div class="sideRight"> <script>news_two();</script> <div class="rightNews mt20"> <div class="rightTit"><span></span>扩展阅读</div> <div class="rightNewsList"> <dl> <dt><a href="https://www.zwen.com.cn/zs/5923702.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/pic/335.jpg" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://www.zwen.com.cn/zs/5923702.html" target="_blank"><em>利用</em>SQL对<em>MySQL</em><em>数<em>据库</em></em>进行操作:从入门到精通</a></p> </dd> </dl> <dl> <dt><a href="https://www.dtxt.com.cn/d/2023/09/11/167935.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/pic/383.jpg" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://www.dtxt.com.cn/d/2023/09/11/167935.html" target="_blank">知识:<em>MySQL</em><em>数<em>据库</em></em>语句实例</a></p> </dd> </dl> <dl> <dt><a href="https://www.qwen.cn/html/202012/01/1523765.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/pic/180.jpg" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://www.qwen.cn/html/202012/01/1523765.html" target="_blank"><em>MySQL</em><em>数<em>据库</em></em>操作语法详解</a></p> </dd> </dl> <dl> <dt><a href="https://www.qwen.cn/html/202012/03/1549529.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/pic/341.jpg" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://www.qwen.cn/html/202012/03/1549529.html" target="_blank">如何搭建<em>MySQL</em><em>数<em>据库</em></em>系统</a></p> </dd> </dl> <dl> <dt><a href="https://www.dtxt.com.cn/d/2023/12/29/246930.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/pic/285.jpg" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://www.dtxt.com.cn/d/2023/12/29/246930.html" target="_blank"><em>mysql</em><em>数<em>据库</em></em>select查询语句</a></p> </dd> </dl> <dl> <dt><a href="https://www.juzii.com.cn/hjz/201207/04/1639302.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/pic/656.jpg" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://www.juzii.com.cn/hjz/201207/04/1639302.html" target="_blank"><em>MySQL</em><em>数<em>据库</em></em>之修改表结构</a></p> </dd> </dl> </div> </div> <div class="rightNews mt20"> <div class="rightTit"><span></span>最近发布</div> <div class="rightNewsList"> <dl> <dt><a href="https://900zi.500zi.com/html/2024/0815/bGLB3DZne8.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/2024/08/15/f8fa8d94621250c2c7d8c8eae33447bf.jpg" alt="念想900字作文素材大全" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/html/2024/0815/bGLB3DZne8.html" target="_blank">念想900字作文素材大全</a></p> <p class="key"> <span>2024-08-15</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/html/2024/0815/VPnx6dxp34.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/2024/08/15/93d10f60e0e87e67407c2284a2252657.jpg" alt="端午节趣事作文900字" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/html/2024/0815/VPnx6dxp34.html" target="_blank">端午节趣事作文900字</a></p> <p class="key"> <span>2024-08-15</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/html/2024/0815/WEpGo69p4j.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/2024/08/15/575cadfbf943ed7278ce1669c44c982d.jpg" alt="学生记事900字作文素材大全" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/html/2024/0815/WEpGo69p4j.html" target="_blank">学生记事900字作文素材大全</a></p> <p class="key"> <span>2024-08-15</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/html/2024/0815/DrNgo58N1m.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/2024/08/15/06a2e5dc89afb979002353935fed554a.jpg" alt="900字优美议论文高中" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/html/2024/0815/DrNgo58N1m.html" target="_blank">900字优美议论文高中</a></p> <p class="key"> <span>2024-08-15</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/html/2024/0815/mlp9JwZLe1.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/2024/08/15/6c165e30b662b41b1a0b70d613cb8f26.jpg" alt="心愿 900字优秀作文" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/html/2024/0815/mlp9JwZLe1.html" target="_blank">心愿 900字优秀作文</a></p> <p class="key"> <span>2024-08-15</span> <span></span> </p> </dd> </dl> <dl> <dt><a href="https://900zi.500zi.com/html/2024/0815/6xLOZk9p04.html" target="_blank"><img src="https://900zi.500zi.com/uploadfile/img/2024/08/15/6180a082fb1656ec9324908b885bae3b.jpg" alt="扇子里的温情:一段动人的故事" width="230" height="150" /></a></dt> <dd> <p class="tit"><a href="https://900zi.500zi.com/html/2024/0815/6xLOZk9p04.html" target="_blank">扇子里的温情:一段动人的故事</a></p> <p class="key"> <span>2024-08-15</span> <span></span> </p> </dd> </dl> </div> </div> <div class="rightClass mt20"> <div class="rightTit"><span></span>推荐专题</div> <div class="txt"> <a href="https://900zi.500zi.com/zhuanti/1/0/twyzdzw900z.html" title="托物言志的作文900字">托物言志的作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/jts900z.html" title="检讨书900字">检讨书900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/lyetjzw900z.html" title="六一儿童节作文900字">六一儿童节作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/yjzw900zjxw.html" title="遇见作文900字记叙文">遇见作文900字记叙文</a> <a href="https://900zi.500zi.com/zhuanti/1/0/lswxdns900z.html" title="老师我想对你说900字">老师我想对你说900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/yjzw900z591.html" title="游记作文900字">游记作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/kzxzdhzw900z.html" title="开在心中的花作文900字">开在心中的花作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/dwzw900z.html" title="端午作文900字">端午作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/zj900zgzzw.html" title="周记900字高中作文">周记900字高中作文</a> <a href="https://900zi.500zi.com/zhuanti/1/0/zw900z.html" title="作文900字">作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/jxxdth900z.html" title="军训心得体会900字">军训心得体会900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/mw900z.html" title="美文900字">美文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/jjsqdzw900z.html" title="借景抒情的作文900字">借景抒情的作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/wdmq900z.html" title="我的母亲900字">我的母亲900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/zj900z.html" title="周记900字">周记900字</a> </div> </div> </div> </div> <script>show(4);</script> <div class="footer"> <div class="footLinks"> <div class="footLink"> <div class="tit">猜你喜欢:</div> <div class="txt txtTwo"> <a href="https://900zi.500zi.com/zhuanti/1/0/czzw900z.html" title="初中作文900字">初中作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/gzyxzw900z.html" title="高中优秀作文900字">高中优秀作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/jxxdth900z.html" title="军训心得体会900字">军训心得体会900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/whty900zzw.html" title="我和体育900字作文">我和体育900字作文</a> <a href="https://900zi.500zi.com/zhuanti/1/0/wxmzdyx900z.html" title="我心目中的英雄900字">我心目中的英雄900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/jxw900z.html" title="记叙文900字">记叙文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/xsdzw900z.html" title="写事的作文900字">写事的作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/900zjts.html" title="900字检讨书">900字检讨书</a> <a href="https://900zi.500zi.com/zhuanti/1/0/xyjdhg900z.html" title="西游记读后感900字">西游记读后感900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/shcdhg900z.html" title="水浒传读后感900字">水浒传读后感900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/hjfzw900z.html" title="好家风作文900字">好家风作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/gycxdylw900z.html" title="关于诚信的议论文900字">关于诚信的议论文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/wdmm900z.html" title="我的妈妈900字">我的妈妈900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/gqjzw900z.html" title="国庆节作文900字">国庆节作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/jxgx900z.html" title="军训感想900字">军训感想900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/yjzw900zjxw.html" title="遇见作文900字记叙文">遇见作文900字记叙文</a> <a href="https://900zi.500zi.com/zhuanti/1/0/jxxd900z.html" title="军训心得900字">军训心得900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/dhg900z.html" title="读后感900字">读后感900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/nwdnykzw900z.html" title="难忘的那一刻作文900字">难忘的那一刻作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/gzjxw900z.html" title="高中记叙文900字">高中记叙文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/lbxpljdhg900z.html" title="鲁滨逊漂流记读后感900字">鲁滨逊漂流记读后感900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/wzjpdr900z.html" title="我最敬佩的人900字">我最敬佩的人900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/900zzw.html" title="900字作文">900字作文</a> <a href="https://900zi.500zi.com/zhuanti/1/0/fljsdhg900z.html" title="傅雷家书读后感900字">傅雷家书读后感900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/sw900z.html" title="散文900字">散文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/xzdygzw900z.html" title="心中的阳光作文900字">心中的阳光作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/gddzw900z.html" title="感动的作文900字">感动的作文900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/900zsb.html" title="900字随笔">900字随笔</a> <a href="https://900zi.500zi.com/zhuanti/1/0/y900z.html" title="雨900字">雨900字</a> <a href="https://900zi.500zi.com/zhuanti/1/0/yjzw900z591.html" title="游记作文900字">游记作文900字</a> </div> <div class="more">展开</div> </div> </div> <div class="copyRight pc"> <p class="left">900字范文 免责声明<span></span>© 2024 All Rights Reserved.</p> <p class="right"><a href="http://beian.miit.gov.cn/" target="_blank" rel="external nofollow">湘ICP备2024057051号</a><span></span><a href="/sitemap.html" target="_blank">网站地图</a><span></span><a href="/sitemap.xml" target="_blank">XML</a><span></span></p> </div> <div class="copyRight mobile"> <p>© 2024 All Rights Reserved.</p> <p><a href="/">900字范文</a> 免责声明<span></span></p> </div> </div> <div id="backTop" class="none"></div> <script type="text/javascript">op('content',67806815,1);</script> </body> </html>