JSP操作MySQL資料庫執行個體講解
一:概述
在開始介紹之前先談談為什麼要寫這片文章,個人認為作為一個營運工程師,我們要熟悉的知識網路,不僅僅要限於點知識的掌握,比如linux系統,web伺服器的搭建,資料庫等等,還要熟悉這些點組成的網路,確切的說,點之間的是怎麼相互影響的,點與點之間怎麼相互操作等等,比如在某個點出現問題時,我們可以系統的分析,最終尋找到問題的根源。那麼在web前端的JSP程式或者PHP,ASP等)是怎麼通過中間的程式與背景資料庫建立起一條線這裡我們暫且將JSP,tomcat,mysql稱為一條所謂的線),怎麼通訊,怎麼相互影響,這裡涉及的內容太多了,限於個人水平有恨,僅介紹一下JSP怎麼通過tomcat,串連背景mysql資料庫。
二:拓撲圖
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/1Z6255034-0.png" border="0" alt="" />
實驗環境:Centos5.8(kernel 2.6.18)+tomcat5.5+mysql5.0
三:JSP串連MySQL
註:伺服器的搭建不是本文的重點
這裡先介紹一下前端的JSP頁面和Tomcat串連的相關知識點,這裡談談個人的理解,首先JSP程式和tomcat通訊要通過tomcat提供的串連池,tomcat可以在串連池中設定最大數量的串連,提供給JSP程式串連,串連池中的串連可以動態釋放與回收。但是串連池中提供的串連數要小於Mysql串連池的數量。
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/1Z6256324-1.png" border="0" alt="" />
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/1Z6253959-2.png" border="0" alt="" />
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!
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/1Z62523H-3.png" border="0" alt="" />
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();
- %>
- 上述代碼僅是實現的一例。
四:測試
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/1Z6253315-4.png" border="0" alt="" />
本文出自 “好望角” 部落格,轉載請與作者聯絡!