Grosse MàJ
This commit is contained in:
BIN
P51/connexionBDD/.Connexion.java.swp
Normal file
BIN
P51/connexionBDD/.Connexion.java.swp
Normal file
Binary file not shown.
BIN
P51/connexionBDD/Connexion.class
Normal file
BIN
P51/connexionBDD/Connexion.class
Normal file
Binary file not shown.
160
P51/connexionBDD/Connexion.java
Normal file
160
P51/connexionBDD/Connexion.java
Normal file
@ -0,0 +1,160 @@
|
||||
package fr.blankoworld.connexionBDD;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @author 3dossmanno
|
||||
*/
|
||||
public class Connexion {
|
||||
|
||||
|
||||
// Donnees privees
|
||||
private String serveur;
|
||||
private String port;
|
||||
private String bdd;
|
||||
private String id;
|
||||
private String mdp;
|
||||
|
||||
private Connection connection;
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
// Constructeur
|
||||
public Connexion(String server, String port, String database, String login, String password)
|
||||
{
|
||||
this.serveur = server;
|
||||
this.port = port;
|
||||
this.bdd = database;
|
||||
this.id = login;
|
||||
this.mdp = password;
|
||||
}
|
||||
|
||||
// Ascesseurs, etc ...
|
||||
public String getServer(){
|
||||
return this.serveur;
|
||||
}
|
||||
|
||||
public void setServer(String chaine){
|
||||
this.serveur = chaine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the port
|
||||
* @uml.property name="port"
|
||||
*/
|
||||
public String getPort(){
|
||||
return this.port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param port the port to set
|
||||
* @uml.property name="port"
|
||||
*/
|
||||
public void setPort(String chaine){
|
||||
this.port = chaine;
|
||||
}
|
||||
|
||||
public String getDatabase(){
|
||||
return this.bdd;
|
||||
}
|
||||
|
||||
public void setDatabase(String chaine){
|
||||
this.bdd = chaine;
|
||||
}
|
||||
|
||||
public String getLogin(){
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setLogin(String chaine){
|
||||
this.id = chaine;
|
||||
}
|
||||
|
||||
public String getPassword(){
|
||||
return this.mdp;
|
||||
}
|
||||
|
||||
public void setPassword(String chaine){
|
||||
this.mdp = chaine;
|
||||
}
|
||||
|
||||
// Verification presence du pilote
|
||||
public String[] driverPresent(){
|
||||
String[] tableau = new String[2];
|
||||
|
||||
try{
|
||||
// On verifie que le pilote Oracle est disponible
|
||||
Class.forName( "oracle.jdbc.driver.OracleDriver" );
|
||||
tableau[0] = "0";
|
||||
tableau[1] = "Pilote Oracle trouve";
|
||||
}
|
||||
catch(ClassNotFoundException ex){
|
||||
tableau[0] = "1";
|
||||
tableau[1] = "Pilote pas trouve";
|
||||
}
|
||||
|
||||
return tableau;
|
||||
}
|
||||
|
||||
// Connexion a la base
|
||||
// Doit pouvoir retourner un tableau avec une chaine expliquant si oui ou non nous sommes connectes, et une autre donnant l'erreur
|
||||
// Ceci permet d'avoir des messages en francais ...
|
||||
public String[] connect(){
|
||||
String[] tableau = new String[2];
|
||||
|
||||
// S'il l'est, nous continuons en preparant notre creme
|
||||
connection = null;
|
||||
String url = "jdbc:oracle:thin:@" + this.serveur + ":" + this.port + ":" + this.bdd;
|
||||
String identifiant = this.id;
|
||||
String mdp = this.mdp.toString();
|
||||
|
||||
// puis nous tentons d'appliquer la creme
|
||||
try {
|
||||
connection = DriverManager.getConnection(url, identifiant, mdp);
|
||||
tableau[0] = "0";
|
||||
tableau[1] = "Acces a la base: Accepte.\n";
|
||||
|
||||
DatabaseMetaData metaData;
|
||||
|
||||
metaData = connection.getMetaData();
|
||||
System.out.println(metaData.getDriverName());
|
||||
System.out.println(metaData.getDriverVersion());// getVersion
|
||||
System.out.println(metaData.getConnection().toString());// getConnectionName ???
|
||||
|
||||
}
|
||||
catch(SQLException sqle) {
|
||||
tableau[0] = "1";
|
||||
tableau[1] = "Acces a la base: Refuse.\n" + sqle.getMessage();
|
||||
// TODO: handle exception
|
||||
}
|
||||
|
||||
return tableau;
|
||||
}
|
||||
|
||||
// Deconnexion a la base de donnees
|
||||
public String[] disconnect(){
|
||||
String[] tableau = new String[2];
|
||||
|
||||
if(connection != null){
|
||||
try{
|
||||
connection.close();
|
||||
tableau[0] = "0";
|
||||
tableau[1] = "Connexion ferm<72>e.";
|
||||
} catch(Exception e){
|
||||
tableau[0] = "1";
|
||||
tableau[1] = e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return tableau;
|
||||
}
|
||||
|
||||
}
|
140
P51/connexionBDD/Connexion.java~
Normal file
140
P51/connexionBDD/Connexion.java~
Normal file
@ -0,0 +1,140 @@
|
||||
package fr.blankoworld.connexionBDD;
|
||||
|
||||
public class Connexion {
|
||||
|
||||
|
||||
// Donnees privees
|
||||
private String serveur;
|
||||
private String port;
|
||||
private String bdd;
|
||||
private String id;
|
||||
private String mdp;
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
// Constructeur
|
||||
public Connexion(String server, String port, String database, String login, String password)
|
||||
{
|
||||
this.serveur = server;
|
||||
this.port = port;
|
||||
this.bdd = database;
|
||||
this.id = login;
|
||||
this.mdp = password;
|
||||
}
|
||||
|
||||
// Ascesseurs, etc ...
|
||||
public String getServer(){
|
||||
return this.serveur;
|
||||
}
|
||||
|
||||
public void setServer(String chaine){
|
||||
this.serveur = chaine;
|
||||
}
|
||||
|
||||
public String getPort(){
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(String chaine){
|
||||
this.port = chaine;
|
||||
}
|
||||
|
||||
public String getDatabase(){
|
||||
return this.bdd;
|
||||
}
|
||||
|
||||
public void setDatabase(String chaine){
|
||||
this.bdd = chaine;
|
||||
}
|
||||
|
||||
public String getLogin(){
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setLogin(String chaine){
|
||||
this.id = chaine;
|
||||
}
|
||||
|
||||
public String getPassword(){
|
||||
return this.mdp;
|
||||
}
|
||||
|
||||
public void setPassword(String chaine){
|
||||
this.mdp = chaine;
|
||||
}
|
||||
|
||||
// Verification presence du pilote
|
||||
public String[] driverPresent(){
|
||||
String[] tableau = new String[2];
|
||||
|
||||
try{
|
||||
// On verifie que le pilote Oracle est disponible
|
||||
Class.forName( "oracle.jdbc.driver.OracleDriver" );
|
||||
tableau[0] = "0";
|
||||
tableau[1] = "Pilote Oracle trouve";
|
||||
}
|
||||
catch(ClassNotFoundException ex){
|
||||
tableau[0] = "1";
|
||||
tableau[1] = "Pilote pas trouve";
|
||||
}
|
||||
|
||||
return tableau;
|
||||
}
|
||||
|
||||
|
||||
// Connexion a la base
|
||||
// Doit pouvoir retourner un tableau avec une chaine expliquant si oui ou non nous sommes connectes, et une autre donnant l'erreur
|
||||
public String[] connect(){
|
||||
String[] tableau = new String[2];
|
||||
tableau[0] = "0";
|
||||
tableau[1] = "Je suis sense me connecter !";
|
||||
|
||||
// S'il l'est, nous continuons en preparant notre creme
|
||||
Connection connection = null;
|
||||
String url = "jdbc:oracle:thin:@" + this.serveur + ":" + this.port + ":" + this.bdd;
|
||||
String identifiant = this.id;
|
||||
String mdp = this.mdp;
|
||||
|
||||
// puis nous tentons d'appliquer la creme
|
||||
try {
|
||||
|
||||
connection = DriverManager.getConnection(url, identifiant, mdp);
|
||||
tableau[1] = "Acces a la base: Accepte.";
|
||||
}
|
||||
catch(SQLException sqle) {
|
||||
tableau[1] = "Accès à la base: Refuse.";
|
||||
// TODO: handle exception
|
||||
} finally {
|
||||
if(connection!=null){
|
||||
try{connection.close();
|
||||
|
||||
tableau[1] = "Connexion fermee.";
|
||||
} catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
return tableau;
|
||||
}
|
||||
|
||||
// Deconnexion a la base de donnees
|
||||
public String[] disconnect(){
|
||||
String[] tableau = new String[2];
|
||||
tableau[0] = "0";
|
||||
tableau[1] = "Je suis sense me deconnecter !";
|
||||
|
||||
return tableau;
|
||||
}
|
||||
|
||||
}
|
BIN
P51/connexionBDD/Principal.class
Normal file
BIN
P51/connexionBDD/Principal.class
Normal file
Binary file not shown.
95
P51/connexionBDD/Principal.java
Normal file
95
P51/connexionBDD/Principal.java
Normal file
@ -0,0 +1,95 @@
|
||||
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 Principal {
|
||||
|
||||
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 ENSEIGNANTS"); // executeQuery ne retourne jamais null !
|
||||
System.out.println("## Requ<71>te: Envoy<6F>e et re<72>ue.");
|
||||
|
||||
// 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(resultat.toString());
|
||||
|
||||
System.out.println(rsmd.getColumnName(2) + " " + rsmd.getColumnName(3));
|
||||
|
||||
resultat.isBeforeFirst();
|
||||
System.out.println();
|
||||
|
||||
resultat.next();
|
||||
// On s'occupe de la premi<6D>re ligne
|
||||
//System.out.println(resultat.toString()); // Donne la connexion actuelle
|
||||
System.out.println(resultat.getObject("NOM") + " " + resultat.getObject("PRENOM"));
|
||||
|
||||
// Affichage du r<>sultat
|
||||
resultat.next();
|
||||
// traitement de la premi<6D>re ligne
|
||||
System.out.println(resultat.getObject("NOM") + " " + resultat.getObject("PRENOM"));
|
||||
|
||||
while(resultat.next()){
|
||||
//traitement des autres lignes
|
||||
System.out.println(resultat.getObject("NOM") + " " + resultat.getObject("PRENOM"));
|
||||
}
|
||||
|
||||
} 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");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user