com.microsoft.sqlserver.jdbc.SQLServerDriver not found error

前端 未结 8 1856
暖寄归人
暖寄归人 2021-02-13 14:56

I am trying to connect to my SQL Server 2008 database from Java and I\'m having the same problem from this thread.

String userName = \"xxxx\";
String password =          


        
相关标签:
8条回答
  • 2021-02-13 15:06

    here is your answer

    String userName = "xxxx";
        String password = "xxxx";
        String url = "jdbc:sqlserver:xxx.xxx.xxx.xxx;databaseName=asdfzxcvqwer;integratedSecurity=true";
    
        try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                connection = DriverManager.getConnection(url, userName, password); 
    
      } catch (Exception e)
      {
         e.printStackTrace();
      }
    
    0 讨论(0)
  • 2021-02-13 15:08

    You dont need both jTDS and JDBC in your classpath. Any one is required. Here you need only sqljdbc.jar.

    Also, I would suggest to place sqljdbc.jar at physical location to /WEB-INF/lib directory of your project rather than adding it in the Classpath via IDE. Then Tomcat takes care the rest. And also try restarting Tomcat.

    You can download Jar from : www.java2s.com/Code/JarDownload/sqlserverjdbc/sqlserverjdbc.jar.zip

    EDIT:

    As you are supplying Username and Password when connecting,

    You need only jdbc:sqlserver://localhost:1433;databaseName=test, Skip integratedSecurity attribute.

    0 讨论(0)
  • 2021-02-13 15:15

    You are looking at sqljdbc4.2 version like :

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    

    but, for sqljdbc4 version statement should be:

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    

    I think if you change your first version to write the correct Class.forName , your application will run.

    0 讨论(0)
  • 2021-02-13 15:15

    For me it worked after manually copying the sqljdbc4-2.jar into WEB-INF/lib folder. So please have a try on this too.

    0 讨论(0)
  • 2021-02-13 15:21
    public static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=dbName";
    public static final String USERNAME = "xxxx";
    public static final String PASSWORD = "xxxx";
    
    /**
     *  This method
        @param args     command line argument
    */
    public static void main(String[] args)
    {
       try
       {
            Connection connection;
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());   
            connection = DriverManager.getConnection(MainDriver.URL,MainDriver.USERNAME,
                            MainDriver.PASSWORD);
            String query ="select * from employee";
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while(resultSet.next())
            {
                System.out.print("First Name: " + resultSet.getString("first_name"));
                System.out.println("  Last Name: " + resultSet.getString("last_name"));                
            }
       }catch(Exception ex)
       {
            ex.printStackTrace();
       }
    }
    
    0 讨论(0)
  • 2021-02-13 15:21

    For me it was a wrong maven dependency deceleration, so here is the correct way:

    for sqljdbc4 use: sqljdbc4 dependency

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>
    

    for sqljdbc42 use: sqljdbc42 dependency

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc42</artifactId>
        <version>6.0.8112</version>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题