DriverManager vs. DataSource
At a high level, there are two ways to connect to a SQL database in Java.
- DriverManager (this article)
- DataSource
When using DriverManager, you place the database driver JAR (such as mysql.jar or postgresql.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 / Add to Build Path
Download the latest database connector such as mysql-connector-j-<version>.jar from https://mvnrepository.com or postgresql-<version>.jar from https://jdbc.postgresql.org/download and add the JARs to your Java Build Path.
Create the class
- In the left panel of Eclipse, expand your project.
- Right-click on the src folder and select New > Class.
- The class can be named anything you want, such as DbConnect, and select Finish.
- Update the markup to have the following.
In this example, com.mysql.cj.jdbc.Driver is used. This value will 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://example.com:3306/your_database_name";
private Connection conn;
public Connection connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(jdbcURL,sqlUsername,sqlPassword);
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
And here is a similar example that uses Postgres.
package postgres;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnect {
private String jdbcURL = "jdbc:postgresql://172.0.0.2:5432/db1?user=john.doe&password=itsasecret";
private Connection conn;
public Connection connect() {
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(jdbcURL);
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.
Did you find this article helpful?
If so, consider buying me a coffee over at