JAVA WEB 模块大全第22章论坛加载数据库驱动问题
发表在JavaWeb图书答疑 2011-03-16
是否精华
版块置顶:
问题是这样的。
第22章的进入论坛界面可以显示,当点击进入论坛是出错。在页面上显示的是
java.lang.NullPointerException
com.toolsbean.DB.getRs(DB.java:71)
com.dao.CategoryDao.getCategoryMap(CategoryDao.java:133)
com.servlet.IndexServlet.doPost(IndexServlet.java:27)
com.servlet.IndexServlet.doGet(IndexServlet.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.filter.CookieFilter.doFilter(CookieFilter.java:43)
com.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:24)

在控制器里面显示的是
加载数据库驱动失败!
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.toolsbean.DB.<init>(DB.java:20)
at com.dao.CategoryDao.getCategoryMap(CategoryDao.java:131)
at com.servlet.IndexServlet.doPost(IndexServlet.java:27)
at com.servlet.IndexServlet.doGet(IndexServlet.java:20)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CookieFilter.doFilter(CookieFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:24)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
创建数据库连接失败!
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.toolsbean.DB.getCon(DB.java:31)
at com.toolsbean.DB.doPstm(DB.java:50)
at com.dao.CategoryDao.getCategoryMap(CategoryDao.java:132)
at com.servlet.IndexServlet.doPost(IndexServlet.java:27)
at com.servlet.IndexServlet.doGet(IndexServlet.java:20)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CookieFilter.doFilter(CookieFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:24)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
2011-3-16 15:59:22 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet index threw exception
java.lang.NullPointerException
at com.toolsbean.DB.getRs(DB.java:71)
at com.dao.CategoryDao.getCategoryMap(CategoryDao.java:133)
at com.servlet.IndexServlet.doPost(IndexServlet.java:27)
at com.servlet.IndexServlet.doGet(IndexServlet.java:20)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CookieFilter.doFilter(CookieFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:24)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
我是使用的数据库是SQL Server2005(补丁都打了,和数据库应该没有关系,因为别的章节的数据库在2005中可以运行),数据库驱动msbase.jar mssqlserver.jar msutil.jar已经正确放到lib中
在com.toolsbean.DB里的代码是这样
package com.toolsbean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {
private Connection con;
private PreparedStatement pstm;
private String user="king";
private String password="123";
private String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url="jdbc:sqlserver://localhost:1433;DatabaseName=db_forum";
//jdbc:sqlserver://127.0.0.1:1433;DatabaseName=spc
/** 构造方法,在该方法中加载数据库驱动  */
public DB(){
try{
Class.forName(className);
}catch(Exception e){
System.out.println("加载数据库驱动失败!");
e.printStackTrace();
}
}

/**创建数据库连接*/
public Connection getCon(){
if(con==null){
try {
con=DriverManager.getConnection(url,user,password);
//con.setAutoCommit(true);
} catch (SQLException e) {
System.out.println("创建数据库连接失败!");
con=null;
e.printStackTrace();
}
}
return con;
}

/**
 *@功能:对数据库进行增、删、改、查操作
 *@参数:sql为SQL语句;params为Object数组,里面存储的是为sql表示的SQL语句中"?"占位符赋值的数据 
 */
public void doPstm(String sql,Object[] params){
if(sql!=null&&!sql.equals("")){
if(params==null)
params=new Object[0];
getCon();
if(con!=null){
try{
System.out.println(sql);
pstm=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(int i=0;i<params.length;i++)
pstm.setObject(i+1,params[i]);
pstm.execute();
}catch(SQLException e){
System.out.println("doPstm()方法出错!");
e.printStackTrace();
}
}
}
}

/**
 * @功能:获取调用doPstm()方法执行查询操作后返回的ResultSet结果集
 * @返回值:ResultSet
 */
public ResultSet getRs() throws SQLException{
return pstm.getResultSet();
}
/**
 * @功能:获取调用doPstm()方法执行更新操作后返回影响的记录数
 * @返回值:int
 * @throws SQLException
 */
public int getCount() throws SQLException{
return pstm.getUpdateCount();
}
/**
 * @功能:释放PrepareStatement对象与Connection对象
 */
public void closed(){
try{
if(pstm!=null)
pstm.close();
}catch(SQLException e){
System.out.println("关闭pstm对象失败!");
e.printStackTrace();
}
try{
if(con!=null){
con.close();
}
}catch(SQLException e){
System.out.println("关闭con对象失败!");
e.printStackTrace();
}
}
}


麻烦帮我看看这个是为什么?
分享到:
精彩评论 3
绿草_mrkj
学分:0 LV1
2011-03-16
沙发
 出现这个问题 是因为你连接数据库的去掉没有添加到程序中。
kingxianstar
学分:0 LV1
TA的每日心情
2022-01-10 12:33:44
2011-03-19
板凳
[FIELDSET][LEGEND]引自:1楼[/LEGEND]
 出现这个问题 是因为你连接数据库的去掉没有添加到程序中。
[/FIELDSET]

回复:
  
这个问题我已经解决了,但是有出现了另外一个问题,点击静茹论坛出现访问无效。后台提示:
 Servlet.service() for servlet jsp threw exception
[font color=#FF0000]org.apache.jasper.JasperException: The absolute uri: http://www.mingribook.com/tages/xmenu cannot be resolved in either web.xml or the jar files deployed with this application
[/font] at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:116)
at org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:315)
at org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:148)
at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:431)
at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:494)
at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1444)
at org.apache.jasper.compiler.Parser.parse(Parser.java:138)
at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:216)
at org.apache.jasper.compiler.ParserController.parse(ParserController.java:103)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:154)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:315)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
at org.apache.jsp.visitPage.indexTemp_jsp._jspService(indexTemp_jsp.java:89)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.servlet.CategoryServlet.alllist(CategoryServlet.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.servlet.CategoryServlet.doPost(CategoryServlet.java:37)
at com.servlet.CategoryServlet.doGet(CategoryServlet.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.SubmitFilter.doFilter(SubmitFilter.java:38)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.AbleFilter.doFilter(AbleFilter.java:29)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.LoginFilter.doFilter(LoginFilter.java:39)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CookieFilter.doFilter(CookieFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.PathFilter.doFilter(PathFilter.java:38)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.LimitFilter.doFilter(LimitFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:24)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
看红色的提示,貌似是明日的标签库出了问题。
东北虎
学分:0 LV1
2011-03-22
地板
您好,出现这个问题是缺少标签库文件,请将文件放到相应的位置,然后再试一次。
首页上一页 1 下一页尾页 3 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照