public class Main {
public static void main(String[] args) {
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=实验6";//数据源 !!!!注意若出现加载或者连接数据库失败一般是这里出现问题
String Name="sa";
String Pwd="123456";
try{
Class.forName(driverName);
System.out.println("加载驱动成功!");
}catch(Exception e){
e.printStackTrace();
System.out.println("加载驱动失败!");
}
try
{
Connection conn=DriverManager.getConnection(dbURL,Name,Pwd);
System.out.println("连接数据库成功");
}catch(Exception e){
e.printStackTrace();
System.out.println("连接失败");
}
}
}
加载驱动成功!
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3157)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:82)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3121)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7151)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2478)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1687)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1528)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:866)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:569)
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:23)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 14 more
2 个解决方案
#1
参考这个帖子
https://bbs.csdn.net/topics/392395716?list
https://bbs.csdn.net/topics/392395716?list
#2
这个也参考一下
https://bbs.csdn.net/topics/392397868
https://bbs.csdn.net/topics/392397868
#1
参考这个帖子
https://bbs.csdn.net/topics/392395716?list
https://bbs.csdn.net/topics/392395716?list
#2
这个也参考一下
https://bbs.csdn.net/topics/392397868
https://bbs.csdn.net/topics/392397868