日期:2014-05-18  浏览次数:20666 次

连接数据库的JavaBean放在Action中就不能用了吗?
Java code

public class DbUtil {
    int i=0,j;
    String [][]data = new String[3][3];
    Connection con = null;
    ResultSet rs=null;
    Statement stmt=null;
    
    String drivers = "com.sybase.jdbc3.jdbc.SybDriver";    
    String url = "jdbc:sybase:Tds:dawnspace:5000/Denglu";
    public DbUtil(){
        try
        {
            Class.forName(drivers).newInstance();
            con = DriverManager.getConnection(url,"sa","");       
            stmt = con.createStatement();
            
        }
        catch(java.lang.Exception e)
        {
            e.printStackTrace();
        }
        
    }
}

上面这段程序单独运行可以成功连接到数据库里面,但如果在Action中用就会报错
Java code

public class DisplayAllAction extends Action{
    
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response){
        ActionErrors errors =  new ActionErrors();

        if (!errors.isEmpty()) {
            saveErrors(request, errors);
            return (new ActionForward(mapping.getInput()));
        }
        
        HttpSession session = request.getSession();
        
        String[][]data = new String[3][3];
        [color=#FF0000]DbUtil db = new DbUtil();[/color]
//        data = db.displayall();
        session.setAttribute(Constants.DISPLAY_ALL, data);
        return (mapping.findForward(Constants.FORWARD_SUCCESS));

    }

}



java.lang.ClassNotFoundException: com.sybase.jdbc3.jdbc.SybDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at model.DbUtil.<init>(DbUtil.java:17)
at actions.DisplayAllAction.execute(DisplayAllAction.java:34)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)