Tomcat configuration data source steps and using JNDI

1、

Configure Tomcat's conf/context.xml

<Resource name="jdbc/news" 

              auth="Container"  type="javax.sql.DataSource"  maxActive="100" 

              maxIdle="30" maxWait="10000" username="admin"  password="54321" 

              driverClassName="oracle.jdbc.OracleDriver" 

              url="jdbc:oracle:thin:@localhost:1521:news"/>

 

Configure Tomcat's conf/context.xml

<Resource name="jdbc/news" 

              auth="Container"  type="javax.sql.DataSource"  maxActive="100" 

              maxIdle="30" maxWait="10000" username="root"  password="54321" 

              driverClassName="com.mysql.jdbc.Driver" 

              url="jdbc:mysql://127.0.0.1:3306/news"/>

 

2. Use JNDI to get the connection object

//initialize the context

Context cxt=new InitialContext();

//Get the data source object associated with the logical name

DataSource ds=(DataSource)cxt.lookup("java:comp/env/jdbc/news");

Connection connection=ds.getConnection();

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326799671&siteId=291194637