java.sql.SQLException:没有为jdbc找到合适的驱动程序:sqlserver:

时间:2015-07-05 13:10:23

标签: java sql-server jdbc

问题

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

Sql Exception

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=ReamsDB;user=sa;password=xxx;

我尝试了许多来自互联网的解决方案,但没有人帮助我。请帮助我摆脱这个问题。

附上所有文件。

感谢。

## 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 ##
<%@ 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>

2 个答案:

答案 0 :(得分:0)

我阅读了DriverManager代码。before_action :admin_user, only: :destroy 调用以下代码:

来自jdk1.7的

代码

DriverManager.getConnection(String url)

如果 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"); } 不包含sqlserver驱动程序的DriverInfo(或url格式错误)。代码将抛出registeredDrivers

您可以调用SQLException("No suitable driver found for "+ url, "08001")来测试是否包含dirver。如果是,我认为这是url格式错误。否则,我认为这是一个lib问题。在致电DriverManager.getDriver(String url)之前,您可以致电DriverManager.registerDriver(java.sql.Driver driver)或尝试Class.forNameDriverManager.getConnection可以检查lib。如果您没有该类,它将抛出Class.forName

变量ClassNotFoundException已在registeredDrivers方法初始化。您可以查看它以获取更多有用信息。

希望这些可以帮助你

答案 1 :(得分:0)

不确定它是否会回答您的问题但不是我的问题(不在当地主持人工作):DriverManager.getConnection没有1参数但是3

像这样:String url = "jdbc:sqlserver://BCNSDBA042\\DEV_CP1_CI_AS"; String user = "user"; String passwd = "passwrd"; Connection conn = DriverManager.getConnection(url, user, passwd);

您的网址应如下"jdbc:sqlserver://localhost:5432/NAME

希望得到帮助。