首页 >

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

数据库|mysql教程Jsp连接Mysql数据库:利用Servlet监听器【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
*
* 资源初始化监听器
*
package kinglong.jmediasoft.servlets;
import javax.servlet. * ;
import javax.servlet.http. * ;
import com.mysql.jdbc.jdbc2.optional. * ;
public  class ResourceManagerListener implements ServletContextListener {
private DataSource ds = null ;
public  void contextInitialized(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
String jdbcurl = application.getInitParameter( ” jdbcURL ” );
String user = application.getInitParameter( ” user ” );
String password = application.getInitParameter( ” password ” );
try {
ds = new MysqlConnectionPoolDataSource();
ds.setUrl(jdbcurl);
ds.setUser(user);
ds.setPassword(password);
} catch (Exception e) {
application.log( ” 无法创建数据源: ” + e.getMessage());
return ; 
} 
application.setAttribute( ” dataSource ” ,ds);//将数据源变量置于应用作用域中
}
public  void contextDestroyed(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:query var ="comicinfo" dataSource ="$ { dataSource}" 
sql =”SELECT * FROM comicinfo”  /> 
数据库测试页面 <br /><table><br /><tr><br /><td><b> $ { fn:escapeXml(column.key)}: <br /><td> $ { fn:escapeXml(column.value)}</p><p>step4:重新启动Tomcat应用,用http://localhost:8080/test/test.jsp的链接来执行此页面,我的输出如下。这是我的comicinfo表中的唯一一条记录。黑体是表的字段,后面是对应的字段值。<div class="khw">红客论坛源码,vscode 光标错位,ubuntu execl,npm和tomcat,sqlite是什么牌子,ecshop 配送方式插件,新手引导界面前端框架,爬虫怎么获取cookie值,php 阶乘,seo黑帽2022,赌博兼职网站源码,网页监控代码,ecstore模板开发lzw</div> <div class="tag"> <a href="https://www.liuzhongwei.com/tags/x120789" rel="tag">运行环境</a></div> </div><!-- .entry-content --> <hr> <header class="lbox"> <div class="post-content"> <span style="float:left;padding-right:10px;"><a href="https://www.liuzhongwei.com/page/60300.html" rel="bookmark" title="MySQL创建用户与授权【MySQL】"> <img src="https://liuzhongwei2023.oss-cn-shanghai.aliyuncs.com/uploads/wpic/w06981.jpg" alt="Jsp连接Mysql数据库:利用Servlet监听器【MySQL】" /> </a></span> <li><a href="https://www.liuzhongwei.com/page/60300.html" rel="bookmark" title="MySQL创建用户与授权【MySQL】">MySQL创建用户与授权【MySQL】</a></li> <p>MySQL创建用户与授权【MySQL】 | MySQL创建用户与授权【MySQL】 ... </p> </div> </header> <header class="lbox"> <div class="post-content"> <span style="float:left;padding-right:10px;"><a href="https://www.liuzhongwei.com/page/398143.html" rel="bookmark" title="解决JavaScript运行环境"> <img src="https://liuzhongwei2023.oss-cn-shanghai.aliyuncs.com/uploads/wpic/w06981.jpg" alt="Jsp连接Mysql数据库:利用Servlet监听器【MySQL】" /> </a></span> <li><a href="https://www.liuzhongwei.com/page/398143.html" rel="bookmark" title="解决JavaScript运行环境">解决JavaScript运行环境</a></li> <p>解决JavaScript运行环境 | 解决JavaScript运行环境 ... </p> </div> </header> <header class="lbox"> <div class="post-content"> <span style="float:left;padding-right:10px;"><a href="https://www.liuzhongwei.com/page/363678.html" rel="bookmark" title="怎么配置php运行环境"> <img src="https://liuzhongwei2023.oss-cn-shanghai.aliyuncs.com/uploads/wpic/w06981.jpg" alt="Jsp连接Mysql数据库:利用Servlet监听器【MySQL】" /> </a></span> <li><a href="https://www.liuzhongwei.com/page/363678.html" rel="bookmark" title="怎么配置php运行环境">怎么配置php运行环境</a></li> <p>怎么配置php运行环境 | 怎么配置php运行环境 ... </p> </div> </header> <footer class="entry-footer"> <span class="cat-links">Posted in 未分类</span> | <span class="tags-links">Tagged <a href="https://www.liuzhongwei.com/tags/x120789" rel="tag">运行环境</a></span> </footer><!-- .entry-footer --> <!-- #post-## --> <nav class="navigation post-navigation" aria-label="文章"> <h2 class="screen-reader-text">文章导航</h2> <div class="nav-links"><div class="nav-previous"><a href="https://www.liuzhongwei.com/page/1257724.html" rel="prev">mysql恢复到指定时间 |linux 联接 mysql</a></div><div class="nav-next"><a href="https://www.liuzhongwei.com/page/1658.html" rel="next">[Linux] cpupower命令:调整CPU主频参数</a></div></div> </nav> </main><!-- #main --> </section><!-- #primary --> <aside id="secondary" class="widget-area col-sm-12 col-md-12 col-lg-4" role="complementary"> <hr> <section id="block-4" class="widget widget_block"> <hr class="wp-block-separator has-alpha-channel-opacity"/> </section></aside><!-- #secondary --> </div><!-- .row --> </div><!-- .container --> </div><!-- #content --> <footer id="colophon" class="site-footer" role="contentinfo"> <div class="container"> <div class="site-info"> © 牛的日记 | www.liuzhongwei.com <br> 网站部分内容来源于网友供稿,若有侵权请联系删除,970928#QQ.com<br> </div><!-- close .site-info --> </div> </footer><!-- #colophon --> </div><!-- #page --> <!--colorcode_start--> <script type="text/javascript" src="https://www.liuzhongwei.com/wp-content/plugins/colorcode/js/wp-colorcode.js?ver=20121102"></script> <script type="text/javascript"> window.onload = function() { DlHighlight.HELPERS.highlightByName('colorcode', 'pre','', false); } </script> <!--colorcode_end--> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?362c2492cf3ac506a1c032629e391abd"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>