0

How can I connect to Microsoft SQL Server from my java code?

Code:

public class insert {

public static void main(String[] args) {
        try {

            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            String url = "jdbc:microsoft:sqlserver://LENOVO-PC\\SQLEXPRESS;DatabaseName=dbtest";
            Connection connection = DriverManager.getConnection(url , "sa" , "Aa123456");



            Statement st = connection.createStatement();
            st.executeUpdate("INSERT INTO [dbo].[table] VALUES ('come')");

        }
             catch (ClassNotFoundException e)
                {
                  e.printStackTrace();
                  System.exit(1);
                }
                catch (SQLException e)
                {
                  e.printStackTrace();
                  System.exit(2);
                }

}

     }

Error:

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at insert.main(insert.java:12)

Microsoft SQL Server name is: LENOVO-PC\SQLEXPRESS and sqljdbc.jar is already added to the referenced libraries.

1
  • Do ypu have driver library in your classpath? Commented Sep 6, 2018 at 4:35

2 Answers 2

1

The class name com.microsoft.jdbc.sqlserver.SQLServerDriver you're trying to load is from a very old version of the Microsoft SQL Server 2000 version of the driver. Around 2005, Microsoft changed this to com.microsoft.sqlserver.jdbc.SQLServerDriver (notice the switch in order between jdbc and sqlserver). At that time, they also changed the driver URL prefix from jdbc:microsoft:sqlserver: to jdbc:sqlserver:.

In other words, you need to:

  1. Use Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") (optional, driver will be loaded automatically)
  2. Change your URL to jdbc:sqlserver://LENOVO-PC\\SQLEXPRESS;databaseName=dbtest (note the prefix change and DatabaseName -> databaseName. See also Building the Connection URL.

With recent JDBC drivers, it is not necessary to explicitly load the driver class in simple Java applications, so instead of step 1, you could also remove the Class.forName line.

Also make sure you are using a recent version of the driver, see the mssql-jdbc project on GitHub (latest stable version at time of writing is 7.0.0).

Sign up to request clarification or add additional context in comments.

3 Comments

... and also use a current release of the driver itself.
@GordThompson True, but the fact the OP mentioned they are using sqljdbc.jar is an indication they are at least using a version newer than the Microsoft SQL Server 2000 JDBC driver (which iirc had sqlserver.jar + maybe some other dependencies). I've added an explicit reminder in my answer.
I also followed the below to enable port 1433: Got to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager ->Expand SQL Server Network Configuration-> Protocol ->Enable TCP/IP Right box Double Click on TCP/IP and go to IP Adresses Tap and Put port 1433 under TCP port.
0

Correct the driver class name:

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

1 Comment

That in itself will not be enough, as also the URL is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.