ADVERTISEMENT
How to connect any java program to a database

How to Connect any Java Program to a Database




You probably have heard about the database, and probably wondered how you would connect to it using java. This article initiates Java Database Tutorial Series and it demonstrates how to connect any java program to a database. Our example is a console application.

To connect any java application to the database you need the JDBC driver, you can get the latest version of JDBC Driver from bitbucket.org from their download page JDBC driver is a jar file that enables java programs to interact with a given database.

Our example will demonstrate how to connect java to the database using the SQLite database. To connect java application to the SQLite database, you will be required to add the JDBC driver jar file to your IDE. I have covered 3 tutorials demonstrating how to import jar files to Eclipse IDE, NetBeans IDE, and IntelliJ IDE, I won’t repeat those procedures, for now, kindly click on respective links to explore.

We also need to create the database that we will be connecting to. To manage SQLite databases in windows I use a software called SQLite Browser,  it’s a cool software and completely free. SQLite Browser will enable you to create databases, tables, and many more options. For more info about SQLite Browser, see this article. Throughout this Java Database tutorial series, we will be using SQLite Browser. I also wrote another detailed article on Everything you need to know about SQLiteBrowswer, where I did a full tutorial, starting from creating the database, tables, and tones of things

Open the SQLite Browser and Create a new Database file from File > New Database or by simply pressing Ctrl+N (for windows users). Save your database in the same directory as src directory in your project. I won’t detail where you should save your database, because I have written an in-depth article on where you should save databases for any Java Project using Any IDE.



Please give your database any name, I called mine JavaDatabaseTutorials.db. Please note SQLite Browser creates databases with .db extension, these databases are compatible with SQLite as illustrated in this article so no need to force the .sqlite extension. By the way, if you force .sqlite extension, you won’t be able to open that database with SQLite Browser and therefore you won’t be able to create tables.

If you are not sure where to save your database in your java project, kindly see a tutorial on where to save databases for any java Project in the Project files.

How to Connect Any Java Program to SQLite Database

After importing JDBC driver to your IDE and creating a database, go ahead and create a new Java Class. I called mine DbConnection.java. Feel free to give your class any name.

Add the following code to your DbConnection.java file

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

public class DbConnection {
  public static Connection connect() {
    Connection con = null; 
    try {
      Class.forName("org.sqlite.JDBC");
      con = DriverManager.getConnection("jdbc:sqlite:javaDatabaseTutorials.db"); // connecting to our database
      System.out.println("Connected!");
    } catch (ClassNotFoundException | SQLException e ) {
      // TODO Auto-generated catch block
      System.out.println(e+"");
    }
    
    return con; 
  }
}

 

Next, create the main class that will execute our program. Call it TheMain.java or anything; this class will contain java’s main method. Now call connect method of DbConnection.java as shown below.

public class TheMain {
  public static void main(String [] args) {
    DbConnection.connect();
  }
}

 

Run your application from your IDE, if you don’t see “Connected!” message from your console, something is wrong with your database connection. If you see “NoClassFoundException”, that means JDBC driver was not properly imported.