Java Connection Data Source-read Tomcat data source configuration

Source: Internet
Author: User

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&AMP;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

Related Article

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.