Before writing JDBC, always write a class manually, with hard code to write classname, URL, user name and password, and then get to connection through DriverManager. That is very convenient to write, but if you want to change, you need to modify the source code to compile, to the late modification inconvenience. Recently saw a way to use Jndi to configure the JDBC data source using an XML file, and the XML file is stored under the war, so it's convenient for us to modify the JDBC connection properties.
First understand the next Jndi, Baidu search results: Jndi (Java naming and directory Interface,java naming and directory interface) is a standard Java naming system interface provided by Sun, Jndi provides a unified client API, Through the implementation of the different access provider Interface Jndi SPI, the manager maps the Jndi API to a specific naming service and directory system, allowing the Java application to interact with these naming services and directory services. A directory service is a naming service in which objects are not only named but also attributes. My understanding is that the service provider provides an API that can then be used to find the functionality provided by the service provider.
To use Jndi to configure the data source under Tomcat, you first have to Create a context.xml file in the Meta-inf directory. For example:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Context>3 <Resource4 name= "Jndi/datasource"//Data source name, used in Jndi5 type= "Javax.sql.DataSource"//Data Source Type6 Driverclassname= "Com.mysql.jdbc.Driver"//driver name7 URL= "jdbc:mysql://localhost:3306/database"//URL of the connection data source8 username= "name"//database user name9 Password= "Pass"//Database PasswordTen maxactive= "Ten"// Specifies the maximum number of database connections active in the database connection pool,0 means unrestricted One Maxldle= "5"//Specifies the maximum number of database connections in the database connection pool that are idle,0 means unrestricted A maxwait= "80000"//Specifies the maximum time (in milliseconds) that a database connection in a database connection pool is idle, and an exception will be thrown if this event is exceeded. -1 means you can wait indefinitely. - > - the </Resource> - </Context>
When the data source is configured, it starts calling, and the calling code is as follows:
1 ImportJavax.naming.InitialContext;2 Importjavax.naming.NamingException;3 ImportJavax.sql.DataSource;4 Private StaticInitialContext context;5 Private StaticDataSource DataSource;6 7 Public StaticConnection Getconn () {8Connection con =NULL;9 Try {TenContext =NewInitialContext (); OneDataSource = (DataSource) context.lookup ("Java:comp/env/jndi/datasource"); //Jndi/datasource Here is the name of the configuration data source ACon =datasource.getconnection (); -}Catch(SQLException e) { - e.printstacktrace (); the}Catch(namingexception e) { - e.printstacktrace (); - } - returncon; +}
This will get to the connection, and then you can use the same as before.
Using Jndi to load JDBC data sources in Tomcat