Blog system of Java Web Project (2)

Source: Internet
Author: User
Tags driver manager

1. JDBC technology

Source: http://zengyiqiang2006.blog.163.com/blog/static/102868226201052673216733/

JDBC technology (a database access technology)

The core is to use the drivermanager class operation and management to implement the implementation class of the driver interface. The programmer only needs to connect the object to this driver manager class.

(Connection con = drivermanager. getconnection (RUL ))

JDBC Driver (name of the implementation class implementing the Driver Interface)

What we usually call "which JDBC driver/Driver Class is used ?" It actually refers to the implementation class that implements the driver interface. If you use the implementation class (COM. mySQL. JDBC. driver) or use the implementation class provided by the SQL Server 2000 database vendor (om. microsoft. JDBC. sqlserver. sqlserverdriver); whether to use a third-party implementation class or the implementation class provided by Java itself (sun. JDBC. ODBC. jdbcodbcdriver) but no matter which driver you use, you must know the corresponding URL.

JDBC connection pool technology: encapsulates and modifies drivermanager,

The connection pool is actually a collection in Java, such as list, map, and vector.

The core of the connection pool technology is to repeatedly use multiple connection objects in the connection pool (SET). Its underlying implementation is based on the JDBC technology, that is, the implementation class of the driver interface for drivermanager operations and management.

Summary:

I. How to create multiple connection objects?

According to the source code, you only need to send the statement several times to drivermanager.

Ii. How to obtain the connection object in the connection pool?

You only need to send the package to drivermanager before encapsulation. After encapsulation, you need to send the package to datasource.

3. The drivermanager object in Java mainly serves to obtain the connection object of the implementation class implementing the driver interface.

For example, connection con = drivermanager. getconnection (RUL ))

4. The datasource object in Java is a bit like the drivermanager before encapsulation. It is mainly used to obtain connection objects in a third-party component (connection pool ).

For example, context = new initialcontext (); // JNDI knowledge

Datasource DS = (datasource) Context. Lookup ("Java:/COMP/ENV/jdbc/mysqlds ");

Connection con = Ds. getconnection ();

V. Connection Pool: it is actually a set in which a certain number of connection objects are saved, so it manages the connection of the entire database.

6. Data source: it is actually a datasource object in Java. It is mainly used to manage the connection pool and obtain the connection objects in a third-party component (connection pool), a bit like drivermanager. In other words, the management of each connection pool is controlled by an object called a data source, represented by datasource in Java, which is expressed by the <data-source> label in the XML file.

7. Two methods to obtain the connection object,

One is the traditional requirement for drivermanager, con = drivermanager. getconnection ()

The other is to request data from datasource, con = (datasource) New initialcontext (). Lookup ("Java:/COMP/ENV/jdbc/testdb"). getconnection ();

8. JNDI

Context context = new initialcontext (); // JNDI knowledge

Datasource DS = (datasource) Context. Lookup ("Java:/COMP/ENV/jdbc/mysqlds"); // use

The lookup method of JNDI is used to find the existing datasource object in Tomcat. Java:/COMP/env

This is a fixed method.

Connection con = Ds. getconnection (); // If the datasource object exists, you can obtain the connction object.

2. Configure the database connection pool in Tomcat 6.0

Source: http://zengyiqiang2006.blog.163.com/blog/static/10286822620105267244342/

The following uses the MySQL database as an example. Other databases have the same configuration.
First, copy the MySQL driver to the lib directory under tomcat. C: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ Lib
Modify the context. xml file under c: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ conf and add the following code to it:
Add a property reloadable = "true" to the context tag"

<Watchedresource> WEB-INF/Web. xml </watchedresource> <! -- You do not need to add this --> <Resource Name = "JDBC/mysqlds" auth = "Container" type = "javax. SQL. datasource "maxactive =" 100 "maxidle =" 30 "maxwait =" 10000 "username =" root "Password =" admin "driverclassname =" com. mySQL. JDBC. driver "url =" JDBC: mysql: // localhost: 3306/databasename "/>

The following explains the meaning of each attribute:
Name indicates the specified JNDI name (this name can be written at will)
Auth indicates the authentication method, generally container
Type indicates the data source bed type, using the standard javax. SQL. datasource
Maxactive indicates the largest database connection in the connection pool.
Maxidle indicates the maximum number of idle connections
Maxwait maximum wait time when the database connection in the pool is occupied
Logabandoned indicates whether the dropped database connection is recorded for tracking.
Username indicates the Database User Name
Password indicates the password of the database user.
Driverclassname indicates JDBC driver
URL indicates the URL of the database.

Example:

<Context> <watchedresource> WEB-INF/Web. xml </watchedresource> <! -MySQL data source configuration --> <resourcename = "JDBC/mysqlds" auth = "Container" type = "javax. SQL. datasource "maxactive =" 100 "maxidel =" 30 "maxwait =" 10000 "username =" root "Password =" 5982285 "driverclassname =" com. mySQL. JDBC. driver "url =" JDBC: mysql: // localhost/myblog "/> <! -Oracle Data source configuration --> <resourcename = "JDBC/oracleds" auth = "Container" type = "javax. SQL. datasource "maxactive =" 100 "maxidel =" 30 "maxwait =" 10000 "username =" Scott "Password =" tiger "driverclassname =" oracle. JDBC. driver. oracledriver "url =" JDBC: oracle: thin :@ localhost: 1521: sky "/> </context>

Attribute description: Name:Data Source Name, usually JDBC/xxxFormat

Auth: ContainerContainer

Type: Javax. SQL. datasourceNote: javaxNot Java

Username:Database username

Password:Database User Password

Maxidle:The maximum number of idle instances and the maximum idle time for database connection. Database Connection exceeds idle time

The connection is marked as unavailable and then released. Set to 0Unlimited.

Maxactive:The maximum number of database connections in the connection pool. Set to 0Unlimited.

Maxwait:Maximum connection establishment wait time. If this time is exceeded, an exception occurs. Set to-1Indicates

Unlimited.

Driveclassname: Full name of the driver category for the database

URL:Database Connection

Many people on the Internet say they need to use web. xmlFile ReconfigurationIn fact, configuration population is also possible.

With <watchedresource> WEB-INF/Web. xml </watchedresource> configured, you do not need to configure it in the web. xml file.

Or configured in context. xml instead of server. XML, so you do not need to configure in Web. xml without testing.

3. For attributes of HTML <label> labels

The for Attribute specifies the form element to which the label is bound.

One of the following methods is usually used to associate the Form Control: the form control is used as the content of the tag, which is an implicit form, you can also name a target form ID for the for Attribute under the <label> label. This is an explicit form.

For example, in XHTML:

Explicit contact:<label for="SSN">Social security number:</label><Input type = "text" name = "socsecnum"id="SSn"/> Implicit contact:<label>Date of birth: <input type = "text" name = "dofb"/></label>

The first tag is to explicitly associate the text "social security number:" With the form's social security number input control ("socsecnum, the value of its for attribute is the same as the control ID, which is SSN. The second tag ("Date of Birth:") does not require the for attribute, and its related controls do not require the ID attribute, they are implicitly connected by placing the <input> label in the <label> label.

Instance

A simple HTML form with two input fields and related tags:

<form>  <label for="male">Male</label>  <input type="radio" name="sex" id="male" />  <br />  <label for="female">Female</label>  <input type="radio" name="sex" id="female" /></form>

4. One problem: the reference to entity "password" must end with the ';' delimiter.

This error is caused by a roge' & 'in database_properties.xml. This XML tag...

<param name="url" value="jdbc:mysql://129.12.16.16/jwnl?user=myusername&password=mypassword"/>



Shocould actually read...

<param name="url" value="jdbc:mysql://129.12.16.16/jwnl?user=myusername&amp;password=mypassword"/>



This is because the sax XML parsing libraries used by jwnl are generic XML libraries and therefore regular CT an escaped

Charcter sequence.

Test successful

Test page

5. Another problem: Name JDBC is not bound in this context

I have been searching for this problem for a long time and it has never worked. Both the context. xml and MySQL drivers have no problem, but I keep reporting this error! Finally, I was speechless and found that the mistake was to write a word by myself!

DS = (datasource) Context. Lookup ("Java: COMP/ENV/jdbc/mysqlds"); // Note: The first word is Java, not JDBC!

Someone on the Internet explained that the following content should be added to Web. xml:

<Resource-ref>

<Description> blog </description>

<Res-ref-Name> JDBC/mysqlds </RES-ref-Name>

<Res-type> javax. SQL. datasource </RES-type>

<Res-auth> container </RES-auth>

</Resource-ref>

I still reported an error after testing. It's useless!

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.