How to operate MySQL database instances using JSP

Source: Internet
Author: User

How to operate MySQL database instances using JSP

I. Overview

Before starting the introduction, let's talk about why we should write this article. I personally think that as an O & M engineer, we should be familiar with the knowledge network, not just by mastering some knowledge, such as the linux system, the construction of web servers, databases, and so on should also be familiar with the networks composed of these points. Specifically, how the points affect each other and how the points interact with each other, for example, when a problem occurs at a certain point, we can analyze the system to find the root cause of the problem. In this case, how does the web Front-end JSP program or PHP, ASP, etc.) establish a line with the background database through the Intermediate program? Here we will temporarily release JSP, tomcat, mysql is called a so-called line), how to communicate, how to affect each other, there is too much content involved here, limited to personal level hate, just introduce how JSP through tomcat, connect to the mysql database in the background.

2. Topology

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1Z6255034-0.png "border =" 0 "alt =" "/>

Lab environment: Centos5.8 (kernel 2.6.18) + tomcat5.5 + mysql5.0

Iii. JSP connection to MySQL

Note: Server construction is not the focus of this Article

Here we will first introduce the front-end JSP page and Tomcat connection knowledge points. Here we will talk about my personal understanding. First, JSP programs and tomcat communication should be through the connection pool provided by tomcat, tomcat can set the maximum number of connections in the connection pool to provide JSP program connections. connections in the connection pool can be dynamically released and recycled. However, the number of connections provided in the connection pool is smaller than the number of Mysql connection pools.

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 configuration connection pool

 
 
  1. Tomcat connection pool Configuration
  2. Vi/vim server. xml
  3.  
  4. Oracle Database Connection Pool Configuration
  5. Configure the following information in
  6. <Resource
  7. Auth = "Container"
  8. Description = "sqlserver Datasource"
  9. Name = "jdbc/ora"
  10. Type = "javax. SQL. DataSource"
  11. MaxActive = "50"
  12. MaxIdle = "10"
  13. Username = "" ----> username used to connect to the database
  14. MaxWait = "10000"
  15. DriverClassName = "oracle. jdbc. driver. OracleDriver"
  16. Password = "" -----> password used to connect to the database
  17. Url = "jdbc: oracle: thin: @ host: port/databases"
  18. RemoveAbandoned = "true"
  19. RemoveAbandonedTimeout = "60"
  20. LogAbandoned = "true"/>
  21.  
  22. MySQL database connection pool Configuration
  23.  
  24. <Resource
  25. Name = "jdbc/TestDB"
  26. Auth = "Container"
  27. Type = "javax. SQL. DataSource"
  28. MaxActive = "100"
  29. MaxIdle = "30"
  30. MaxWait = "10000"
  31. Username = "javauser"
  32. Password = "javadude"
  33. DriverClassName = "com. mysql. jdbc. Driver"
  34. Url = "jdbc: mysql: // localhost: 3306/javatest"/>
  35.  
  36. SQL connection pool Configuration
  37. <Resource
  38. Auth = "Container"
  39. Description = "sqlserver Datasource"
  40. Name = "jdbc/sqlserver110"
  41. Type = "javax. SQL. DataSource"
  42. MaxActive = "100"
  43. MaxIdle = "10"
  44. Username = ""
  45. MaxWait = "10000"
  46. DriverClassName = "com. microsoft. jdbc. sqlserver. SQLServerDriver"
  47. Password = ""
  48. Url = "jdbc: microsoft: sqlserver: IP (port); database name;" reconnect = true"
  49. RemoveAbandoned = "true"
  50. RemoveAbandonedTimeout = "60"
  51. LogAbandoned = "true"/>

Tomcat5.5 parameter explanation:

 
 
  1. Tomcat5.5 parameter description:
  2. 1 maxActive: Maximum number of dB connections in pool. Make sure you
  3. Configure your mysqld max_connections large enough to handle
  4. All of your db connections. Set to-1 for no limit
  5. The maximum number of connections in the connection pool is-1, indicating that the number of data connections is greater than the number of connections.
  6. 2 maxIdle: Maximum number of idle dB connections to retain in pool.
  7. Set to-1 for no limit. See also the DBCP documentation on this
  8. And the minEvictableIdleTimeMillis configuration parameter
  9. The maximum number of idle connections in the connection pool)
  10. 3 maxWait: Maximum time to wait for a dB connection to become available
  11. In MS, in this example 10 seconds. An Exception is thrown if
  12. This timeout is exceeded. Set to-1 to wait indefinitely
  13. Maximum wait time (ms) for a connection to become available.
  14. 4 driverClassName: Class name for the old mm. mysql JDBC driver is
  15. Org. gjt. mm. mysql. Driver-we recommend using Connector/J though.
  16. Class name for the official MySQL Connector/J driver is com. mysql. jdbc. Driver.
  17. 5 url: The JDBC connection url for connecting to your MySQL dB
  18. 6 removeAbandoned = "true" abandoned dB connections are removed and recycled)
  19. Explanation: The default value is false when abandoned data connections are recycled to the connection pool.
  20. 7 removeAbandonedTimeout = "60" a dB connection has been idle before it is considered abandoned) Unit: seconds
  21. Explanation: the number of seconds when a connection is idle will be abandoned.
  22. 8 logAbandoned = "true"
  23. The default value of record abandoned data connections is false.

Create a WEB-INF/web. xml in the directory of the web application and add the following content

 
 
  1. web.xml configuration 
  2. <resource-ref> 
  3.  <description>Oracle Datasource example</description> 
  4.  <res-ref-name>jdbc/myoracle</res-ref-name> 
  5.  <res-type>javax.sql.DataSource</res-type> 
  6.  <res-auth>Container</res-auth> 
  7. </resource-ref>  

User who connects to the database through JSP

 
 
  1. MySQL configuration
  2. Mysql> grant all privileges on *. * TO javauser @ localhost
  3. -> Identified by 'javadude' with grant option;
  4. Mysql> create database javatest;
  5. Mysql> use javatest;
  6. Mysql> create table testdata (
  7. -> Id int not null auto_increment primary key,
  8. -> Foo varchar (25 ),
  9. -> Bar int );
  10. Mysql> insert into testdata values (null, 'Hello', 12345 );
  11. Query OK, 1 row affected (0.00 sec)
  12. Mysql> select * from testdata; + ---- + ------- + | ID | FOO | BAR | + ---- + ------- + | 1 | hello | 12345 | + ---- + ------- + 1 row in set (0.00 sec) note: 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 shocould be removed once testing is complete!
  13. 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1Z62523H-3.png "border =" 0 "alt =" "/>

JSP test page

 
 
  1. <% @ Page import = "java. SQL. *" %>
  2. <% @ Page contentType = "text/html; charset = gb2312" %>
  3. <% @ Page language = "java" %>
  4. <% @ Page import = "com. mysql. jdbc. Driver" %>
  5. <% @ Page import = "java. SQL. *" %>
  6. <%
  7. String driverName = "com. mysql. jdbc. Driver ";
  8. String userName = "javauser ";
  9. String userPasswd = "java ";
  10. String dbName = "javatest ";
  11. String tableName = "testdata ";
  12. String url = "jdbc: mysql: // localhost/" + dbName + "? User = "+ userName +" & password = "+ userPasswd;
  13. Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
  14. Connection connection = DriverManager. getConnection (url );
  15. Statement statement = connection. createStatement ();
  16. String SQL = "SELECT * FROM" + tableName;
  17. ResultSet rs = statement.exe cuteQuery (SQL );
  18. While (rs. next ())
  19. {
  20. String foo = rs. getString ("foo ");
  21. String bar = rs. getString ("bar ");
  22. Out. print (foo + "");
  23. Out. print (bar + "");
  24. }
  25. Rs. close ();
  26. Statement. close ();
  27. Connection. close ();
  28. %>
  29. The above code is only an example of implementation.

Iv. Test

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1Z6253315-4.png "border =" 0 "alt =" "/>

This article is from the "haangangular" blog. For more information, contact the author!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.