1

I have the above error and am wondering what to do. I have done the following things already:

  1. downloading the sqljdbc4.jar from Microsofts website
  2. installing it to my local maven repository
  3. including it in pom.xml like this:

    com.microsoft.sqlserver sqljdbc4 4.0

Since I am using jdbc4, I read that I do not have to call Class.forName, but can build the connection to the database directly. So why do I still get the nosuitabledriver error?

EDIT: When using ClassForName like below, I get a ClassNotFoundException.

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //I also tried Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
String dbURL = "jdbc:sqlserver://localhost;databaseName=test;integratedSecurity=trues";
conn = DriverManager.getConnection(dbURL);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kevin Wu
  • 1,357
  • 1
  • 15
  • 34
  • Could you try with `Class.forName` to be sure ? – Arnaud Nov 10 '16 at 10:18
  • please show your code that you had try to connect to DB. – Ye Win Nov 10 '16 at 10:20
  • tried that, but then get "ClassNotFoundException":) – Kevin Wu Nov 10 '16 at 10:20
  • I mean, please post your Java code and if necessary pls also post your project structure, so we can give more help. – Ye Win Nov 10 '16 at 10:21
  • I edited my question. Thanks for helping. – Kevin Wu Nov 10 '16 at 10:24
  • 1
    if you face ClassNotFoundException at Class.forName, it mean you don't have the valid sqljdbc4 java at your java classpath, please check and expand your jar from Java class path and make sure there have this package ("com.microsoft.jdbc.sqlserver.SQLServerDriver") – Ye Win Nov 10 '16 at 10:25
  • And also that is the correct path Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); – Ye Win Nov 10 '16 at 10:27
  • as said in the code comment, I tried that too. I also tried this:File->Project->Module->Dependencies and then add the jar. The IntelliJ IDE should thereby add it to the classpath. When expanding the External Library Section, I can see that the class is there. – Kevin Wu Nov 10 '16 at 10:28

2 Answers2

2

First you need to make sure your Driver classpath to correct classpath:

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

Changed to:

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

Second you need to make sure your dbURL to correct dbURL:

String dbURL = "jdbc:sqlserver://localhost;databaseName=test;integratedSecurity=trues";

Change to:

String dbURL = "jdbc:sqlserver://localhost:1443;databaseName=test;integratedSecurity=trues";

Third, add your DB username and password in getConnection method:

conn = DriverManager.getConnection(dbURL);

Change to:

conn = DriverManager.getConnection(dbURL,"yourUserName","yourPass");

hope it helps.

Ye Win
  • 2,020
  • 14
  • 21
  • @KevinWu, after these 3 are correct, if you have still error, please let me know that errors. – Ye Win Nov 10 '16 at 10:32
  • well, it doesn't really solve the problem. The error is at Class.ForName and not during connection. – Kevin Wu Nov 10 '16 at 10:34
  • ClassNotFoundException again? – Ye Win Nov 10 '16 at 10:34
  • not from External jar, you need to check sql jar is have in your java classpath – Ye Win Nov 10 '16 at 10:39
  • How do I do that? – Kevin Wu Nov 10 '16 at 10:48
  • Go to Project-> Libraries and add the directory folder that has the JARs. Everything in that folder will be added to CLASSPATH. – Ye Win Nov 10 '16 at 10:48
  • other link that help you to have your jar in classpath : http://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project – Ye Win Nov 10 '16 at 10:53
  • Ok, now it works. Instead of adding module dependency in project level, I added it to main. Are you aware of the difference? – Kevin Wu Nov 10 '16 at 10:55
  • I don't understand "I added it to main", anyway now you have your jar in classpath and your program will work if code config settings are correct. pls refer my answer again for config if you face any new errors. – Ye Win Nov 10 '16 at 10:57
  • When you add dependencies, you have the option of attaching to the project, main or test – Kevin Wu Nov 10 '16 at 10:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127796/discussion-between-ye-win-and-kevin-wu). – Ye Win Nov 10 '16 at 10:59
-1

I added the external module to main now instead of the project directory. I assume that I need to do the latter before I deploy the application. But now, the error is gone.

Kevin Wu
  • 1,357
  • 1
  • 15
  • 34