Chargement du ComboBox à partir de la base

1.      Chargement du ComboBox à partir de la base (txtTypeclient.addItem)
2.      ComboBox2 chargement avec tableau (toArray-DefaultComboBoxModel)
3.      ComboBox3 chargement avec tableau simplifiée en fonction

1. Chargement du ComboBox à partir de la base (txtTypeclient.addItem)

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboBox extends JFrame {
	private JComboBox<String> txtTypeclient = new JComboBox<String>();
	public ComboBox(){
		this.setTitle("Ma première fenêtre Java");
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             
		this.setVisible(true);
		//Instanciation d'un objet JPanel
		JPanel pan = new JPanel();
		//Définition de sa couleur de fond
		pan.setBackground(Color.ORANGE);
		
		System.out.println("1. Chargement du pilote JDBC");
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		}
		catch(ClassNotFoundException exp) {
			System.out.println("Erreur de chargement du pilote JDBC");
		}
		
		System.out.println("2. Connexion a la base");
		String url ="jdbc:mysql://"+"localhost"+"/"+"test2";
		Connection maConnection = null;
		try {
			maConnection = DriverManager.getConnection(url, "root", "");
		}
		catch(SQLException exp) {
			System.out.println("Erreur de connexion a :"+ url);
		}
		
		System.out.println("3. Execution requete");
		String requete = "select * from personne;";
		try {
			Statement unStat = maConnection.createStatement();
			ResultSet lesResultats =  unStat.executeQuery(requete);
			while(lesResultats.next()) {
				//chargement de la base ComboBox
				txtTypeclient.addItem(lesResultats.getString("nom")+" "+lesResultats.getString("prenom")+" "+lesResultats.getInt("age"));
			}
			unStat.close();
		}
		catch(SQLException exp){
			System.out.println("Erreur d'execution de la requete :"+ requete);
		}

		
		System.out.println("5. Fermeture de la base");
		try {
			if(maConnection != null) {
				maConnection.close();
			}
		}
		catch(SQLException exp) {
			System.out.println("Erreur de fermeture de la connexion.");
		}
		
		pan.add(this.txtTypeclient);      
		//On prévient notre JFrame que notre JPanel sera son content pane
		this.setContentPane(pan);               
		this.setVisible(true);
	}
	public static void main(String[] args) {
		ComboBox unComboBox = new ComboBox();
		//manipulation de chaîne
		int idpersonne = Integer.parseInt("15/Marie DENG".split("/")[0]);
		System.out.println(idpersonne);
	}

}

2. ComboBox2 chargement avec tableau (toArray-DefaultComboBoxModel)

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboBox2 extends JFrame {
	private JComboBox<String> txtTypeclient = new JComboBox<String>();
	public ComboBox2(){
		this.setTitle("Ma première fenêtre Java");
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             
		this.setVisible(true);
		//Instanciation d'un objet JPanel
		JPanel pan = new JPanel();
		//Définition de sa couleur de fond
		pan.setBackground(Color.ORANGE);
		
		System.out.println("1. Chargement du pilote JDBC");
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		}
		catch(ClassNotFoundException exp) {
			System.out.println("Erreur de chargement du pilote JDBC");
		}
		
		System.out.println("2. Connexion a la base");
		String url ="jdbc:mysql://"+"localhost"+"/"+"test2";
		Connection maConnection = null;
		try {
			maConnection = DriverManager.getConnection(url, "root", "");
		}
		catch(SQLException exp) {
			System.out.println("Erreur de connexion a :"+ url);
		}
		
		System.out.println("3. Execution requete");
		String requete = "select * from personne;";
		ArrayList<String> tableau = new ArrayList<>();
		try {
			Statement unStat = maConnection.createStatement();
			ResultSet lesResultats =  unStat.executeQuery(requete);
			while(lesResultats.next()) {
				tableau.add(lesResultats.getString("nom")+" "+lesResultats.getString("prenom")+" "+lesResultats.getInt("age"));
			}
			unStat.close();
		}
		catch(SQLException exp){
			System.out.println("Erreur d'execution de la requete :"+ requete);
		}

		String[] stringArray = tableau.toArray(new String[tableau.size()]);
		txtTypeclient.setModel(new DefaultComboBoxModel(stringArray));
		
		System.out.println("5. Fermeture de la base");
		try {
			if(maConnection != null) {
				maConnection.close();
			}
		}
		catch(SQLException exp) {
			System.out.println("Erreur de fermeture de la connexion.");
		}
		
		pan.add(this.txtTypeclient);      
		//On prévient notre JFrame que notre JPanel sera son content pane
		this.setContentPane(pan);               
		this.setVisible(true);
	}
	public static void main(String[] args) {
		ComboBox2 unComboBox = new ComboBox2();
		//manipulation de chaîne
		int idpersonne = Integer.parseInt("15/Marie DENG".split("/")[0]);
		System.out.println(idpersonne);
	}

}

3. ComboBox3 chargement avec tableau simplifiée en fonction

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboBox3 extends JFrame {
	private JComboBox<String> txtTypeclient = new JComboBox<String>();
	public ComboBox3(){
		this.setTitle("Ma première fenêtre Java");
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             
		this.setVisible(true);
		//Instanciation d'un objet JPanel
		JPanel pan = new JPanel();
		//Définition de sa couleur de fond
		pan.setBackground(Color.ORANGE);

		txtTypeclient.setModel(new DefaultComboBoxModel(tableau()));
		
		pan.add(this.txtTypeclient);      
		//On prévient notre JFrame que notre JPanel sera son content pane
		this.setContentPane(pan);               
		this.setVisible(true);
	}
	public String[] tableau(){
		
		System.out.println("1. Chargement du pilote JDBC");
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		}
		catch(ClassNotFoundException exp) {
			System.out.println("Erreur de chargement du pilote JDBC");
		}
		
		System.out.println("2. Connexion a la base");
		String url ="jdbc:mysql://"+"localhost"+"/"+"test2";
		Connection maConnection = null;
		try {
			maConnection = DriverManager.getConnection(url, "root", "");
		}
		catch(SQLException exp) {
			System.out.println("Erreur de connexion a :"+ url);
		}
		
		System.out.println("3. Execution requete");
		String requete = "select * from personne;";
		
		ArrayList<String> tableau = new ArrayList<>();
		try {
			Statement unStat = maConnection.createStatement();
			ResultSet lesResultats =  unStat.executeQuery(requete);
			while(lesResultats.next()) {
				tableau.add(lesResultats.getString("nom")+" "+lesResultats.getString("prenom")+" "+lesResultats.getInt("age"));
			}
			unStat.close();
		}
		catch(SQLException exp){
			System.out.println("Erreur d'execution de la requete :"+ requete);
		}
		System.out.println("5. Fermeture de la base");
		try {
			if(maConnection != null) {
				maConnection.close();
			}
		}
		catch(SQLException exp) {
			System.out.println("Erreur de fermeture de la connexion.");
		}

		String[] stringArray = tableau.toArray(new String[tableau.size()]);
		
		return stringArray;
	}
	public static void main(String[] args) {
		ComboBox3 unComboBox = new ComboBox3();
		//manipulation de chaîne
		int idpersonne = Integer.parseInt("15/Marie DENG".split("/")[0]);
		System.out.println(idpersonne);
	}

}

ComboBox simplifié

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboBox extends JFrame {
	private JComboBox<String> jcbUser = new JComboBox<String>();
	public ComboBox(){
		this.setTitle("Fenêtre Java");
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             
		this.setVisible(true);
		//Instanciation d'un objet JPanel
		JPanel pan = new JPanel();
		//Définition de sa couleur de fond
		pan.setBackground(Color.ORANGE);
		pan.add(this.jcbUser);
		
		// chargement des données
		jcbUser.setModel(new DefaultComboBoxModel(tableau()));
		
		//On prévient notre JFrame que notre JPanel sera son content pane
		this.setContentPane(pan);               
		this.setVisible(true);
	}
	// Création du JComboBox de tableau MYSQL

	public String[] tableau() {
		ArrayList<String> tableau = new ArrayList<>();
		try {
			Connection maConnection = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
			Statement unStat = maConnection.createStatement();
			ResultSet lesResultats = unStat.executeQuery("select * from user");
			while (lesResultats.next()) {
				tableau.add(lesResultats.getString("iduser") + " " + lesResultats.getString("nom") + " " + lesResultats.getString("prenom"));
			}

			unStat.close();
			if (maConnection != null) {
				maConnection.close();
			}
		} catch (Exception exp) {
			System.out.println(exp.getMessage());
		}

		String[] stringArray = tableau.toArray(new String[tableau.size()]);

		return stringArray;
	}
	public static void main(String[] arg) {
		System.out.println("Hello");
		ComboBox unComboBox = new ComboBox();
	}
}

Grille Java

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Grille extends JFrame {
	public Grille() {
		this.setTitle("Fenêtre Java");
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             
		this.setVisible(true);
		//Instanciation d'un objet JPanel
		JPanel pan = new JPanel();
		//Définition de sa couleur de fond
		pan.setBackground(Color.ORANGE);
		
		
		DefaultTableModel model = new DefaultTableModel();
		try {
			Connection maConnection = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
			Statement unStat = maConnection.createStatement();
			ResultSet lesResultats = unStat.executeQuery("select * from user");
			// Récupération des métadonnées pour les noms de colonnes
			ResultSetMetaData metaData = lesResultats.getMetaData();
			int columnCount = metaData.getColumnCount();
			String[] columnNames = new String[columnCount];
			for (int i = 1; i <= columnCount; i++) {
			    columnNames[i - 1] = metaData.getColumnName(i);
			}
			// Remplissage du modèle de tableau avec les données
			model.setColumnIdentifiers(columnNames);
			// Remplissage du modèle de tableau avec les données de la base de données
			while (lesResultats.next()) {
			    Object[] rowData = new Object[columnCount];
			    for (int i = 1; i <= columnCount; i++) {
			        rowData[i - 1] = lesResultats.getObject(i);
			    }
			    model.addRow(rowData);
			}
			unStat.close();
			if(maConnection != null) {
				maConnection.close();
			}
		}
		catch(Exception exp) {
			System.out.println(exp.getMessage());
		}
		// Création du JTable avec le modèle de tableau
		JTable table = new JTable(model);
		// Création du JScrollPane avec le JTable
		JScrollPane scrollPane = new JScrollPane(table);
		pan.add(scrollPane);
		
		
		//On prévient notre JFrame que notre JPanel sera son content pane
		this.setContentPane(pan);               
		this.setVisible(true);
	}
	public static void main(String[] arg) {
		System.out.println("Hello");
		Grille uneGrille = new Grille();
	}
}