<small id='aJGlU'></small><noframes id='aJGlU'>

  • <i id='aJGlU'><tr id='aJGlU'><dt id='aJGlU'><q id='aJGlU'><span id='aJGlU'><b id='aJGlU'><form id='aJGlU'><ins id='aJGlU'></ins><ul id='aJGlU'></ul><sub id='aJGlU'></sub></form><legend id='aJGlU'></legend><bdo id='aJGlU'><pre id='aJGlU'><center id='aJGlU'></center></pre></bdo></b><th id='aJGlU'></th></span></q></dt></tr></i><div id='aJGlU'><tfoot id='aJGlU'></tfoot><dl id='aJGlU'><fieldset id='aJGlU'></fieldset></dl></div>

    1. <legend id='aJGlU'><style id='aJGlU'><dir id='aJGlU'><q id='aJGlU'></q></dir></style></legend>
      • <bdo id='aJGlU'></bdo><ul id='aJGlU'></ul>

      1. <tfoot id='aJGlU'></tfoot>
      2. 如何使用 Struts 2 在 Apache Tomcat 应用程序中建立数据库连接

        时间:2023-10-15

        <small id='8hx6I'></small><noframes id='8hx6I'>

      3. <i id='8hx6I'><tr id='8hx6I'><dt id='8hx6I'><q id='8hx6I'><span id='8hx6I'><b id='8hx6I'><form id='8hx6I'><ins id='8hx6I'></ins><ul id='8hx6I'></ul><sub id='8hx6I'></sub></form><legend id='8hx6I'></legend><bdo id='8hx6I'><pre id='8hx6I'><center id='8hx6I'></center></pre></bdo></b><th id='8hx6I'></th></span></q></dt></tr></i><div id='8hx6I'><tfoot id='8hx6I'></tfoot><dl id='8hx6I'><fieldset id='8hx6I'></fieldset></dl></div>

            1. <tfoot id='8hx6I'></tfoot>

                <bdo id='8hx6I'></bdo><ul id='8hx6I'></ul>

                • <legend id='8hx6I'><style id='8hx6I'><dir id='8hx6I'><q id='8hx6I'></q></dir></style></legend>
                    <tbody id='8hx6I'></tbody>

                • 本文介绍了如何使用 Struts 2 在 Apache Tomcat 应用程序中建立数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在应用程序级别建立数据库连接,所以我想在 Apache Tomcat 服务器的 web.xml 文件中创建连接.我正在使用 Struts2 MVC 框架开发我们的应用程序.实际上我不想在每个 Java 文件上创建数据库连接.如何在应用程序中建立数据库连接?

                  I want to make database connection at application level, so I want create connection in web.xml file of Apache Tomcat server. I am developing our application using Struts2 MVC framework. Actually I don't want to create database connection at each Java file. How to make a database connection in application?

                  我正在尝试在 web.xml 中创建连接,但是 Connection conn = ds.getConnection 行显示了类似 java.lang.NullPointerException 的错误();.所有代码都显示在下面

                  I am trying create a connection in web.xml but a error like java.lang.NullPointerException is being displayed at line Connection conn = ds.getConnection();. All code is being displayed in below

                  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;
                   }
                  }
                  

                  推荐答案

                  @Resource 没有意义.如果您需要有关注入资源的更多信息,您应该阅读 教程.而是创建一个 ServletContextListener 并将注释放在那里.关于上下文初始化事件设置上下文属性

                  Putting @Resource on the action bean property makes no sense. If you need more information about injecting resources you should read a tutorial. Instead create a ServletContextListener and put annotation there. On context initialized event set context attribute

                  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中):

                  web.xml (should be in WEB-INF):

                  <listener>
                    <listener-class>com.servlet.MyServletContextListener</listener-class>
                  </listener>
                  

                  现在可以在execute方法中获取数据源

                  now you can get datasource in the execute method

                  ds = (DataSource)ServletActionContext.getServletContext().getAttribute("ds");
                  

                  这篇关于如何使用 Struts 2 在 Apache Tomcat 应用程序中建立数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <tbody id='errdE'></tbody>
                  1. <i id='errdE'><tr id='errdE'><dt id='errdE'><q id='errdE'><span id='errdE'><b id='errdE'><form id='errdE'><ins id='errdE'></ins><ul id='errdE'></ul><sub id='errdE'></sub></form><legend id='errdE'></legend><bdo id='errdE'><pre id='errdE'><center id='errdE'></center></pre></bdo></b><th id='errdE'></th></span></q></dt></tr></i><div id='errdE'><tfoot id='errdE'></tfoot><dl id='errdE'><fieldset id='errdE'></fieldset></dl></div>

                    1. <tfoot id='errdE'></tfoot>

                      • <legend id='errdE'><style id='errdE'><dir id='errdE'><q id='errdE'></q></dir></style></legend>

                        <small id='errdE'></small><noframes id='errdE'>

                        • <bdo id='errdE'></bdo><ul id='errdE'></ul>