Connect to a SQL database in Eclipse

Home > Search
  by

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 https://dev.mysql.com/downloads/connector/j/. 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.

 

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

In this example, com.mysql.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.jdbc.Driver in this example because the mysql.jar has com/mysql/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.servlet;

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://example.com:3306/your_database_name"; 
    private Connection conn;

	public Connection connect() {
		try {
		  Class.forName("com.mysql.jdbc.Driver");
		  conn = DriverManager.getConnection(jdbcURL,sqlUsername",sqlPassword);
		  return conn;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			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"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
  DbConnect db = new DbConnect();
  Connection conn = db.connect();
  if(conn == null){
    out.print("Database Connection Failed");
  }
  else {
    out.print("Database Connection Successful");
  }
%>
</body>
</html>

 

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.




Comments