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