Goal is to connect to MS SQL Server database using Java and perform some SQL statements.
Issue while finding the class, Error: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
tried both classname com.microsoft.jdbc.sqlserver.SQLServerDriver
AND com.microsoft.sqlserver.jdbc.SQLServerDriver
Classpath also set in Eclipse to:
CLASSPATH C:/Program Files/Microsoft JDBC Driver 4.1 for SQL Server/sqljdbc_4.1/enu/sqljdbc.jar
also changed win7 enviornment variables to:
.;C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc.jar;C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc4.jar;C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc41.jar;
Used this Code:
package edu.umt.oop.lecture7;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class databasepro {
public static void main(String[] args)
{
Connection connection = null;
try
{
// the sql server driver string
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// the sql server url
String url = "jdbc:microsoft:sqlserver://C-LHE-CS-68541:1433;DatabaseName=CMSA_Console";
// get the sql server database connection
connection = DriverManager.getConnection(url,"sa", "123456");
System.out.println("\nSuccess");
// now do whatever you want to do with the connection
// ...
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
}
}
Complete Error thrown is :
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at edu.umt.oop.lecture7.databasepro.main(databasepro.java:15)