我的 servlet 函数如下所示:
代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
String userName;
String passwd;
Connection conn = null;
userName = (String)request.getParameter("userName");
passwd = (String)request.getParameter("password");
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //Or any other driver
}
catch( Exception x ){
System.out.println( "Couldn’t load drivers!" );
}
try
{
conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.123:1433;databaseName=test","sample","sample");
}
catch( Exception x)
{
System.out.println("Couldnot get connection");
}
}
输出进入两个catch语句。如何克服这个问题?
尽快回复?
最佳答案
您是在 Eclipse 中运行它吗?看起来您需要将驱动程序 JAR 文件添加到您的依赖项中。您可以通过 Eclipse 中的项目构建路径设置来执行此操作(右键单击项目,选择构建路径 -> 配置构建路径)。然后,在“库”选项卡下,您可以添加所需的任何 jar,例如 SQL Server 驱动程序 JAR 文件。
如果您将其部署到 Servlet 容器,则 WEB-INF/lib
文件夹中似乎缺少 JAR 文件。将其复制到此处,您应该会发现它有效。
关于java - eclipse中sql server连接出现Classnotfound异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6372815/