我现有的web应用程序从上下文中获取数据库池参数。现在,我正在开发一个新的web服务,它将部署为一个单独的应用程序,但使用的是以前的数据库。
在这个新的web服务应用程序中,我没有使用任何servlet,也没有使用JSP。它们将仅仅是服务类的集合。在这种情况下,如何获取上下文参数?
在前面的例子中,当我使用servlet时,我的上下文如下:
<?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>..。我从servlet或JSP获得上下文,如下所示:
Connection conn = MySqlDB.getDBConnection(getServletContext());但是现在我没有使用任何servlet/JSP,我很困惑。请帮帮我。
发布于 2010-09-09 20:08:42
即使您没有servlet上下文,只要您使用问题中的资源定义在web应用程序中运行,您就应该能够使用JNDI获得它:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/NetmarketDB");
Connection con = ds.getConnection("root", "xxxxxxxx");如果您不是在web应用程序中运行,请执行以下操作:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/netmarket", root", "xxxxxxxx");https://stackoverflow.com/questions/3676288
复制相似问题