The Tomcat server connects the Jndi data source configuration of the MySQL database with the Java code that obtains the connection

Source: Internet
Author: User

First, import the MySQL jar package into the Tomcat/lib folder

And then configure the following in the Tomcat/conf/context.xml file
<Resourcename= "Jdbc/mysql"Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "+"maxwait= "10000"Driverclassname= "Com.mysql.jdbc.Driver"URL= "jdbc:mysql://server address: Port number/database name"username= "Database user name"Password= "Database Password"/>

Note: When you configure the above, simply replace the Url,username,password with your own information, and other content can be copied and pasted

The server address is native to 127.0.0.1 or localhost, the port number is usually 3306, and if you change it, fill in the changed port number

The database user name is usually root

Password for the database password set for itself

And finally need to write a piece of Java code
 Public classTest {Connection Connection=NULL;  PublicConnection tomcatgetconnection () {Try{Context Context=NewInitialContext (); DataSource DataSource= (DataSource) context.lookup ("java:comp/env/Jdbc/mysql"); Connection=datasource.getconnection (); } Catch(namingexception e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); }        returnconnection; }}

Note: The previous code is labeled Red with the <resource name= "jdbc/mysql" in the configuration file./> Name is the same, you can name it as you wish, usually in this format.

The Tomcat server connects the Jndi data source configuration of the MySQL database with the Java code that obtains the connection

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.