100 lines
2.6 KiB
Java
100 lines
2.6 KiB
Java
|
package fr.blankoworld.connexionBDD;
|
|||
|
|
|||
|
import java.sql.Connection;
|
|||
|
import java.sql.DriverManager;
|
|||
|
import java.sql.ResultSet;
|
|||
|
import java.sql.ResultSetMetaData;
|
|||
|
import java.sql.SQLException;
|
|||
|
import java.sql.Statement;
|
|||
|
|
|||
|
public class testConnexion {
|
|||
|
|
|||
|
public static void main(String args[]){
|
|||
|
|
|||
|
System.out.println("## D<>but");
|
|||
|
|
|||
|
try{
|
|||
|
Class.forName( "oracle.jdbc.driver.OracleDriver" );
|
|||
|
System.out.println("## Trouv<75>");
|
|||
|
}
|
|||
|
catch(ClassNotFoundException ex){
|
|||
|
System.out.println("## Pas trouv<75>");
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
Connection connection = null;
|
|||
|
String url = "jdbc:oracle:thin:@grive.u-strasbg.fr:1521:v920";
|
|||
|
String identifiant = "dut";
|
|||
|
String mdp = "dut";
|
|||
|
|
|||
|
try {
|
|||
|
|
|||
|
connection = DriverManager.getConnection(url, identifiant, mdp);
|
|||
|
System.out.println("## Acc<63>s <20> la base: Accept<70>.");
|
|||
|
|
|||
|
try {
|
|||
|
// Cr<43>ation d'une requ<71>te
|
|||
|
Statement requete = connection.createStatement();
|
|||
|
// R<>cup<75>ration du r<>sultat de la requ<71>te
|
|||
|
ResultSet resultat = requete.executeQuery("SELECT * FROM GAULOIS"); // executeQuery ne retourne jamais null !
|
|||
|
System.out.println("## Requ<71>te: Envoy<6F>e et re<72>ue.");
|
|||
|
|
|||
|
int nbreLignes = 0;
|
|||
|
while(resultat.next()){
|
|||
|
nbreLignes++;
|
|||
|
}
|
|||
|
System.out.println("Nbre: " + nbreLignes);
|
|||
|
|
|||
|
|
|||
|
// On catalogue l'ensemble des donn<6E>es relatives <20> la table r<>sultante (pas le contenu mes les donn<6E>es de colonnes, etc ..)
|
|||
|
ResultSetMetaData rsmd = resultat.getMetaData();
|
|||
|
|
|||
|
// Nombre de colonnes (Commence <20> 1 !)
|
|||
|
int i = rsmd.getColumnCount();
|
|||
|
// System.out.println("Nombre de colonne: " + i);
|
|||
|
|
|||
|
// Donner les ent<6E>tes de colonnes
|
|||
|
for(int j = 1; j < i; j++){
|
|||
|
System.out.print(rsmd.getColumnName(j) + " ");
|
|||
|
}
|
|||
|
System.out.println();
|
|||
|
|
|||
|
// Cr<43>ation d'une requ<71>te
|
|||
|
Statement requete2 = connection.createStatement();
|
|||
|
// R<>cup<75>ration du r<>sultat de la requ<71>te
|
|||
|
ResultSet jeu = requete2.executeQuery("SELECT * FROM GAULOIS"); // executeQuery ne retourne jamais null !
|
|||
|
System.out.println("## Requ<71>te: Envoy<6F>e et re<72>ue.");
|
|||
|
|
|||
|
while(jeu.next()){
|
|||
|
//traitement des autres lignes
|
|||
|
for(int j = 1; j < i; j++){
|
|||
|
System.out.print(jeu.getObject(j) + " ");
|
|||
|
if(j != i - 1){
|
|||
|
System.out.print("");
|
|||
|
}
|
|||
|
}
|
|||
|
System.out.println();
|
|||
|
}
|
|||
|
|
|||
|
} catch (Exception ex){
|
|||
|
System.out.println("## Requ<71>te: Non r<>ussie.");
|
|||
|
}
|
|||
|
} catch (SQLException sqle) {
|
|||
|
|
|||
|
System.out.println("## Acc<63>s <20> la base: Refus<75>.");
|
|||
|
// TODO: handle exception
|
|||
|
} finally {
|
|||
|
if(connection!=null){
|
|||
|
try{connection.close();
|
|||
|
|
|||
|
System.out.println("## Connexion ferm<72>e.");
|
|||
|
} catch(Exception e){
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
System.out.println("## Fin");
|
|||
|
}
|
|||
|
|
|||
|
}
|