I am trying to connect to SQL Server 2008 using JDBC. My SQL Server 2008 has windows authentication on it.
My code is
public class T1 {
/**
* @param args
*/
private static void Connect(){
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://servername;databaseName=employee;user=username;password=''/*since it is windows authentication*/;";
java.sql.Connection con = DriverManager.getConnection(connectionUrl);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e2)
{
e2.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
T1.Connect();
}
I end up getting a ClassNotFoundException .
The stack trace is java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader1ドル.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)
-
1) post the stacktrace 2) is connector jar in classpath?guido– guido2013年01月22日 03:32:34 +00:00Commented Jan 22, 2013 at 3:32
-
Did you read Microsoft documentation?Dmitry Zagorulkin– Dmitry Zagorulkin2013年01月22日 03:44:34 +00:00Commented Jan 22, 2013 at 3:44
-
Possibly Duplicated If you are using the command prompt refer here for classpath usage Referencethar45– thar452013年01月22日 04:02:02 +00:00Commented Jan 22, 2013 at 4:02
2 Answers 2
If you're using Eclipse as your development environment, do the following:
- Right-click on your project in eclipse.
- Click on "Build Path" -> "Configure Build Path"
- You will be in the "Libraries" tab by default. Now, click on "Add External Jars" and add the JDBC Driver Jar for MSSQL Server. (In case you don't have it, download it from JDBC Driver for MSSQL Server 2008
- Click on OK and try running your program again.
Reply back if you still face issues.
Comments
A ClassNotFoundException likely means that you don't have the SQL*Server drivers available on your classpath