Reputation: 365
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.
I've update my CLASSPATH and still getting the same error.
Upvotes: 0
Views: 1154
Reputation:
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
Upvotes: 1
Reputation: 130
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.
Upvotes: 2