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.