javasql-serverintellij-ideasqljdbc

JDBC jar added to Classpath still receiving error


I'm testing my connection to a database. I cannot get this to compile properly. I have added the sqljdbc4.jar to the classpath via the Intellij IDE (perhaps it was done incorrectly?) but it is showing up under Classpath from Open Modules under the Project Structures. How do I solve this?

Here's my error:

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.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 Automate.dbConnect(Automate.java:16)
at Automate.main(Automate.java:34)

Here's the code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

class Automate {
    public void dbConnect(String db_connect_string,
                      String db_userid,
                      String db_password)
{
    try {
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

        Connection conn = DriverManager.getConnection(db_connect_string,
                db_userid, db_password);
        System.out.println("connected");
        Statement statement = conn.createStatement();
        String queryString = "select * from sysobjects where type='u'";
        ResultSet rs = statement.executeQuery(queryString);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void main(String[] args)
{
    Automate connServer = new Automate();
    connServer.dbConnect("jdbc:sqlserver://SERVER, "USERNAME",
            "PASSW");
}

Solution

  • My problem was I was using an outdated JDBC class name.

    This was the fix:

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

    Small bug..big problem