Skip to content

JDBC and Connectivity

trekitch edited this page Apr 24, 2017 · 6 revisions

A database is a structured set of data help within a computer. Databases are usually made of multiple tables that are composed of rows and columns of different data. Databases can be very helpful when organizing data. Each row in a database usually has a primary key that each row is organized by. This primary key helps to identify that each row is unique. This becomes especially helpful if you have a database of items and their prices, or teachers and their classes. JDBC stands for Java Database Connectivity and it is how you can create, access and manage databases in Java. In Java development, it is important to understand how to properly use databases to handle data such as customer information or a database of products.

Iteam No (PK) Deecription (FK) Price Qty
1 Toy Car 2.30 23
32 Dog Food 4.00 45

Above is an example of what a table in a database may look like. As you can see each item is order in rows and each column is a different attribute of the item. Also notice the PK and FK next to some of the column headings. This denotes that the field is either a primary key or foreign key. A primary key is a unique identifier of items in a table. In this case it is Item No because each item will have its own unique item number. A foreign key is a primary key from another table that is used to connect the two tables.

Connecting to a Database

In order to access database application is Java you first need to import the proper packages to use the JDBC drivers.

import java.sql.*;

This import statement will allow you to create, select, update, etc databases in Java. Now that we have the proper packages imported we will need to establish a connect to the server that we are using. To do this we can use the DriverManager.getConnect() method. This method establishes a connection to the server. For this example, our server address will be 127.0.0.1.

DriverManager.getConnection(jdbc:postgresql://127.0.0.1)

The statement that is inside of the parathesis of the DriverManager method is called the URL. This is how the application will know where to go to access your database. If your database is looked by a username and password then you can also put these as parameters.

DriverManager.getConnection(jdbc:postgresql://127.0.0.1, username, password)

replace username and password with the username and password for you server.

It is also a good idea to declare your URL, username, and password as variables for easy use.

Testing your connection

Now that we are connected it is a good idea to make sure that we are connected to the server. To do this we can simply put the DriverManager method inside of a try catch statement that outputs a statement that tells you if you are properly connected or not.

Connection conn = null;

try 
{
   conn = DriverManager.getConnection(url, user, password);
   System.out.println("Connected to the PostgreSQL server successfully.");
} 
catch (SQLException e) 
{
    System.out.println(e.getMessage());
}

Output

Connected to the PostgreSQL server successfully.
Clone this wiki locally