JDBC jar added to Classpath still receiving error [duplicate]

試著忘記壹切 提交于 2020-01-03 07:05:05

问题


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");
}

回答1:


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



来源:https://stackoverflow.com/questions/43530285/jdbc-jar-added-to-classpath-still-receiving-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!