Attempt to connect to SQL Server causes ClassNotFoundException
import java.sql.*;
public class MysqlConnect {
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "\\host/context/";
String dbName = "theDatabaseName";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver ";
String userName = "theUserName";
String password = "thePassword";
try {
Class.forName(driver).newIn开发者_如何学运维stance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am facing problem while the running this code.
I have already downloaded the SQL Server driver (sqljdbc), set it in the class path and copied it into the java/lib directory, but still I am getting the same result: ClassNotFoundException
.
Can anybody help me?
Please check the JDBC URL. The one mentioned by you \\xeon-s5/LDF RAID (G)/
does not seem to be correct URL. A Typical URL will look like jdbc:sqlserver://neptune.acme.com:1433
. Check out this further. Here how to connect to SQL server from java.
You don't have 'com.microsoft.sqlserver.jdbc.SQLServerDriver' class in your classpath.
Make sure you have following jars in your CLASSPATH: Msbase.jar, Msutil.jar, Mssqlserver.jar
More details here: http://support.microsoft.com/kb/313100
You have a trailing space at the end of your class name.
"com.microsoft.sqlserver.jdbc.SQLServerDriver "
should most likely be
"com.microsoft.sqlserver.jdbc.SQLServerDriver"
精彩评论