数据库连接池方法类
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
请教各位大神帮忙一下。数据库和服务分开测试的时候都可以运行。部署在一起就报错!
请各位大神帮忙看看,是什么问题?
问题已经解决了,是导的jar包的问题