我有一个网站开发项目使用本地安装的Tomcat 7
。我正在尝试使用微软的jdbc驱动程序(sqljdbc41.jar
)连接到sqljdbc41.jar
。
sqljdbc41.jar
位于我的应用程序构建路径中:
我要出口它。此外,在Tomcat应用程序目录lib
文件夹中,我还放置了sqljdbc41.jar
的副本。
没有编译错误,但在运行时,当我试图加载Server驱动程序时,会得到以下信息:
ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver
异常将在以下代码块中引发:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://"+server+":1433;databaseName="+database+";user=sa;password="+password+";";
con = (Connection) DriverManager.getConnection(connectionUrl);
我看到许多关于这一主题的帖子没有得到解决:
还有更多。
编译器级别为1.7和JRE为1.7 -根据文档,我相信我使用的是正确的驱动程序。这也说明必须设置CLASSPATH:
echo $CLASSPATH
/var/common/sqljdbc41.jar
这就是事实。此外:
java -version
java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM)
64-Bit Server VM (build 24.75-b04, mixed mode)
那么,为什么我还会遇到这个?
更新
我再次从微软下载了sqljdbc41.jar
--只是为了确保第一个jar没有损坏。
按照Mnemonic的链接,我从Java路径中删除了jar,并将新下载的jar放到web应用程序的WEB-INF/lib
文件夹中。然后我重新启动Eclipse和Tomcat服务器,并清理Tomcat服务器和项目。
还能拿到ClassNotFoundException
。
另外,Eclipse可以看到驱动程序:
发布于 2015-12-15 21:05:42
代码Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
不能抛出ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver
因为名字不一样。是否有可能在代码中设置错误?
我从他们的网站下载了sqljdbc41.jar,并看到这个类的正确名称是com.microsoft.sqlserver.jdbc.SQLServerDriver
。
$ jar tf sqljdbc41.jar | grep SQLServerDriver.class
com/microsoft/sqlserver/jdbc/SQLServerDriver.class
我刚刚在微软的web文档中找到了这两个名字,所以它们要么在某个时候重命名这个类(更改了它的包),要么它们的一些文档出现了错误。
您所需要做的就是将该.jar放到Tomcat的lib目录(例如apache-tomcat-7.0.67\lib
)中,然后重新启动Tomcat。
如果您有正确的类名,而且lib目录中有正确的jar,并且仍然看到该错误,我想知道您的eclipse设置中是否有某种类型的错误,而部署from不知怎么会迫使加载这个损坏的类名。(我不使用Eclipse,也不知道如何从那里部署)。
尝试创建一个非常简单的应用程序(不要告诉eclipse有关MS驱动程序类的信息):
@WebServlet("/")
public class SimpleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Set response content type
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<h1>" + "Welcome to the servlet!" + "</h1>");
try {
String server = "localhost";
String database = "testDB";
String password = "sapassword";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://"+server+":1433;databaseName="+database+";user=sa;password="+password+";";
Connection con = (Connection) DriverManager.getConnection(connectionUrl);
} catch (ClassNotFoundException e) {
out.println("<h2>" + e.getClass().getSimpleName() + "_" + e.getMessage() + "</h2>");
} catch (SQLException e){
out.println("<h2>" + e.getClass().getSimpleName() + "_" + e.getMessage() + "</h2>");
} finally {
out.println("<h1>" + "That's the end of the servlet!" + "</h1>");
}
}
}
并运行它。如果您看到的输出如下:
Welcome to the servlet!
SQLServerException_The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
That's the end of the servlet!
这意味着司机装得很好。连接失败的b/c,我目前没有运行SQLServer实例来对其进行测试。
https://stackoverflow.com/questions/34210769
复制相似问题