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服务器 搭建--->详见
- mysql数据库服务器的搭建-->详见
注:服务器的搭建不是本文的重点
这里先介绍一下前端的JSP页面和Tomcat连接的相关知识点,这里谈谈个人的理解,首先JSP程序和tomcat通信要通过tomcat提供的连接池,tomcat可以在连接池中设置最大数量的连接,提供给JSP程序连接,连接池中的连接可以动态的释放与回收。但是连接池中提供的连接数要小于Mysql连接池的数量。
tomcat配置连接池
- tomcat连接池配置
- vi/vim server.xml
- Oracle数据库的连接池配置
- 在<host> </host>中配置如下信息
- <Resource
- auth="Container"
- description="sqlserver Datasource"
- name="jdbc/ora"
- type="javax.sql.DataSource"
- maxActive="50"
- maxIdle="10"
- username="" ---->连接数据库的用户名
- maxWait="10000"
- driverClassName="oracle.jdbc.driver.OracleDriver"
- password=""----->连接数据库的用户密码
- url="jdbc:oracle:thin:@host:port/databases"
- removeAbandoned="true"
- removeAbandonedTimeout="60"
- logAbandoned="true"/>
- MySQL数据库的连接池配置
- <Resource
- name="jdbc/TestDB"
- auth="Container" type="javax.sql.DataSource"
- maxActive="100"
- maxIdle="30"
- maxWait="10000"
- username="javauser"
- password="javadude"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/javatest"/>
- SQL的连接池配置
- <Resource
- auth="Container"
- description="sqlserver Datasource"
- name="jdbc/sqlserver110"
- type="javax.sql.DataSource"
- maxActive="100"
- maxIdle="10"
- username=""
- maxWait="10000"
- driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
- password=""
- url="jdbc:microsoft:sqlserver:IP(端口);数据库名字;"reconnect=true"
- removeAbandoned="true"
- removeAbandonedTimeout="60"
- logAbandoned="true" />
tomcat5.5参数解释:
- tomcat5.5参数说明:
- 1 maxActive: 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 表示不限制 注意数据的连接数要大于此连接数
- 2 maxIdle: 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
- 保持在连接中最大的闲置连接数(在连接池最大的空闲连接数)
- 3 maxWait: 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
- 4 driverClassName: 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
- <resource-ref>
- <description>Oracle Datasource example</description>
- <res-ref-name>jdbc/myoracle</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
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; +----+-------+-------+ | ID | FOO | BAR | +----+-------+-------+ | 1 | hello | 12345 | +----+-------+-------+ 1 row in set (0.00 sec) 注意:Create a new test user, a new database and a single test table. Your MySQL user must have a password assigned. The driver will fail if you try to connect with an empty password Note!!!: the above user should be removed once testing is complete!
JSP测试页面
- <%@ page import="java.sql.*" %>
- <%@ page contentType="text/html; charset=gb2312" %>
- <%@ page language="java" %>
- <%@ page import="com.mysql.jdbc.Driver" %>
- <%@ page import="java.sql.*" %>
- <%
- String driverName="com.mysql.jdbc.Driver";
- String userName="javauser";
- String userPasswd="java";
- String dbName="javatest";
- String tableName="testdata";
- String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection connection=DriverManager.getConnection(url);
- Statement statement = connection.createStatement();
- String sql="SELECT * FROM "+tableName;
- ResultSet rs = statement.executeQuery(sql);
- while (rs.next())
- {
- String foo = rs.getString("foo");
- String bar = rs.getString("bar");
- out.print(foo+" ");
- out.print(bar+" ");
- }
- rs.close();
- statement.close();
- connection.close();
- %>
- 上述代码仅是实现的一例。
四:测试