zi663227 发表于 2018-9-28 12:00:29

JSP操作MySQL数据库实例讲解

  JSP操作MySQL数据库实例讲解
  一:概述
  在开始介绍之前先谈谈为什么要写这片文章,个人认为作为一个运维工程师,我们要熟悉的知识网络,不仅仅要限于点知识的掌握,比如linux系统,web服务器的搭建,数据库等等,还要熟悉这些点组成的网络,确切的说,点之间的是怎么相互影响的,点与点之间怎么相互操作等等,比如在某个点出现问题时,我们可以系统的分析,最终查找到问题的根源。那么在web前端的JSP程序(或者PHP,ASP等)是怎么通过中间的程序与后台的数据库建立起一条线(这里我们暂且将JSP,tomcat,mysql称为一条所谓的线),怎么通信,怎么相互影响,这里涉及的内容太多了,限于个人水平有恨,仅介绍一下JSP怎么通过tomcat,连接后台的mysql数据库。
  二:拓扑图

  实验环境:Centos5.8(kernel 2.6.18)+tomcat5.5+mysql5.0
  三:JSP连接MySQL


[*]web服务器 搭建--->详见 http://haicang.blog.51cto.com/2590303/963670
[*]mysql数据库服务器的搭建-->详见http://haicang.blog.51cto.com/2590303/930541  


  注:服务器的搭建不是本文的重点
  

  这里先介绍一下前端的JSP页面和Tomcat连接的相关知识点,这里谈谈个人的理解,首先JSP程序和tomcat通信要通过tomcat提供的连接池,tomcat可以在连接池中设置最大数量的连接,提供给JSP程序连接,连接池中的连接可以动态的释放与回收。但是连接池中提供的连接数要小于Mysql连接池的数量。


  tomcat配置连接池
  


[*]tomcat连接池配置
[*]vi/vimserver.xml
[*]
[*]Oracle数据库的连接池配置
[*]在 中配置如下信息
[*]连接数据库的用户名         
[*]      maxWait="10000"         
[*]      driverClassName="oracle.jdbc.driver.OracleDriver"         
[*]      password=""----->连接数据库的用户密码         
[*]      url="jdbc:oracle:thin:@host:port/databases"         
[*]      removeAbandoned="true"         
[*]      removeAbandonedTimeout="60"         
[*]      logAbandoned="true"/>
[*]
[*]MySQL数据库的连接池配置
[*]
[*]
[*]
[*]SQL的连接池配置
[*]
  

  tomcat5.5参数解释:
  


[*]tomcat5.5参数说明:
[*]1maxActive: Maximum number of dB connections in pool. Make sure you
[*]            configure your mysqld max_connections large enough to handle
[*]            all of your db connections. Set to -1 for no limit
[*]         连接池中最大的连接数 设为-1 表示不限制注意数据的连接数要大于此连接数
[*]2maxIdle: Maximum number of idle dB connections to retain in pool.
[*]         Set to -1 for no limit.See also the DBCP documentation on this
[*]         and the minEvictableIdleTimeMillis configuration parameter
[*]         保持在连接中最大的闲置连接数(在连接池最大的空闲连接数)
[*]3maxWait: Maximum time to wait for a dB connection to become available
[*]         in ms, in this example 10 seconds. An Exception is thrown if
[*]         this timeout is exceeded.Set to -1 to wait indefinitely
[*]         等待一个连接成为可用连接的最大等待时间 单位毫秒ms
[*]4driverClassName: Class name for the old mm.mysql JDBC driver is
[*]         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
[*]         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
[*]5    url: The JDBC connection url for connecting to your MySQL dB
[*]6    removeAbandoned="true" (abandoned dB connections are removed and recycled)
[*]      解释:被遗弃的数据连接 回收到连接池中    默认为false
[*]7    removeAbandonedTimeout="60"(a dB connection has been idle before it is considered abandoned)单位秒
[*]      解释:在一个连接空闲多少秒会被遗弃
[*]8   logAbandoned="true"
[*]      记录被遗弃的数据连接 默认为false
  

  在web应用程序的目录下创建WEB-INF/web.xml,并添加如下内容
  


[*]web.xml configuration
[*]
[*] Oracle Datasource example
[*] jdbc/myoracle
[*] javax.sql.DataSource
[*] Container
[*]
  

  JSP连接数据库的用户
  


[*]MySQL configuration
[*]mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost
[*]    ->   IDENTIFIED BY 'javadude' WITH GRANT OPTION;
[*]mysql> create database javatest;
[*]mysql> use javatest;
[*]mysql> create table testdata (
[*]    ->   id int not null auto_increment primary key,
[*]    ->   foo varchar(25),
[*]    ->   bar int);
[*]mysql> insert into testdata values(null, 'hello', 12345);
[*]Query OK, 1 row affected (0.00 sec)mysql> select * from testdata; +----+-------+-------+ |>
  

  JSP测试页面
  


[*]
[*]
[*]
[*]
[*]
[*]
[*]上述代码仅是实现的一例。
  

  四:测试



页: [1]
查看完整版本: JSP操作MySQL数据库实例讲解