FreeKB - Eclipse Connect to a SQL database via DriverManager
Eclipse - Connect to a SQL database via DriverManager

DriverManager vs. DataSource

At a high level, there are two ways to connect to a SQL database in Java.

  • DriverManager
  • DataSource

When using DriverManager, you place the database driver JAR (such as mysql.jar) on the system running Eclipse and then you configure Eclipse to use the JAR. When using DataSource, you place the database driver JAR on the system running the application server (JBoss, Tomcat, WebSphere). 

DataSource has numerous advantages over DriverManager. With DataSource you only need to place the JAR on one system (the application server). Another advantage is that you can leverage additional capabilities, such as connection pooling. This article describes how to connect to a SQL database via DriverManager. Refer to this article to connect to a database via DataSource.


Download JAR

The database JAR (Java Archive) is what is used to be able to establish a connection to a database. For example, if using a MySQL database, you would use the mysql-connector-java-<version>.jar to establish a connection to MySQL. The MySQL JAR is downloaded from Based on the type of database you are connecting to, download the appropriate JAR.


Place the JAR on the same system as Eclipse

You will place the JAR somewhere on the same system as Eclipse, such as C:\Program Files (x86)\jdbc (Windows) or /opt/jdbc (Mac / Linux).


Add the JAR to Java Build Path in Eclipse

Next, add the JAR to your Java Build Path, which will allow you to run your project on a server runtime environment (Tomcat, WebSphere) in Eclipse and make a connection to the database. This is also known as adding the jar to the classpath.


Create the class

  1. In the left panel of Eclipse, expand your project.
  2. Right-click on the src folder and select New > Class.
  3. The class can be named anything you want, such as DbConnect, and select Finish.
  4. Update the markup to have the following.

In this example, com.mysql.cj.jdbc.Driver is used. This value would certainly be different if you are using a database other than MySQL, and even if you are using MySQL, this value may still be different based on the JAR you are using. The driver is obtained from the JAR file being used to connect to the database. Viewing the content of the JAR file can be used to determine the appropriate driver. The content of the JAR file will simply be a collection of files and directories. As an example, we are using com.mysql.cj.jdbc.Driver in this example because the mysql.jar has com/mysql/cj/jdbc/Driver.class.

Note - Instead of hard coding the username and password in as code in your Java app, you may want to consider obtaining the username and password from a properties file.

package com.main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnect {
    private String sqlUsername = "your_username";
    private String sqlPassword = "your_password";
    private String jdbcURL = "jdbc:mysql://"; 
    private Connection conn;

	public Connection connect() {
		try {
		  conn = DriverManager.getConnection(jdbcURL,sqlUsername,sqlPassword);
		  return conn;
		} catch (ClassNotFoundException e) {
			return null;
		} catch (SQLException e) {
			return null;


Use the class in an HTML or JSP page

<%@page import="your_package_name.your_class_name"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
  DbConnect db = new DbConnect();
  Connection conn = db.connect();
  if(conn == null){
    out.print("Database Connection Failed");
  else {
    out.print("Database Connection Successful");


J2C alias

You will need to decide if you want to bind the WebSphere data source to a J2C alias or if you want to code your app(s) to get the J2C alias from WebSphere.


You can now run your project in eclipse, or export your project and run it on an application server. If running the project in Eclipse, if the connection to the database is successful, the following should be displayed. 


In this example, the WAR was deployed to WebSphere, and the connection was successful.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 7f65f in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |