tomcat连接池。。。网页上使用可以,直接在类里运行不可以
配置好该配的xml(server,web)后,启动tomcat后在网页中使用可以连接成功,但在类里就是不行。
我的类是这么写的——
----------------------------------------
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class foo {
String foo = "Not Connected";
String bar =null;
public void init() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/vivizone");
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
String sql="select * from msg";
ResultSet rst =
stmt.executeQuery(sql);
if(rst.next()) {
foo=rst.getString(2);
bar=rst.getString(3);
}
conn.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
public String getFoo() { return foo; }
public String getBar() { return bar;}
public static void main(String[] args){
foo dm=new foo();
dm.init();
System.out.println(dm.getBar());
}
}
----------------------------------------
tomcat是启动好的,我一运行就提示:
javax.naming.NoInitialContextExcetion:Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file : java.naming.factory. initial
请问这是怎么呢?