• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Senior,
This is my first post in Coderanch.

I am trying to connect sqlserver 2014 with netbeans java.


To do this ,I have following settings

Installed these packages

jdk-15.0.1
jre1.8.0_271
sqljdbc_8.4.1.0_enu

Classpah=C:\Program Files\Java\jdk-15.0.1\bin;C:\Program Files\Java\jre1.8.0_271\bin
path=C:\Program Files\Java\jdk-15.0.1\bin;C:\Program Files\Java\jre1.8.0_271\bin


I have everything correct but still facing problem

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

Please help me

1-instllaion_folder.png
[Thumbnail for 1-instllaion_folder.png]
2-driver.png
[Thumbnail for 2-driver.png]
3-classpath.png
[Thumbnail for 3-classpath.png]
 
Rancher
Posts: 4786
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters you don't need to set a CLASSPATH environment variable.
That usually causes more trouble than it's worth.

Is this a Java app somewhere?
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir I have removed Classpath but problem is still exists.
 
Marshal
Posts: 71729
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You shouldn't have a System CLASSPATH, but you do need a CLASSPATH for your application, so the java and javac tools can find that driver resource. Have a look in the Java™ Tutorials, but don't update it as you would the PATH. Look up the javac and java tools and look up the -cp option (also called -classpath or --class-path). You want something like

javac -cp .;"C:\Program Files\SQLServer\drivers" MyApp.java

...or whatever the location of the driver is. I am assuming you are using Windows®. The dot means, “current directory.” Yes, you do need to include the current directory, or the location of your XXX.java files, otherwise you won't find them. You might only need the quote marks if there are spaces in the names of your folders.

[edit]You have the real location of your driver .jar, and I didn't know it.
 
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the driver classes are in a jar file, then the jar file needs to be on the classpath:


javac -cp .;"C:\Program Files\SQLServer\drivers\TheJarfile.jar" MyApp.java

 
Dave Tolls
Rancher
Posts: 4786
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, this is an IDE, Netbeans specifically, so that won't really apply.

Hence my question of whether this is a Java app, or whether this is trying to use Netbeans as a DB front end.
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted here: https://www.dreamincode.net/forums/topic/420557-commicrosoftsqlserverjdbcsqlserverdriver/

Please read:  https://coderanch.com/wiki/660346/Wiki/Forthright-Cross-Posting-Sites
 
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. The SQL Server JDBC driver jar should NOT be placed in the JDK directory. In fact, Java's anti-tampering system may have excluded it for that very reason.

2. CLASSPATH does not point to "bin" directories. It points to directories containing classes. And/Or to JAR files.

3. Putting 2 different JDK/JRE  versions in a CLASSPATH doesn't help. You shouldn't put JDK's in the CLASSPATH, because Java infers their location from where it found the Java executable.

4. Putting 2 different JDK/JRE versions in a CLASS doesn't help either. Only the first one in the list would be used, so in your example, it would use version 15, not 18. The PATH points to the bin directory of the JDK or JRE. Note that one difference between a JDK and a JRE is that a JDK has 2 bin directories. Only the upper-level bin directory should be in the PATH. The JDK contains an embedded JRE, which is why there's a lower-level bin directory as well, but it's found automatically by the upper-level stuff where needed.

5. Note to Norm: Ordinarily you should not include the JDBC driver jar in the CLASSPATH of the javac (compile) command. It's only needed at runtime (the java command). Unless you're planning on using proprietary extensions to the driver and I recommend avoiding that. In fact, you wouldn't even need to set CLASSPATH on javac unless you're pulling in outside code.

Hope that clears up a few things.
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There wasn't any code posted so you can not see if there is an import statement(or reference) for any classes for the SQL driver.  If there were, the javac command needs the classpath to point to the jar file with the classes.
If  the code does not use any of the classes in the jar file, there is no need to have the classpath point to the jar file for the javac command.
 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:There wasn't any code posted so you can not see if there is an import statement(or reference) for any classes for the SQL driver.  If there were, the javac command needs the classpath to point to the jar file with the classes.
If  the code does not use any of the classes in the jar file, there is no need to have the classpath point to the jar file for the javac command.



Quite correct. But as I said, it's not recommended to use driver-specific code in JDBC and doubly-so for beginners.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir here are codes



I have everything that you said, but error is still exists.
could you please tell me more what should I verify next?

Regards
 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove the Class.forName() statement. It has been obsolete for about 15 years.

The ClassNotFoundException comes from not having your SQLServer driver JAR file in your CLASSPATH.

Once you fix that:

I'm not confident that this URL is valid:


Backslashes are not supposed to be part of a URL. Assuming that your SQL Server host is on machine ZANIB-PC, then the proper URL is more likely to be:


However, the common JDBC syntax practices usually puts the database name right after the hostname (so you wouldn't need a dataBaseName option). Then again SQL Server is weird, That might be the database ownername.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seniors

This time I have following image

https://imgur.com/a/D3pnyz9



it says: Connection Succeeded
It shows also driver

but when I use these codes



then the result is

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://ZANIB-PC/SQLEXPRESS:1433;databaseName=at20;user=sa;password=123;

I have only 1 folder in JC:\Program Files\Java
jdk-15.0.1

Should I add driver here?

https://imgur.com/a/GFUYiKO


OR here
https://imgur.com/a/NO0tGeo


Please help me at this last stage

Regard







 
Dave Tolls
Rancher
Posts: 4786
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you running this in order to see that exception?

When your code runs it does not use the Netbeans DB connection.
They are two unrelated things.

The jar file needs to be part of your project (not stuck in some random part of your PC).
 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I checked the Microsoft docs and the element after the servername is the instancename and Microsoft uses the   backslash. Trust Microsoft to screw things up.

However, the instancename probably isn't required here, since that would be for a more complicated server configuration. The more straightforward URL would be:

Using default values of instancename (SQLEXPRESS) and port (1433).

Beyond that, what I'm seeing is a typical case of why we say that the best "IDE" is Windows Notepad. You don't code "Netbeans Java" applications, you code Java® applications. The classpath inside the Netbeans IDE is one thing. The classpath when running a Java app stand-alone ins a very different environment. If you only know how to do things in an IDE you will be crippled - production applications don't run in IDEs: they are only development aids.

The "No suitable driver" message comes when the JDBC URL doesn't match any of the URL patterns defined in the JDBC driver(s) in the class path. In this case, it's probably because I recommended turning the backslash into a slash. Again, curse you, Microsoft!

Try my simplified URL and see what that gives.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir TIM

Thanks for helping
but...

still my bad luck



https://imgur.com/a/afARFNT

I am very upset sir, all has went wrong.

May be somebody solve my problem online while checking driver files in project.

My anydesk id
681 514 267

Regards

 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your picture links don't display. I think it's because they don't have "file extensions" on the URL.

Do this:

1. Uncomment the Class.forName() statement. Not because JDBC wants it but because it will cause the classloader to look for the SQL Server driver. Until you get your project to stop throwing the ClassNotFoundException on the ClassForName() your project is not including the SQL Server JDBC driver jar in the classpath and if the SQL Server driver is not found will result in a "failure to find a suitable driver" error.

2. Please take note: The SQL Server driver jar should be included as part of the project test run profile's classpath in NetBeans. It has to be included via the --classpath command line option or via CLASSPATH environment when executing the application from a command line prompt, as command line knows nothing about NetBeans.

3. Comment/remove Class.forName() once JDBC works properly, since that was just to test classloading and is not used, as I said before, by JDBC itself.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected Sir,

I used these codes now



uncommented Class.forName
commented DriverManager

Facing same error



it means driver not found

Sir please believe me I have add relevant jar files into project.
How to show you here. this forum does of support images to add in thread.

I have sucked here badly.

I request you again please have a look at my project.

Thanks in advance

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add this statement  before the Class.forName call to display what classpath is passed to the java program:

Copy the output and paste it here.

When I use that statement I get:


Running: "C:\Program Files\Java\jdk1.8.0_60\jre\bin\java.exe" -cp .;pdfbox-app-2.0.8.jar TestCode26  

java.class.path=.;pdfbox-app-2.0.8.jar

0 error(s)

 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


java.class.path=C:\Users\zanib\Documents\NetBeansProjects\crud\target\classes
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would expect to see the path to the jar file with the missing class.  I don't know the IDE and can not suggest how to have it include the needed path.
 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tariqq Mehmoodd wrote:

java.class.path=C:\Users\zanib\Documents\NetBeansProjects\crud\target\classes
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver



Notice that the mssql-jdbc jar isn't in that list? Until it is, that's what you'll get.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected Boss

I am using Netbeans 12
Please see if something is wrong in images.


https://imgur.com/a/pA2AZ0m


https://imgur.com/a/09j1U6A


https://imgur.com/a/mdK228T





https://imgur.com/a/Lp7wKhv



Regards

 
Marshal
Posts: 26300
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first two are the operating system's environment variables. Netbeans doesn't use them.

The second two: I don't use Netbeans but it looks like you're configuring a database in Netbeans. Perhaps the next step would be to configure your Netbeans project to use that database?

Actually I just searched the web for a tutorial about how to use JDBC in Netbeans. I found this one for example: https://www.developer.com/java/data/creating-a-jdbc-application-in-netbeans-a-step-by-step-guide.html

It has a whole lot of irrelevant (for you) details, so skip directly to Step 4. Yes, it says "MySQL" and you've got SQL Server, but the point is to associate the JDBC driver's jar file with your project. Step 4 explains how to do that.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all participants
Finally problem solved

I followed the instruction in this link

https://www.microsoft.com/en-us/sql-server/developer-get-started/java/windows/step/2.html

added this dependency


Now everything is ok

Thanks again

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you copy and paste here what this line prints out when the code is working?


That will help us in the future to debug this type of problem.
Thanks.
 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you copy and paste here what this line prints out when the code is working?


That will help us in the future to debug this type of problem.
Thanks.



Tariqq's solution won't help much here. He's using Maven to create and execute a JAR.

I haven't seen Maven used this way before, but if it helps him get started with JDBC, who am I to complain? He's suffered enough.

Now, unless I've missed something, this is NOT creating an executable JAR, it's creating a normal JAR without including the dependencies into the target. That means that the app shouldn't be able to run without Maven's help.

In fact, a quick check seems to indicate that Maven is creating a JAR with only his code in it and the "exec" goal is apparently adding the local cached junit and SQL server jars to the execution CLASSPATH.

I doubt that the NetBeans environment is set up for this, since it's not how IDEs normally run apps, but it is good enough to test from the command line and that's a start.

For the record, the last time I worked with "NetBeans", it was still known as "Forte", which is why I can't help on that part.

Anyway while Tariq's solution may be a bit shuddersome, his persistence has been admirable, so I'm giving him a cow!
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! What a lot of configuring.  In the end will the OP have a program that can be executed in a command prompt using the java command without having NB or maven around?
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you copy and paste here what this line prints out when the code is working?


That will help us in the future to debug this type of problem.
Thanks.




java.class.path=C:\Users\zanib\Documents\NetBeansProjects\crud\target\classes;C:\Users\zanib\.m2\repository\com\microsoft\sqlserver\mssql-jdbc\8.2.2.jre8\mssql-jdbc-8.2.2.jre8.jar
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.  That shows the needed jar file is on the classpath.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:
In fact, a quick check seems to indicate that Maven is creating a JAR with only his code in it and the "exec" goal is apparently adding the local cached junit and SQL server jars to the execution CLASSPATH.



Respected Sir, What does it mean?

Am I doing something wrong which can create trouble in future?
Actually I am new in JAVA, I am learning with google.
Please guide me if I am at wrong way.

Thanks
 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Wow! What a lot of configuring.  In the end will the OP have a program that can be executed in a command prompt using the java command without having NB or maven around?



No. Maven exec is creating a classpath which includes the JARs I mentioned from the user's local Maven repository cache (~/.m2/repository) and then executing the main class. Maven is not creating an app that can be run "as is" without Maven.

To execute the code without Maven, one would have to either create an executable JAR or run the Java command with a classpath that explicitly referenced those JARs or copies of those JARs. And, again, NetBeans has its own mechanisms, but I'm not familiar with those.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sir for helping

This time this simple command is working properly



Thanks again
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you causing the java program to be executed?  Is it via some button or menu item in NB?
Or are you executing the java command in a command prompt window?
Or how?
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like this sir

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Like this sir


Is that post a response to my question about how the code was executed?  Source code is just text, It does not show how it  can be executed.
 
Tim Holloway
Saloon Keeper
Posts: 23038
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm want to know whether you did your "mvn exec" from a DOS prompt or by some sort of command to NetBeans.

I'm guessing it was a DOS prompt, myself.
 
Tariqq Mehmoodd
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected sir's.

I added this dependency in pom.xml

https://imgur.com/a/d2Rfami

Nothing else

At this stage I am not learning how to make this project Executable.

I am just trying to create CRUD.

I have been working with Visual Foxpro for the last 20 years.
Developing all types of softwares.

Just trying to move to JAVA.

Regards
 
Maybe he went home and went to bed. And took this tiny ad with him:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic