Results 1 to 4 of 4
  1. #1
    Parthi1705 is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default MS SQL server+ JDBC Connection

    hi,
    I am new to java i need to connect MS SQL server+ JDBC Connection
    I am getting the below error when i try to connect the code in Eclipse

    import java.sql.*;

    public class SQLServerTest {

    public static void main(String[] args) {

    // Create a variable for the connection string.

    String connectionUrl="jdbc:sqlserver://localhost:1433;DatabaseName=Master";



    // Declare the JDBC objects.
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
    // Establish the connection.
    Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
    con = DriverManager.getConnection(connectionUrl);

    // Create and execute an SQL statement that returns some data.
    String SQL = "Select * from sys.sysobjects where xtype='u'";
    stmt = con.createStatement();
    rs = stmt.executeQuery(SQL);

    // Iterate through the data in the result set and display it.
    while (rs.next()) {
    System.out.println(rs.getString(4) + " " + rs.getString(6));
    }
    }

    // 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) {}
    }
    }
    }

    ERROR:

    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 SQLServerTest.main(SQLServerTest.java:20)

    What would be the reason.

    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: MS SQL server+ JDBC Connection

    The jar file containing the driver needs to be on the runtime classpath.
    How that is setup in your IDE will probably be in the documentation for Eclipse.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Parthi1705 is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: MS SQL server+ JDBC Connection

    Quote Originally Posted by Parthi1705 View Post
    Re: MS SQL server+ JDBC Connection
    The jar file containing the driver needs to be on the runtime classpath.
    How that is setup in your IDE will probably be in the documentation for Eclipse.
    Where does the jar file containing the driver will be , and what needs to be to have runtime classpath .
    I couldn't find any details i just connect Eclipse and coded and getting the error what needs to be modified or needs to be added at Eclipse .


    Thanks!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: MS SQL server+ JDBC Connection

    You'll need to download it from Microsoft.
    Then add it into your project, but exactly how is an Eclipse thing, so read up on the Eclipse documentation.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JDBC connection
    By Dineshmtech in forum JDBC
    Replies: 2
    Last Post: 01-07-2012, 08:20 AM
  2. JDBC Connection
    By Riyaz.hk77 in forum New To Java
    Replies: 2
    Last Post: 11-06-2010, 05:53 PM
  3. Replies: 0
    Last Post: 12-01-2009, 10:01 PM
  4. JDBC Connection...
    By onlysumitg in forum JDBC
    Replies: 3
    Last Post: 08-21-2009, 11:34 AM
  5. SQL Server 2005 Jdbc connection
    By mgt83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-27-2007, 04:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •