Reputation: 1237
My existing web application gets database pooling parameters from context. Now, I'm developing a new web service to be deployed as a separate application, but using the previous database.
In this new web service application, I am not using any servlet, as well as JSPs. They'll just be a collection of service classes. In such a case, how do I get the context parameters?
In the earlier case when I'm using a servlet, my context is as below:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" privileged="true" >
<Resource
name="jdbc/NetmarketDB"
auth="Container"
type="javax.sql.DataSource"
removeAbandoned="true"
removeAbandonedTimeout="30"
maxActive="100"
maxIdle="30"
maxWait="1000"
username="root"
password="xxxxxxxx"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/netmarket"/>
</Context>
... and I get the context from a servlet or JSP as below:
Connection conn = MySqlDB.getDBConnection(getServletContext());
But now that I'm not using any servlet/JSP, I'm confused. Please help me out.
Upvotes: 1
Views: 666
Reputation: 8596
Even if you don't have the servlet context as long as you are running inside your web application with the resource definition in your question you should be able to get it using JNDI:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/NetmarketDB");
Connection con = ds.getConnection("root", "xxxxxxxx");
And if you are not running within a web application then do this:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/netmarket", root", "xxxxxxxx");
Upvotes: 1