0

I'm getting an error while trying to connect my java application to a SQL Server database, here's my code.

 try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection(CONN,USER,PASS);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from articulos");
        while(rs.next()) {
            int id = rs.getInt(1);
            String descripcion = rs.getString(2);
            float precio = rs.getFloat(3);
            int rubro = rs.getInt(4);
            
            Articulo a = new Articulo(id,descripcion,precio);
            lista.add(a);
        }
        
    } catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(GestorBD.class.getName()).log(Level.SEVERE, null, ex);
    }

and i keep having this error

SEVERE: null
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:333)
at gestor.GestorBD.obtenerArticulos(GestorBD.java:19)
at repasobd.RepasoBD.main(RepasoBD.java:14)

This shows my driver connected.

Driver Connected

I've update my CLASSPATH and still getting the same error.

classpath

2 Answers 2

2

You need to have com.microsoft.sqlserver.jdbc.SQLServerDriver class in your classpath which can be found in Microsoft JDBC Driver. So the issue isn't in the connection. It's that you are missing a driver lib.

Sign up to request clarification or add additional context in comments.

Comments

1

Do not use the system wide CLASSPATH variable - it has been deprecated for ages.

Your NetBeans screeshot show the drivers available for the built-in NetBeans database query tool. This does not change the classpath of your project.

If you are using an Ant based project, right click on the project, choose properties, then add the jar file of the driver in the "Libraries" section.

If it's a Maven project, you need to add the dependency to the Microsoft JDBC driver, through Maven

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.