我想在应用程序级别建立数据库连接,所以我想在web.xml
Apache Tomcat 服务器的文件。我正在使用 Struts2 MVC 框架开发我们的应用程序。实际上我不想在每个Java 文件上创建数据库连接。如何在应用程序中建立数据库连接?
我正在尝试创建连接web.xml
但像这样的错误java.lang.NullPointerException
正在显示在行Connection conn = ds.getConnection();
。所有代码都显示在下面
META-INFO/context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/dbmy" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000"
username="root" password="mysql"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbmy"/>
</Context>
lib/web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MY</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<resource-ref>
<description>MySQL Datasource</description>
<res-ref-name>jdbc/dbmy</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
我的 Action 类如下:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import com.opensymphony.xwork2.*;
public class GEtResponseObject extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
ServletContext context =null;
PreparedStatement ps =null;
ResultSet rs =null;
@Resource(name="jdbc/dbmy")
private DataSource ds;
public String execute() {
try{
Connection conn = ds.getConnection(); //At this line, A java.lang.NullPointerException error is being occured.
ps = conn.prepareStatement("select * from dbmy.mytable ");
rs = ps.executeQuery();
if (rs.next()) {
System.out.println(rs.getString("mycolom"));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
return Action.SUCCESS;
}
}
Putting @Resource对action bean 属性没有任何意义。如果您需要更多信息注入资源你应该读一读tutorial。相反,创建一个ServletContextListener
并将注释放在那里。在上下文初始化事件上设置上下文属性
public class MyServletContextListener implements ServletContextListener {
@Resource(name="jdbc/dbmy")
private DataSource ds;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("contextInitialized");
ServletContext context = servletContextEvent.getServletContext();
context.setAttribute("ds",ds);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("contextDestroyed");
}
}
web.xml
(应该在 WEB-INF 中):
<listener>
<listener-class>com.servlet.MyServletContextListener</listener-class>
</listener>
现在您可以在执行方法中获取数据源
ds = (DataSource)ServletActionContext.getServletContext().getAttribute("ds");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)