package sql;
import java.sql.*;
public class DBC {
public static void main(String []args) {
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SA0326";
String userName="sa";
String userPwd="123456";
try
{
Class.forName(driverName); //加载sqlserver的驱动类
System.out.println("加载SQLServer驱动类成功!");
}catch(ClassNotFoundException a){
System.out.println("加载SQLServer驱动失败!");
a.printStackTrace();
}
Connection dbcon=null; //处理与特定数据库的连接
try{
dbcon=DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("数据库连接成功!");
dbcon.close();
} catch(SQLException e){
System.out.println("数据库连接失败!");
e.printStackTrace();
}
}
}
然后提示
加载SQLServer驱动类成功!
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4098)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3160)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3123)
at sqljdbc42/com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1981)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at sqljdbc42/com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at sql.DBC.main(DBC.java:19)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 14 more