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();   虽然警告没有影响执行结果,但我实在搞不清楚上面的警告时怎么回事。希望有大侠能解释。谢谢  问题补充:<div class="quote_title">my_corner 写道</div><div class="quote_div">引用静态方法的时候,直接用类引用就可以了。直接PoolConnect.getCon(),这样就可以了。</div><br /><br /><br />可是在Jsp 页面调用的时候。 需要加载进去PoolConnect 这个类。<br /><br />然后指定的id="pool"&nbsp; 也是直接调用pool.getCon();<br /><br />这样不可以么?<br /><br />在jsp里面不能直接PoolConnect.getCon();<br />

解决方案

<%javabean1.PoolConnect.getCon(); %>
解决方案二:
类名.静态方法
解决方案三:
对象怎么调用静态方法呀 你去掉static吧
解决方案四:
引用静态方法的时候,直接用类引用就可以了。直接PoolConnect.getCon(),这样就可以了。

时间: 2024-07-31 09:37:41

The static method getCon() from the type PoolConnect should be accessed in a sta的相关文章

java的前期绑定和后期绑定使用示例_java

后期绑定,是指在运行时根据对象的类型进行绑定,又叫动态绑定或运行时绑定.实现后期绑定,需要某种机制支持,以便在运行时能判断对象的类型,调用开销比前期绑定大.Java中的static方法和final方法属于前期绑定,子类无法重写final方法,成员变量(包括静态及非静态)也属于前期绑定.除了static方法和final方法(private属于final方法)之外的其他方法属于后期绑定,运行时能判断对象的类型进行绑定.验证程序如下: 复制代码 代码如下: class Base{    //成员变量,

Overloading overriding runtime type and object orientation (2)

loading|object Objective 3) Write code to construct instances of any concrete class including normal top level classes inner classes static inner classes and anonymous inner classes.Inner Classes·    A class can be declared in any scope. Classes defi

Overloading overriding runtime type and object orientation (1)

loading|object 6)Overloading overriding runtime type and object orientationObjective 1)State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and &qu

c#3.0系列:Extension Method

我们说在C#3.0中,引入了一些列新的特性,但是个人认为Extension Method 这个特性是最爽的,最有创新的. 它真正的解决了:在保持现有Type原封不动的情况下对其进行扩展,你可以 在对Type的定义不做任何变动的情况下,为之添加所需的方法成员.下面我就来 讲讲. C#3.X出来之前 大家都知道javascript有个特新Prototype,它就如同C#3.X中的Extension Method.这里不多将了. 我们主要看看.NET的实现.在C#3.X出来之前我们可以做到对Type进

深入理解C# 3.x的新特性(2):Extension Method[上篇]

在C#3.0中,引入了一些列新的特性,比如: Implicitly typed local variable, Extension method,Lambda expression, Object initializer, Anonymous type, Implicitly typed array, Query expression, Expression tree. 个人觉得在这一系列新特性的,最具创新意义的还是Extension method,它从根本上解决了这样的问题:在保持现有Type

深入理解C# 3.x的新特性(2):Extension Method[下篇]

四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标记为this关键字的Static Method.在这一节中,我们来进一步认识Extension Method. 和C# 3.0的其他新特性相似,Extension Method仅仅是C#这种.NET Programming Language的新特性而已.我们知道,C#是一种典型的编译型的语言,我们编

[原创]深入理解C# 3.x的新特性(3):从Delegate、Anonymous Method到Lambda Expression

较之前一个版本,对于C# 3.x和VB 9来说,LINQ是最具吸引力的.基本上很多的新的特性都是围绕着LINQ的实现来设计的.借助Extension Method,我们可以为LINQ定义一系列的Operator.通过Lambda Expression我们可以为LINQ编写更加简洁的查询.我们可以说这些新的特性成就了LINQ,也可以说这些新特性就是为了实现LINQ而产生,但是我们应该明白,对于这些新引入的特性,LINQ并非他们唯一的用武之地,在一般的编程中,我们也可以使用它们. 继上一章,介绍Ex

详解Java编程中protected修饰符与static修饰符的作用_java

protected 来谈谈protected访问权限问题.看下面示例1: Test.java class MyObject {} public class Test { public static void main(String[] args) { MyObject obj = new MyObject(); obj.clone(); // Compile error. } } 此时出现上文提到的错误:The method clone from the type Object is not v

CLR 4.0 有哪些新东西? What&#039;s new in CLR 4.0--类型嵌入 Type embedding

前言 Foreword 自从2005年CLR 2.0发布之后, Microsoft有几年没有更新CLR了. 现在新的版本CLR 4.0即将发布. 本人看了几个英文博文. 综合起来, 摘录其要点. 用一个系列介绍几个CLR4.0的新特性. 为照顾英文不好的同学, 特用中英文对照的形式. Microsoft has not upgraded CLR for years since it released CLR 2.0 in 2005. The new CLR 4.0 is about to rel