Use jndi to connect data source in tomcat7

To configure jndi of tomcat7 in the eclipse development environment, you need to add it in context.xml under servers/Tomcat v7.0 Server at localhost (2)-config in eclipse:
Note: The following configuration must be added to context.xml, instead of server.xml
<Resource name="jdbc/quickinfo"    
                  auth="Container"    
                  type="javax.sql.DataSource"   
                  maxActive="100"    
                  maxIdle="30"    
                  maxWait="10000"    
                  username="webbhIn"    
                  password= "password"   
                  driverClassName="oracle.jdbc.driver.OracleDriver"    
                  url="jdbc:oracle:thin:@192.168.6.3:1521:gnt"    
        /> 

name: jndi's name
username: database username
password: database password How


to get data connection:

private DataSource ds =null;
try
        {
            String jndi="java:comp/env/jdbc/quickinfo";

            Context ctx = new InitialContext();
            ds=(DataSource)ctx.lookup( jndi);
        }
        catch(Exception e)
        {
            System.out.println("ERROR:Datasource config is wrong");  
        } 

        System.out.println(ds);

Note: java:comp/env/jdbc/quickinfo format java :comp/env/+jndi name



Note : The configuration of <Resource> is added to the server.xml where the instance of DataSource cannot be obtained

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326255019&siteId=291194637