日期:2014-05-20  浏览次数:20934 次

tomcat5.5配置连接池的问题
我配置好了TOMCAT5.5的连接池可以抛异常
Java code

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
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at com.hxj.tools.DatabaseConn.getConnetion(DatabaseConn.java:16)
    at com.hxj.dao.EmpDAO.getAll(EmpDAO.java:21)
    at com.hxj.dao.EmpDAO.main(EmpDAO.java:50)
Exception in thread "main" java.lang.NullPointerException
    at com.hxj.dao.EmpDAO.getAll(EmpDAO.java:22)
    at com.hxj.dao.EmpDAO.main(EmpDAO.java:50)



我的web.xml
XML code

<resource-ref>
    <description>JNDI DataSource Test</description>
    <res-ref-name>jdbc/sqlserver</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>



我TOMCAT下server.xml
XML code

 <Context path="/pools">
      <Resource
          name="jdbc/sqlserver"
          type="javax.sql.DataSource"
          driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
          maxIdle="2"
          maxWait="5000"
          username="sa"
          password="1"
          url="jdbc:microsoft:sqlserver://localhost;DatabaseName=test"
          maxActive="4"/>
 </Context>



一个链接数据源的类
Java code

package com.hxj.tools;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DatabaseConn {

    public static synchronized Connection getConnetion() {
        Connection conn = null;
        try {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sqlserver");
            conn = ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return conn;
    }
}



DAO类
Java code

package com.hxj.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.hxj.bean.Emp;
import com.hxj.tools.DatabaseConn;

public class EmpDAO {

    public List<Emp> getAll(){
        List<Emp> list =new ArrayList<Emp>();
        String sql = "select * from t_emp";
        Connection conn = null;
        try {
            conn = DatabaseConn.getConnetion();
            PreparedStatement psmt = conn.prepareStatement(sql);
            ResultSet rs = psmt.executeQuery();
            while(rs.next()){
                Emp emp = new Emp();
                emp.setDeptno(rs.getInt("deptno"));
                emp.setEmpname(rs.getString("empname"));
                emp.setEmpno(rs.getString("empno"));
                list.add(emp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                if(conn!=null)
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    
    public static void main(String[] args) {
        EmpDAO empDAO = new EmpDAO();
        List<Emp> list = empDAO.getAll();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Emp element = (Emp) iter.next();
            System.out.println(element.getEmpname());
        }
    }
}