Reputation: 1
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcSQLServerConnection {
public static void main(String[] args) throws ClassNotFoundException {
Connection conn = null;
try {
String url = "jdbc:sqlserver://microsoft\\SQLEXPRESS;databaseName=crud";
String userName = "sa";
String password = "pwd";
System.out.println("Connected2");
Class.forName("com.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected");
if (conn != null) {
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " + dm.getDatabaseProductName());
System.out.println("Product version: " + dm.getDatabaseProductVersion());
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
output:-
Connected2 Exception in thread "main" java.lang.ClassNotFoundException: com.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 JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:28)I have included sqljdbc4.jar in eclipse library but still it is not working on my system please help me in this solution`enter code here.
Upvotes: 0
Views: 2914
Reputation: 533
Obviously the class you try to load (com.sqlserver.jdbc.SQLServerDriver
) is not found.
According to Microsoft Docs "Using the JDBC Driver" you have to load the JDBC-Driver by calling Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
-- note the microsoft
between com
and sqlserver
If this does not help, you might want to
Compile your code and run it on the command line, manually adding the JDBC driver JAR to the classpath (-cp
) to get rid of any pitfall in Eclipse.
Rename the sqljdbc4.jar
to sqljdbc4.zip
, unzip it and check whether the class you try to load really exists in there...
Upvotes: 3