top of page

How to connect a JavaFX program to Oracle Database

Every database application requires an interface so that the user can interact with the database information. Better, if it is a GUI interface where we do not have to stoop down to a low-level, intimidating command interface but get what we want with a click of a button. In this aspect, JavaFX with JDBC can be a killer combination because it has quite a number of visually exciting GUI components that can be used to represent database records in a more meaningful manner.

A JDBC application with JavaFX essentially means that the JavaFX GUI framework was used as the front-end development engine and JDBC was used for the back-end database interaction.

We will use Oracle XE database and its default HR schema. In order to connect Oracle DB, we will use JDBC driver. JDBC driver is a software component enabling a Java application to interact with a database. To add JDBC driver in our project.


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns:fx="">
	<!-- TODO Add Nodes -->


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="253.0" prefWidth="339.0" xmlns="" xmlns:fx="" fx:controller="application.LoginController">
      <PasswordField fx:id="password" layoutX="136.0" layoutY="106.0" promptText="password" />
      <Button layoutX="-39.0" layoutY="-123.0" mnemonicParsing="false" text="Button" />
      <Button layoutX="136.0" layoutY="177.0" mnemonicParsing="false" onAction="#Login" prefHeight="25.0" prefWidth="73.0" text="login" />
      <Label layoutX="115.0" layoutY="14.0" prefHeight="32.0" prefWidth="73.0" text="Login Form" textFill="#fc0000">
            <Font size="14.0" />
      <Label layoutX="45.0" layoutY="64.0" prefHeight="32.0" prefWidth="61.0" text="Name" />
      <Label layoutX="45.0" layoutY="110.0" prefHeight="17.0" prefWidth="61.0" text="Password" />
      <TextField fx:id="userName" layoutX="136.0" layoutY="68.0" promptText="userName" />
      <Label fx:id="lebel" layoutX="71.0" layoutY="138.0" prefHeight="32.0" prefWidth="197.0" textFill="#27e532" />

package application;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class LoginController  {

	private TextField userName;
	private TextField password;
	private Label lebel;
	PreparedStatement pstmt=null;
	Connection con=null;
	ResultSet rs=null;
	public void Login(ActionEvent event){
			try {
				String sql = "select * from login where username=? and password=?";
				pstmt = con.prepareStatement(sql);
				pstmt.setString(2, password.getText());
				rs = pstmt.executeQuery();
				if( {
					lebel.setText("Login Sucessfully!");
					lebel.setText("Login Not Sucessfully!");
			} catch (SQLException e) {


package application;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;
public final class DBUtil {
		private static boolean isDriverLoaded = false;
				System.out.println("Driver Loaded");
				isDriverLoaded = true;	
			}catch(ClassNotFoundException e){
		private final static String url="jdbc:oracle:thin:@localhost:1521:XE";
		private final static String user="SYSTEM";
		private final static String password="system";
		public static Connection getConnection() throws SQLException{
			Connection con = null;
				con  = DriverManager.getConnection(url,user,password);
				System.out.println("Connection established");
			return con;
		public static void closeConnection(Connection con) throws SQLException{
				System.out.println("connection closed");

DBUtil Class

I want to start to explain the code to DBUtil class. In our example, DBUtil class is responsible for DB connection, DB disconnection, database query operations.

getConnection() method connects to DB.

closeConnection() method closes DB connection.
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
	public void start(Stage primaryStage) {
		try {
			Parent root =FXMLLoader.load(getClass().getResource("/application/login.fxml"));
			Scene scene = new Scene(root,400,400);
		} catch(Exception e) {
	public static void main(String[] args) {

310 views0 comments

Recent Posts

See All
bottom of page