首页 新闻 会员 周边 捐助

无法初始化类uitl.dbuti

0
悬赏园豆:80 [已关闭问题] 关闭于 2017-05-02 21:59

数据库连接池方法类

package uitl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {
    private static BasicDataSource bs;
    static{
        Properties ps=new Properties();
        try {
            ps.load(DBUtil.class.getClassLoader()
                    .getResourceAsStream("db.properties"));
            String driver=ps.getProperty("driver");
            String url=ps.getProperty("url");
            String user=ps.getProperty("user");
            String pwd=ps.getProperty("pwd");
            String initSize=ps.getProperty("initSize");
            String maxSize=ps.getProperty("maxSize");
            bs=new BasicDataSource();
            bs.setDriverClassName(driver);
            bs.setUrl(url);
            bs.setUsername(user);
            bs.setPassword(pwd);
            bs.setInitialSize(Integer.parseInt(initSize));
            bs.setMaxActive(Integer.parseInt(maxSize));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() throws SQLException{
        return bs.getConnection();
    }
    public static void close(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException("归还连接失败",e);
            }
        }
    }
}

 

dao方法中添加员工数具的方法

  

 1  public void insert(Emp e){
 2         Connection conn=null;
 3         try {
 4             conn=DBUtil.getConnection();
 5             //System.out.println(conn);
 6             String sql="INSERT INTO emp_ghq VALUES("
 7                     + " emp_ghq_seq_id.nextval,?,?,?,?,?)";
 8             PreparedStatement ps=conn.prepareStatement(sql);
 9             ps.setString(1,e.getName());
10             ps.setString(2,e.getGender());
11             ps.setInt(3,e.getAge());
12             ps.setDouble(4,e.getSalary());
13             ps.setDate(5,e.getHiredate());
14             ps.executeUpdate();
15         } catch (SQLException e1) {
16             e1.printStackTrace();
17             throw new RuntimeException(e1);
18         }finally{
19             DBUtil.close(conn);
20         }
21         
22     }
23 }

 

 服务器:

package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.EmpDao;
import enity.Emp;

public class EmpAddServlet extends HttpServlet{
    public void service(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw=response.getWriter();
        String name=request.getParameter("uname");
        String gender=request.getParameter("gender");
        String age=request.getParameter("age");
        String salary=request.getParameter("salary");
        String hiredate=request.getParameter("hiredate");
        EmpDao dao=new EmpDao();
        Emp e=new Emp();
        e.setName(name);
        e.setGender(gender);
        e.setAge(Integer.parseInt(age));
        e.setSalary(Double.parseDouble(salary));
        e.setHiredate(Date.valueOf(hiredate));
        try{
        dao.insert(e);
        }catch(Exception e1){
            pw.println("<h1>系统繁忙,请稍后重试!</h1>");
        }
    }
}

 



配置文件:

 <servlet>
      <servlet-name>empAddServlet</servlet-name>
      <servlet-class>web.EmpAddServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>empAddServlet</servlet-name>
      <url-pattern>/add</url-pattern>
  </servlet-mapping>

 

 报的错:

HTTP Status 500 - Servlet execution threw an exception

type Exception report

message Servlet execution threw an exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.NoClassDefFoundError: Could not initialize class uitl.DBUtil 
dao.EmpDao.insert(EmpDao.java:64) web.EmpAddServlet.service(EmpAddServlet.java:34) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) note The full stack trace of the root cause is available in the Apache Tomcat/8.0.43 logs.

 

 


Apache Tomcat/8.0.43

 请教各位大神帮忙一下。数据库和服务分开测试的时候都可以运行。部署在一起就报错!

问题补充:

请各位大神帮忙看看,是什么问题?

兔子来吃草的主页 兔子来吃草 | 初学一级 | 园豆:129
提问于:2017-04-29 17:53
< >
分享
所有回答(1)
0

问题已经解决了,是导的jar包的问题

兔子来吃草 | 园豆:129 (初学一级) | 2017-05-02 21:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册