gpt4 book ai didi

java - 直接从命令行运行 .class 时为 "ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver"

转载 作者:行者123 更新时间:2023-12-01 22:02:56 27 4
gpt4 key购买 nike

我知道这个问题已被问过一百万次,我查找了 100 个链接都指向我正在做的事情,但我无法使其正常工作。

代码:

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

public class ConnectMSSQLServer
{
public static void main(String[] args) {

Connection conn = null;
String dbName = "database1";
String serverip="dv-bi-olap1";
String serverport="1433";
String url = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;user=MyUserName;password=*****;";
Statement stmt = null;
ResultSet result = null;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String databaseUserName = "admin";
String databasePassword = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
result = null;
String pa,us;
result = stmt.executeQuery("select * from table1 ");

while (result.next()) {
us=result.getString("uname");
pa = result.getString("pass");
System.out.println(us+" "+pa);
}

conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

CMD命令:

C:\Users\blabla\Desktop>javac ConnectMSSQLServer.java

类文件已创建

set CLASSPATH =.;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar

C:\Users\blabla\Desktop>java ConnectMSSQLServer

java.lang.ClassNotFoundException: com.microsoft.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 ConnectMSSQLServer.main(ConnectMSSQLServer.java:21)

好吧,这有效,所以我尝试了下面

C:\Users\blabla\Desktop>java -CLASSPATH =.;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar -cp . ConnectMSSQLServer

然后这个

C:\Users\blabla\Desktop>java -CLASSPATH =.;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar ConnectMSSQLServer

所有这些都导致了以下错误

java.lang.ClassNotFoundException: com.microsoft.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 ConnectMSSQLServer.main(ConnectMSSQLServer.java:21)

最佳答案

尝试删除 = 并使用小写的 classpath(或 cp)选项

java -classpath .;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar ConnectMSSQLServer

关于java - 直接从命令行运行 .class 时为 "ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com