The static method getCon() from the type PoolConnect should be accessed in a sta

The static method getCon() from the type PoolConnect should be accessed in a sta

问题描述:

请教各位大侠。我连接Mysql时用到了连接池。 在连接池的类中用到了static函数。然后Jsp页面中调用此函数时总出现:The static method getCon() from the type PoolConnect should be accessed in a static way 这个警告。

 

下面是我的连接池的类:

package javabean1;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
public class PoolConnect {
      public static Connection getCon() {
          Connection con = null;
          try {
               Context context=new InitialContext();
               DataSource  ds=(DataSource)context.lookup("java:/comp/env/jdbc/TestDB");
               con=ds.getConnection();
           } catch (Exception e) {
               e.printStackTrace();
          }
          return con;
     }
}

 

下面是我调用语句:

<jsp:useBean id="pool" class="javabean1.PoolConnect" scope="page"></jsp:useBean>(页面中引用连接池的类)

调用类:

Connection conn = pool.getCon();  

 

虽然警告没有影响执行结果,但我实在搞不清楚上面的警告时怎么回事。希望有大侠能解释。谢谢

 

<%
javabean1.PoolConnect.getCon();
%>

引用静态方法的时候,直接用类引用就可以了。直接PoolConnect.getCon(),这样就可以了。

对象怎么调用静态方法呀 你去掉static吧

类名.静态方法