Connect to a SQL database in Eclipse

Home > Search

Add JAR to app servers

The JAR is what is used to be able to establish a connection to a database. Typically, you will be working the same JAR on two different servers. One JAR will be in the lib folder of the server runtime environment used by Eclipse. The other JAR will be in the lib folder of the production application server, such as Tomcat or WebSphere. The JAR on the two separate systems will be the same. For example, if connecting to a MySQL database, the same MySQL JAR would be located on the server runtime environment and production application server.

Ensure both your server runtime environment and production application server have the JAR. For example, if using a MySQL database, you would download the MySQL connector JAR from, and then place the JAR in the lib folder of both the server runtime environment and production application server. If using WebSphere, setup the JDBC, data source, and J2C alias.


Add JAR to Eclipse

This is only necessary for Eclipse to be able to make a database connection to the server runtime environment (testing environment).

  1. Right-click on your project and select Properties.
  2. Select Java Build Paths.
  3. Select the Libraries tab.
  4. Select Add External JARs.
  5. Select the JAR and select Apply.
  6. Select Deployment Assembly.
  7. Select Add, then select Java Build Path Entries and click Next.
  8. Select the JAR and select Finish.

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, select as DbConnect. Check public static void main(String[] args) and select Finish.
  4. Update the markup to have the following.
package com.main.servlet;

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

public class DbConnect {

	public Connection getConnection() {
		try {
		  Connection conn = DriverManager.getConnection("jdbc:mysql://","username","password");
		  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.getConnection();
  if(conn == null){
    out.print("Database Connection Failed");
  else {
    out.print("Database Connection Successful");


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 in the box below so that we can be sure you are a human.