?
Solved

Microsoft JDBC Problems

Posted on 2009-02-11
8
Medium Priority
?
398 Views
Last Modified: 2013-11-23
We are getting the following errors when trying to compile Java code using the microsoft JDBC.

Java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:141)
        at DBTest.main(DBTest.java:29)
//CD \Program Files\Java\jdk1.6.0_12\bin
//javac DatabaseTest.java -classpath .;"C:\Program Files\Java\jdk1.6.0_12\lib";
 
import java.sql.*;
 
public class DBTest {
    
    public static void main(String[] args) {
       String ServerName = "TestServer";
       String PortNumber = "1433";
       String UserName = "Test_User";
       String Password = "Test_Password";
		// 2005 version jdbc:sqlserver://
       String url = "jdbc:sqlserver://" + ServerName + ":" + PortNumber + ";databaseName=Test_Database;user=" + UserName + ";password=" + Password;
       String sql;
       sql = "select count(*) from tbl_Job where EndTime between (CAST(convert(varchar(10),getdate(),120)+' 12:00:00' as 	datetime)-1)  and 'GetDate()' and Status = 'Finished' and Job_Title = 'Nightly '
	Group By StartTime, JobName, Status, EndTime HAVING COUNT(*) = 1";
       //System.out.println (sql);
 
 
      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;
 
      try {
         // Establish the connection.
			//2005 Version com.microsoft.sqlserver.jdbc.SQLServerDriver
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(url);
 
         // Create and execute an SQL statement that returns some data.
         stmt = con.createStatement();
         rs = stmt.executeQuery(sql);
 
	 rs.first();
  	 if (rs.getInt(1) > 0) {
	   System.out.println("*****Message Here*****");
	  }	
      }
 
      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }
}

Open in new window

0
Comment
Question by:earngreen
  • 5
  • 3
8 Comments
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 23614233
you need to have the add a classpath point to the MSSQL connector library
or you could move the library to one of the designated classpaths.
or if you use an IDE, check the Add Library function and find the MSSQL connector libraries.

more info. from the Microsoft site: http://support.microsoft.com/kb/313100

Hope that helps,

Andrew
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 23614275
sorry about that, that should be..

"you need to add a classpath that points to the MSSQL connector library"

pretty confusing in the wrong grammar don't you think?

:D
0
 

Author Comment

by:earngreen
ID: 23615184
Can you give me an example?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
gam3r_3xtr3m3 earned 2000 total points
ID: 23615260
when you compiled the one above you did a:

javac DatabaseTest.java -classpath .;"C:\Program Files\Java\jdk1.6.0_12\lib";

for example you have the MSSQL library on C:\mssql\lib, then you should compile it like:

javac DatabaseTest.java -classpath .;"C:\Program Files\Java\jdk1.6.0_12\lib";"C:\mssql\lib";

with the additional classpath after the semicolon.

Andrew
0
 

Author Comment

by:earngreen
ID: 23615467
I do not have a C:\mssql\lib folder. Do I need to install something additionall? I am using a sql 2005 database.
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 23615523
sorry, the folder was only the placeholder for the actual directory, i assumed you had the library with you.

To connect MSSQL with Java in your code, your gonna need the Microsoft SQL Server 2000 Driver for JDBC from Microsoft's download page and then link the folder where the *.jar's are located on that library you just downloaded to the Java classpath. that should do it.

Andrew
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 23615562
its actually depending on your MSSQL version by the way, there is also the 2005 Driver if i'm not mistaken.
if you wish to have an alternative library, then http://jtds.sourceforge.net/ might be the only one.

Andrew
0
 

Author Comment

by:earngreen
ID: 23615810
Ok, yes that it was I did.  i actually put the .jar file in the same directory.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question