JSP操作MySQL資料庫執行個體講解
一:概述
在開始介紹之前先談談為什麼要寫這片文章,個人認為作為一個運維工程師,我們要熟悉的知識網絡,不僅僅要限于點知識的掌握,比如linux系統,web伺服器的搭建,資料庫等等,還要熟悉這些點組成的網絡,确切的說,點之間的是怎麼互相影響的,點與點之間怎麼互相操作等等,比如在某個點出現問題時,我們可以系統的分析,最終查找到問題的根源。那麼在web前端的JSP程式(或者PHP,ASP等)是怎麼通過中間的程式與背景的資料庫建立起一條線(這裡我們暫且将JSP,tomcat,mysql稱為一條所謂的線),怎麼通信,怎麼互相影響,這裡涉及的内容太多了,限于個人水準有恨,僅介紹一下JSP怎麼通過tomcat,連接配接背景的mysql資料庫。
二:拓撲圖
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLzAzM0MDOzATMvwVMxITMwIzLcRnbl1GajFGd0F2Lc12bj5yb0NWM14SMn1Wavw1LcpDc0RHaiojIsJye.png)
實驗環境:Centos5.8(kernel 2.6.18)+tomcat5.5+mysql5.0
三:JSP連接配接MySQL
注:伺服器的搭建不是本文的重點
這裡先介紹一下前端的JSP頁面和Tomcat連接配接的相關知識點,這裡談談個人的了解,首先JSP程式和tomcat通信要通過tomcat提供的連接配接池,tomcat可以在連接配接池中設定最大數量的連接配接,提供給JSP程式連接配接,連接配接池中的連接配接可以動态的釋放與回收。但是連接配接池中提供的連接配接數要小于Mysql連接配接池的數量。
tomcat配置連接配接池
tomcat連接配接池配置
vi/vim server.xml
Oracle資料庫的連接配接池配置
在中配置如下資訊
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資料庫的連接配接池配置
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的連接配接池配置
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
Oracle Datasource example
jdbc/myoracle
javax.sql.DataSource
Container
JSP連接配接資料庫的使用者
MySQL configuration
mysql>GRANT ALL PRIVILEGES ON *.* TO [email protected]
->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測試頁面
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();
%>
上述代碼僅是實作的一例。
四:測試