java.sql.Exception:没有为jdbc:sqlserver找到合适的驱动程序...使用集成安全性

时间:2017-09-20 19:05:43

标签: java sql-server jdbc

我已阅读各种帖子,但无法将我的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---" + "\r\n");



                    // 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) {}
                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

您的连接字符串不正确。

你在开头错过了两个正斜杠。

您的连接字符串应如下所示:

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

请参阅解释如何构建SQL Server连接字符串的design library 23.2.0

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

相关问题