在web开发是用连接池的话,通过一个公用的java类来得到Connection,然后在网页上嵌入java代码调用这个类来得到Connection,然后操作数据库可以,但我想在其他的java类中调用公用的java类来得到Connection,然后操作数据库,这样我试了为什么不行啊?说什么"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”,希望有知道的人告诉我,谢谢啦~

解决方案 »

  1.   

    在conf/server.xml中配置如下:<Context path="/news" docBase="news" debug="5" reloadable="true" crossContext="true">
             <Resource name="jdbc/sqlserver"  
               auth="Container"      
               type="javax.sql.DataSource"      
               driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"      
               url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=newsdb"      
               username="sa"      
               password=""      
               maxActive="100"      
               maxIdle="30"      
               maxWait="10000" />
            </Context>最后的三个数字可以改变。
    在站点文件夹/WEB-INF/web.xml中配置:<resource-ref>      
        <description>DB Connection</description>      
        <res-ref-name>jdbc/sqlserver</res-ref-name>      
        <res-type>javax.sql.DataSource</res-type>      
        <res-auth>Container</res-auth>      
      </resource-ref> 
      

  2.   

    楼上的朋友!
    上面的代码也可以写在我们的项目工程下的那个WENE-INF文件下的context.xml文件中,
    在连接的时候在应用Context context=new InitContext();
    DataSource ds=(DataSource)context.lookup("java:comp/env/java/sqlserver");
    Connection conn=ds.getConnection();
    一定要记住try啊!
    这只是一个数据源连接的对象生成了,具体的还需要自己操作!
    加油啊 !