0
votes
 import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class JdbcSQLServerConnection {

        public static void main(String[] args) throws ClassNotFoundException {



            Connection conn = null;
            try {


              String url = "jdbc:sqlserver://microsoft\\SQLEXPRESS;databaseName=crud";
              String userName = "sa";
              String password = "pwd";
              System.out.println("Connected2");
              Class.forName("com.sqlserver.jdbc.SQLServerDriver");
              conn = DriverManager.getConnection(url, userName, password);


                System.out.println("Connected");
                if (conn != null) {
                    DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
                    System.out.println("Driver name: " + dm.getDriverName());
                    System.out.println("Driver version: " + dm.getDriverVersion());
                    System.out.println("Product name: " + dm.getDatabaseProductName());
                    System.out.println("Product version: " + dm.getDatabaseProductVersion());
                }

            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (conn != null && !conn.isClosed()) {
                        conn.close();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

output:-

Connected2 Exception in thread "main"
java.lang.ClassNotFoundException: com.sqlserver.jdbc.SQLServerDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:28)
I have included sqljdbc4.jar in eclipse library but still it is not working on my system please help me in this solution`enter code here.
1
How to solve this really depends on your "project type." For example, if you're using a Maven or Gradle project you can use it to resolve the dependency by using groupId com.microsoft.sqlserver, artifactId sqljdbc4, and version 4.0.Emmanuel Rosa
The name of the driver is com.microsoft.sqlserver.jdbc.SQLServerDriver, not com.sqlserver.jdbc.SQLServerDriverMark Rotteveel

1 Answers

3
votes

Obviously the class you try to load (com.sqlserver.jdbc.SQLServerDriver) is not found.

According to Microsoft Docs "Using the JDBC Driver" you have to load the JDBC-Driver by calling Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); -- note the microsoft between com and sqlserver

If this does not help, you might want to

  1. Compile your code and run it on the command line, manually adding the JDBC driver JAR to the classpath (-cp) to get rid of any pitfall in Eclipse.

  2. Rename the sqljdbc4.jar to sqljdbc4.zip, unzip it and check whether the class you try to load really exists in there...