0

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)

asked Jan 22, 2013 at 3:30
3
  • 1) post the stacktrace 2) is connector jar in classpath? Commented Jan 22, 2013 at 3:32
  • Did you read Microsoft documentation? Commented Jan 22, 2013 at 3:44
  • Possibly Duplicated If you are using the command prompt refer here for classpath usage Reference Commented Jan 22, 2013 at 4:02

2 Answers 2

3

If you're using Eclipse as your development environment, do the following:

  1. Right-click on your project in eclipse.
  2. Click on "Build Path" -> "Configure Build Path"
  3. 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
  4. Click on OK and try running your program again.

Reply back if you still face issues.

answered Jan 22, 2013 at 3:55
Sign up to request clarification or add additional context in comments.

Comments

1

A ClassNotFoundException likely means that you don't have the SQL*Server drivers available on your classpath

answered Jan 22, 2013 at 3:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.