Creating database connections

Make sure that the JDBC driver for the database you want to connect to is downloaded and is in the classpath. In our project, we have already ensured this by adding a dependency in Maven. Maven downloads the driver and adds it to the class path of our web application.

It is always a good practice to make sure that the JDBC driver class is available when the application is running. If it is not, we can set a suitable error message and not perform any JDBC operations. The name of the MySQL JDBC driver class is com.mysql.cj.jdbc.Driver:

try { 
  Class.forName("com.mysql.cj.jdbc.Driver"); 
} 
catch (ClassNotFoundException e) { 
  //log excetion 
  //either throw application specific exception or return 
  return; 
} 
 

Then, get ...

Get Java EE 8 Development with Eclipse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.