JavaFx tuto- JDBC & Singleton

Amin
mabttech
4 min readJan 26, 2021

--

JavaFx tuto / workshop : Using JDBC and Singleton design patterns for JavaFx Applicaitons

Using Netbeans and Java 8 we are going to create this project structure

NetBeans + Java 8 — also adding MySQL JDBC Driver jars

/Entite/Personne.java :

// .../Entite/Personne.java
package com.esprit.Entite;

/**
*
* @author MABT
*/
public class Personne {
private int id;
private String nom;
private String prenom;
private int age;

public Personne(int id, String nom, String prenom, int age) {
this.id = id;
this.nom = nom;
this.prenom = prenom;
this.age = age;
}

public Personne(String nom, String prenom, int age) {
this.nom = nom;
this.prenom = prenom;
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public String getPrenom() {
return prenom;
}

public void setPrenom(String prenom) {
this.prenom = prenom;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Personne{" + "id=" + id + ", nom=" + nom + ", prenom=" + prenom + ", age=" + age + '}';
}

}

/IService/IService.java :

//   .../IService/IService.java
package com.esprit.IService;

import java.sql.SQLException;
import java.util.List;

/**
* @author MABT
*/
public interface IService<T> {
void ajouter(T t) throws SQLException;
boolean delete(T t) throws SQLException;
boolean update(T t) throws SQLException;
List<T> readAll() throws SQLException;
}

/Service/ServicePersonne.java :

// .../Service/ServicePersonne.java
package com.esprit.Service;

import com.esprit.Entite.Personne;
import com.esprit.IService.IService;
import java.sql.SQLException;
import java.util.List;
import java.sql.*;
import com.esprit.Utils.DataBase;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author MABT
*/
public class ServicePersonne implements IService<Personne> {

private Connection con;
private Statement ste;

public ServicePersonne() {
con = DataBase.getInstance().getConnection();

}

@Override
public void ajouter(Personne t) throws SQLException {
ste = con.createStatement();
String requeteInsert = "INSERT INTO `esprit1`.`personne` (`id`, `nom`, `prenom`, `age`) VALUES (NULL, '" + t.getNom() + "', '" + t.getPrenom() + "', '" + t.getAge() + "');";
ste.executeUpdate(requeteInsert);
}
public void ajouter1(Personne p) throws SQLException
{
PreparedStatement pre=con.prepareStatement("INSERT INTO `esprit1`.`personne` ( `nom`, `prenom`, `age`) VALUES ( ?, ?, ?);");
pre.setString(1, p.getNom());
pre.setString(2, p.getPrenom());
pre.setInt(3, p.getAge());
pre.executeUpdate();
}


@Override
public boolean delete(Personne t) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean update(Personne t) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public List<Personne> readAll() throws SQLException {
List<Personne> arr=new ArrayList<>();
ste=con.createStatement();
ResultSet rs=ste.executeQuery("select * from personne");
while (rs.next()) {
int id=rs.getInt(1);
String nom=rs.getString("nom");
String prenom=rs.getString(3);
int age=rs.getInt("age");
Personne p=new Personne(id, nom, prenom, age);
arr.add(p);
}
return arr;
}
}

/Utils/DataBase.java :

//   .../Utils/DataBase.java

package com.esprit.Utils;

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

/**
* @author MABT
*/

public class DataBase {
String url = "jdbc:mysql://localhost:3306/esprit1";
String login = "root";
String pwd = "";
public static DataBase db;
public Connection con;
private DataBase() {
try {
con=DriverManager.getConnection(url, login, pwd);
System.out.println("connexion etablie");
} catch (SQLException ex) {
System.out.println(ex);
}
}

public Connection getConnection()
{
return con;
}
public static DataBase getInstance()
{if(db==null)
db=new DataBase();
return db;
}
}

/test/Test.java :

// ... /test/Test.java

package com.esprit.test;

import com.esprit.Entite.Personne;
import com.esprit.Service.ServicePersonne;
import com.esprit.Utils.DataBase;
import java.sql.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author MABT
*/
public class Test {

public static void main(String[] args) {
ServicePersonne ser = new ServicePersonne();

Personne p1 = new Personne("hammadi", "jbeli", 10);
Personne p2 = new Personne("Bajbouj", "Sebsi", 10);

try {
ser.ajouter1(p2);
ser.ajouter(p1);
List<Personne> list = ser.readAll();
System.out.println(list);
} catch (SQLException ex) { System.out.println(ex); }
}

}

Execute the “ Main.java “ class and you will get the following out put in the console showing your DataBase.

Additional info :

You can use NetBeans or IntelliJ IDEA for your project .

( Java 8 ) , and for the DataBase you can use MySQL workingBench or ( in my case ) XAMP / WAMP Server MySQL for the rest of the workshop.

Bibliography :

+CRUD : Search By ID / Search By NAME

--

--