我尝试过很多东西,但仍然遇到同样的错误。我使用的是jdK7和Microsoft sql 2008以及eclipse,jar文件是sqlijdc41.jar(4.1) 我使用构建路径配置构建路径添加了jar文件。
下面是我的代码。
package com.daccess.captureimg;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CaptureImage
*/
@WebServlet("/CaptureImage")
public class CaptureImage extends HttpServlet {
private static final long serialVersionUID = 1L;
public CaptureImage() {
super(); }
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = "jdbc:sqlserver://localhost:60700;databaseName=sampleprojectdb;user=sa;password=dsspl@123";
try {
System.out.println("connecting to the db");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("driver get register");
Connection con=DriverManager.getConnection(url);
System.out.println("connected to the db");
Statement stm=con.createStatement();
stm.executeUpdate("insert into GPSLoaction1 values('123477.7','3467.7','gfasdgfghfsd')");
System.out.println("Value insearted");
} catch (Exception e) { e.printStackTrace(); }
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
以下是控制台中的错误:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.daccess.captureimg.CaptureImage.doGet(CaptureImage.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
请帮助我,我错了。