java.sql.exception:没有找到适合JDBC的驱动程序:SQLServer ..使用集成安全性



我已经阅读了各种帖子,但是无法将我的Java连接到SQL Server 2014 Express的工作:

似乎代码无法找到驱动程序。

我将文件mssql-jdbc-6.2.1.jre8.jar放置在应用程序构建文件夹中。我尝试了有关添加classPath的各种选项,但没有成功,但是对于驱动程序的最新版本来说,这似乎不是必需的。

我不使用IDE

            //===================================================================== 
            import java.sql.*;
            public class connectURL {
                public static void main(String[] args) {

                    System.out.println("----------------------------------Start Connection---" + "rn");

                    // Create a variable for the connection string.
                    String connectionUrl = "jdbc:sqlserver:LocalHost:1433;" +
                        "databaseName=stocksandshares;integratedSecurity=true;";
                    // Declare the JDBC objects.
                    Connection con = null;
                    Statement stmt = null;
                    ResultSet rs = null;
                        try {
                            // Establish the connection.
                                con = DriverManager.getConnection(connectionUrl);
                                // Create and execute an SQL statement that returns some data.
                                String SQL = "SELECT TOP 10 * FROM Person.Contact";
                                stmt = con.createStatement();
                                rs = stmt.executeQuery(SQL);
                                // Iterate through the data in the result set and display it.
                                while (rs.next()) {
                                    System.out.println(rs.getString(4) + " " + rs.getString(6));
                                }
                        }
                    // Handle any errors that may have occurred.
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                    finally {
                        if (rs != null) try { rs.close(); } catch(Exception e) {}
                            if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                            if (con != null) try { con.close(); } catch(Exception e) {}
                    }
                }
            }

您的连接字符串不正确。

您一开始错过了两个前向斜线。

您的连接字符串应如下:

jdbc:sqlserver://LocalHost:1433;databaseName=stocksandshares;integratedSecurity=true;

请参阅此Microsoft文档页面,该页面说明了如何构建SQL Server连接字符串。

并帮自己一个忙,开始使用IDE。

相关内容

最新更新