5

This is my first Java application and I'm completely inexperienced with Java and NetBeans.

I have been trying to connect to sql and get some records for 2 days. The problem is about jdbc driver, let me explain. I have downloaded sqljdbc driver and then followed these steps:

Right-click Project->Select Properties->On the left-hand side click Libraries->Under Compile tab - click Add Jar/Folder button and select sqljdbc4.jar file. Then it should be ok, right?

Then I wrote this code But I cant get rid of this exception:

  Exception in thread "main" java.lang.ClassNotFoundException: 
  com.microsoft.sqlserver.jdbc.SqlServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:30)

This is the code

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    String url = "jdbc:sqlserver://.\\SQLEXPRESS;databaseName=Northwind; Integrated Security = SSPI ";

    Connection con = null;
    Statement stmt = null;

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

        con = DriverManager.getConnection(url);
        String sql = "Select Top 3 from * person.Contact";
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
user2708377
  • 55
  • 1
  • 1
  • 4

3 Answers3

11

According to this page, the class is called SQLServerDriver and not SqlServerDriver. Case is important!

So, try:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Note that with newer versions of JDBC, it's not necessary to load the driver class explicitly with Class.forName(...). The page I linked to explicitly explains that you don't have to do it. So, you can just remove the whole line and then it should work.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • I have been working on this problem for days started mybe ten thread in diffrent forums and formatted my machine 3 times what can i say thank you soooo muchh Jesper – user2708377 Aug 24 '13 at 16:47
  • I have the same problem. but after removing this line, i have another error about, suitable driver not being found. do you know the reason? – ConductedClever Aug 13 '14 at 14:39
  • @ConductedClever Why did you remove the line if that makes your application not work? A "no suitable driver found" error can also mean that your JDBC URL is not correct. (The JDBC driver JAR must also be in your classpath when you run the application). – Jesper Aug 14 '14 at 06:37
  • @Jesper, I am still getting the exception, even though I had already taken care of case sensitivity while typing the class name. I am posting my code in the next comment: – Vikram Jun 11 '15 at 21:07
  • import java.sql.*; public class DatabaseConnectivity { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection m_Connection = DriverManager.getConnection( "jdbc:sqlserver://ip_address:1433;DatabaseName=DB_Name", "username", "password"); System.out.println("Connection successful!!!"); Statement m_Statement = m_Connection.createStatement(); String query = "SELECT * FROM CI_Item"; } } – Vikram Jun 11 '15 at 21:09
  • @Vikram Make sure that the JDBC driver jar file is in your classpath when you run your application. – Jesper Jun 12 '15 at 06:36
  • Class.forName() did it for me in that after using it, I got an error message telling me that the SQLServer JDBC jar I was using was too new for Java 8, which we're still running on some of our servers. I switched to an older jar, and my application began to work properly. – Marvo Nov 06 '22 at 23:45
1

Java: JDBC Connectivity with MSSQL in NetBeans

Steps

  1. Download JDBC from: https://www.microsoft.com/en-in/download/details.aspx?id=11774
  2. Run sqljdbc__enu.exe - unpack this zip file in %Program Files (x86)% with the default directory: Microsoft JDBC DRIVER for SQL Server
  3. Create your new project in NetBeans
  4. Right Click on the project - select Properties - select Libraries from left panel - click Add JAR/Folder button - select your JAR file and open - ok
  5. Open Sql Server Configuration Manager - select Protocols for SQLEXPRESS under Sql Server Network Configuration - Right Click on TCP/IP - select Properties - change Enable to Yes - Click IP Addresses - Goto IPAll - Change TCP Dynamic Ports to 49169 and TCP Port to 1433 - Apply - Ok - Restart the Computer
  6. Open Run and type Services.msc - Start SQL Server Browser
  7. Goto project and write code for database connectivity.

Code for Local Database Connectivity:

String url = "jdbc:sqlserver://YOUR PC NAME;instanceName=SQLEXPRESS;DatabaseName=dbname;integratedSecurity=true";

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myCon = DriverManager.getConnection(url);

Statement myStmt = myCon.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from table name");
0

I have an update about this issue.

  1. Go to this link, find your compatible JDBC driver (I dowloaded 6.0 version).
  2. Find the appropriate jar in the file you downloaded (I used jre7\sqljdbc41.jar).
  3. For Intellij Idea press Ctrl+Shift+Alt+S and open Project Structure then in the dependencies section add your jar file.

I hope it works for you too.

ismet
  • 39
  • 7