无法连接到sqlserver2008的问题
在web项目下的lib文件夹里包含了sqljdbc.jar和sqljdbc4.jar。连接数据库代码 try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=userdatabase","sa","sa");
}catch (Exception e) {
// TODO: handle exception
System.out.println("-----------------------faild---------------------");
e.printStackTrace();
}
return ct;
报错是:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1484)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1329)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.swu.model.ConnectDB.getConn(ConnectDB.java:10)
at com.swu.model.GoodBeanBO.getGoodsBean(GoodBeanBO.java:13)
at com.swu.control.showGoodsClServlet.doGet(showGoodsClServlet.java:33)
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 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:127)
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:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:722)
java.lang.NullPointerException我觉得不应该啊,驱动我是加了的啊,为什么会报错呢???
------解决方案--------------------
问题很明显了,驱动没找到。
检查驱动包是否正确,是否准确引入,是否放在服务器相关目录下,如tomcat的lib
------解决方案--------------------
没加载上驱动,把架包放在webroot,lib下面