Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.sql.SQLException:未找到适合jdbc:sqlserver的驱动程序: 问题_Java_Sql Server_Jdbc - Fatal编程技术网

java.sql.SQLException:未找到适合jdbc:sqlserver的驱动程序: 问题

java.sql.SQLException:未找到适合jdbc:sqlserver的驱动程序: 问题,java,sql-server,jdbc,Java,Sql Server,Jdbc,亲爱的各位, 我使用JDBC成功地将JAVA桌面应用程序与MSSQL连接起来。现在,当我尝试使用相同的方法将Java动态Web应用程序与(使用JDBC的MSSQL)连接时,它将显示异常 Sql异常 我从互联网上尝试了很多解决方案,但没有一个对我有帮助。请帮我解决这个问题 下面是所有文件的附件 谢谢 ## DBTesting.Java ## import java.sql.Connection; import java.sql.DriverManager; import java.sql.Res

亲爱的各位, 我使用JDBC成功地将JAVA桌面应用程序与MSSQL连接起来。现在,当我尝试使用相同的方法将Java动态Web应用程序与(使用JDBC的MSSQL)连接时,它将显示异常

Sql异常 我从互联网上尝试了很多解决方案,但没有一个对我有帮助。请帮我解决这个问题

下面是所有文件的附件

谢谢

## DBTesting.Java ##
import java.sql.Connection;
import java.sql.DriverManager; 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
public class DBTesting {
        public  void establishConnection() throws SQLException  {
            
            
            String user = "sa";
            String pass = "ansari12345";    
            String connectionUrl = "jdbc:sqlserver://localhost;" +
                       "databaseName=ReamsDB;user="+user+";password="+pass+";";
            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;
            String sql = "Select * From Basement";
                    
            try {
                 con = DriverManager.getConnection(connectionUrl);
                System.out.println("DB COnnected !");
                } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            while(rs.next())
            {
                int ID = rs.getInt(1);
            String SquareFeetCount = rs.getString(1);
            String _FinishedPercent = rs.getString(1);
            int s_ID = rs.getInt(1); 
                        
            System.out.println("ID :"+ID+"  SquareFeetCount :"+SquareFeetCount+" FinishedPercent :"+_FinishedPercent);
                
            }
            con.close();
            
            }
            
        
}

##Hello.jsp##
在此处插入标题

我阅读了DriverManager代码。
DriverManager.getConnection(字符串url)
调用以下代码:

来自jdk1.7的代码

   private static Connection getConnection(
        String url, java.util.Properties info, Class<?> caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        synchronized (DriverManager.class) {
            // synchronize loading of the correct classloader.
            if (callerCL == null) {
                callerCL = Thread.currentThread().getContextClassLoader();
            }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        // Walk through the loaded registeredDrivers attempting to make a connection.
        // Remember the first exception that gets raised so we can reraise it.
        SQLException reason = null;

        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info);
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex;
                    }
                }

            } else {
                println("    skipping: " + aDriver.getClass().getName());
            }

        }

        // if we got here nobody could connect.
        if (reason != null)    {
            println("getConnection failed: " + reason);
            throw reason;
        }

        println("getConnection: no suitable driver found for "+ url);
        throw new SQLException("No suitable driver found for "+ url, "08001");
    }
私有静态连接getConnection(
字符串url、java.util.Properties info、类调用者)引发SQLException{
/*
*当callerCl为null时,我们应该检查应用程序的
*(正在间接调用该类)
*类加载器,以便JDBC驱动程序类位于rt.jar之外
*可以从这里加载。
*/
ClassLoader callerCL=caller!=null?caller.getClassLoader():null;
已同步(DriverManager.class){
//同步加载正确的类加载器。
if(callerCL==null){
callerCL=Thread.currentThread().getContextClassLoader();
}
}
如果(url==null){
抛出新的SQLException(“url不能为null”,“08001”);
}
println(“DriverManager.getConnection(\“”+url+“\”);
//浏览试图建立连接的已加载的RegisterAddress驱动程序。
//请记住引发的第一个异常,以便我们可以重新验证它。
SQLException-reason=null;
对于(DriverInfo驱动程序:注册驱动程序){
//如果调用方没有加载驱动程序的权限,则
//跳过它。
如果(允许(aDriver.driver,callerCL)){
试一试{
println(“正在尝试”+aDriver.driver.getClass().getName());
Connection con=aDriver.driver.connect(url、信息);
如果(con!=null){
//成功!
println(“getConnection returning”+aDriver.driver.getClass().getName());
返回(con);
}
}catch(SQLException-ex){
如果(原因==null){
原因=ex;
}
}
}否则{
println(“跳过:+aDriver.getClass().getName());
}
}
//如果我们到了这里,没人能联系上。
如果(原因!=null){
println(“getConnection失败:+原因”);
抛出理由;
}
println(“getConnection:未找到“+url”的合适驱动程序);
抛出新的SQLException(“没有为“+url,”08001)找到合适的驱动程序”);
}
如果
RegisteredDriver
不包含sqlserver驱动程序的DriverInfo(或url格式错误)。代码将抛出
SQLException(“找不到适用于“+url”,08001”的合适驱动程序”)

您可以调用
DriverManager.getDriver(字符串url)
来测试是否包含该目录。如果是,我认为是url格式错误。否则,我认为这是一个lib问题。在调用
DriverManager.getConnection
之前,您可以调用
DriverManager.registerDriver(java.sql.Driver-Driver)
或尝试
Class.forName
Class.forName
可以检查lib。如果您没有该类,它将抛出
ClassNotFoundException

变量
registeredDrivers
loadInitialDrivers
方法中初始化。您可以查看该变量以获取更多有用信息


希望这些可以帮助您

不确定它是否能回答您的问题,但对我而言(不在本地主机上工作):
DriverManager.getConnection
没有1个参数,只有3个

如下所示:
stringurl=“jdbc:sqlserver://BCNSDBA042\\开发CP1_CI_AS”;
字符串user=“user”;
字符串passwd=“passwrd”;
Connection conn=DriverManager.getConnection(url、用户、密码)

您的url应该如下所示
“jdbc:sqlserver://localhost:5432/NAME


希望有帮助。

是的,我附加了我在Java桌面项目中附加的同一个Jar文件。@ZephyrGuo这不是答案。如果你想要任何澄清,你应该在评论中询问。你是否加载了dirver类?
class.forName(“com.mysql.jdbc.Driver”)这是一个mysql示例,您的类名是对的吗?不是。因为在driver4中不需要显式调用class.forName(“com.mysql.jdbc.Driver”);这就是为什么我没有添加这段代码。您的web服务器运行库中有正确的jar吗?如果您使用Tomcat,您应该将jar复制到Tomcat库中。
## Hello.jsp ##
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="JDBCServletPath" method="post">
<input type="submit">
</form>
</body>
</html>
   private static Connection getConnection(
        String url, java.util.Properties info, Class<?> caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        synchronized (DriverManager.class) {
            // synchronize loading of the correct classloader.
            if (callerCL == null) {
                callerCL = Thread.currentThread().getContextClassLoader();
            }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        // Walk through the loaded registeredDrivers attempting to make a connection.
        // Remember the first exception that gets raised so we can reraise it.
        SQLException reason = null;

        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info);
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex;
                    }
                }

            } else {
                println("    skipping: " + aDriver.getClass().getName());
            }

        }

        // if we got here nobody could connect.
        if (reason != null)    {
            println("getConnection failed: " + reason);
            throw reason;
        }

        println("getConnection: no suitable driver found for "+ url);
        throw new SQLException("No suitable driver found for "+ url, "08001");
    }