Read the data source configuration, get the connection
1. Configure the data source
D:\apache-tomcat-7.0.70\conf\context.xml
<Resourcename= "Jdbc/news"Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "+"maxwait= "10000"username= "root"Password= "123456"Driverclassname= "Com.mysql.jdbc.Driver"URL= "JDBC:MYSQL://127.0.0.1:3306/NEWS?USEUNICODE=TRUE&CHARACTERENCODING=GBK"/>
2. Get the connection
//Get database connection PublicConnection GetConnection2 () {Try { //Initializing ContextContext cxt=NewInitialContext (); //gets the data source object associated with the logical nameDataSource ds= (DataSource) cxt.lookup ("Java:comp/env/jdbc/news"); Conn=ds.getconnection (); } Catch(namingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnConn; }
Java Connection Data Source-read Tomcat data source configuration