SQL server+ JDBC Connection

  • hi,

    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.SQLServerDriver");

    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

  • Hi,

    Any one got the same error and resolved it.

    Thanks!

  • I am guessing you will have better luck on a java forum since this is clearly an issue with java connecting to sql. 😉

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply