0

Let me preface this with I'm a seasoned developer, but new to Java. I've done the standard google/so lookup but i can't seem to fix my problem.

I'm toying around with on button click connecting to mssql 2008 server using netbeans on a mac.

I've gone to the services tab, right clicked Drivers and added a new one. I downloaded https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 and added the jar file.

My code is the follwing:

import java.sql.*;
private void btnCheckSQLActionPerformed(java.awt.event.ActionEvent evt) {                                            
         String url = "jdbc:sqlserver://sqlserver01.****.com:1433;databaseName=****;user=****;password=******";
    Connection con = null;
    Statement stmt = null;

    ResultSet rs = null;
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        con = DriverManager.getConnection(url);
        String sql = "Select UserName from Users";
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    }  

I get the error:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

When I remove the line: Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); I get the error:

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://sqlserver01.*****.com:1433;databaseName=****;user=****;password=******

I also tried:

import com.microsoft.sqlserver.jdbc.SQLServerDriver;

But that errors saying package does not exist.

MissCoder87
  • 2,669
  • 10
  • 47
  • 82
  • How are you executing the java command? If you doing the command line, add -cp . If you using something like eclipse, you need to add it into your environment (add external jars) – Mike May 04 '15 at 14:11
  • Add the 'JAR' to your project classpath in netbeans. Refer http://stackoverflow.com/questions/7598623/how-to-setup-classpath-in-netbeans – K139 May 04 '15 at 14:12
  • The Jar is not on your class path – user489041 May 04 '15 at 14:19

0 Answers0