A voir également:
- Récupérés les informations dans une Bdd Java(JDBC)
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Entrer les informations d'identification reseau - Guide
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
1 réponse
Bonjour
import java.sql.*;
public class DisplayDataInTable {
public static void main(String[] args) {
// Etape 1: Charger le pilote JDBC
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
// Etape 2: Établir une connexion
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:mysql://localhost/maBaseDeDonnees", "monNomUtilisateur", "monMotDePasse");
} catch (SQLException e) {
e.printStackTrace();
return;
}
// Etape 3: Créer un objet Statement
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
return;
}
// Etape 4: Exécuter une requête SELECT
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery("SELECT id, nom, age, adresse FROM inscription");
} catch (SQLException e) {
e.printStackTrace();
return;
}
// Etape 5: Parcourir le ResultSet et afficher les valeurs
System.out.println("ID\tNom\tAge\tAdresse");
try {
while (resultSet.next()) {
int id = resultSet.getInt("id");
String nom = resultSet.getString("nom");
int age = resultSet.getInt("age");
String adresse = resultSet.getString("adresse");
System.out.println(id + "\t" + nom + "\t" + age + "\t" + adresse);
}
} catch (SQLException e) {
e.printStackTrace();
}
// Etape 6: Fermer la connexion
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Essaye ça