From the Java documentation
In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName.
Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)
I have a jersey Webservice which connects to SQL Server Express 2016. It has sqljdbc42.jar which is 4.2 driver, in the CLASSPATH
However, if I omit the Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
call, my DriverManager.getConnection
throws a SQLException
(No suitable driver found for jdbc:sqlserver://localhost:1433; ....")
The getConnection
starts succeeding once I add the Class.forName
call.
I am on Java 8.
What am I missing?
UPDATE: I just tried a command line program and it works without the forName
. However, from my Eclipse IDE where I am running my REST service as a Tomcat 8.0 Server on localhost, it doesn't work.