如何利用Database Connection Pool?
我个人用的是Tomcat 4.1.30版的,并且我在Tomcat中己经配置好了Database Connection Pool,同时也在JSP页面中可以得到数据库的连接了。我现在想将得到数据库的连接放在一java文件中去,而不是只能在JSP页面中得到。例如我写了下面一个文件:
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class test
{
public static void main(String args[])
{
Context ctx=null;
DataSource ds=null;
Connection conn=null;
PreparedStatement pre=null;
ResultSet res=null;
String sql="select * from projectprogress";
try
{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
if (ds != null)
conn = ds.getConnection();
pre=conn.prepareStatement(sql);
res=pre.executeQuery();
while (res.next())
{
System.out.println(res.getFloat("ProgressID"));
System.out.println(res.getString("ProgressDescription"));
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
这个文件在编译的时候没有问题,而且把这些代码放到一个JSP页面中运行也没有问题,但是一在Java下面单独运行时,就出现下面的异常
javax.naming.NoInitialContextException:
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
请问我该如何做才能使这个文件可以直接运行?