Quand on regrette php en Java

Aujourd'hui je test la connexion aux SGBD avec Java.

Et la je regrette php et PDO.

Le

Vu le merdier que c'est pour ce connecter à un sgbd me suis tiens PDO c'est quand même pas mal :)

voici donc ce que j'ai fait, mais bon reste à faire un truc bien :)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package toolsbox;

import java.sql.*;
import java.util.Enumeration;
import java.util.Properties;
/**
 * @author moogli
 * @version 0.2
 * @since 2011/12/07
 */
public class JDO {
     /*
     * les infos pour la connexion au sgbd
     */
    /**
     * nom ou ip du serveur
     */
    private String host;
    
    /**
     * chaine de connexion du sgdb choisit
     */
    private String driver;
    
    /**
     * nom du SGBD à utiliser (oracle, mysql, postgre, oci8 etc etc)
     */
    private String sgbd;
    
    /**
     * nom de la base que l'on va utiliser
     */
    private String base;
    
    /**
     * url de connexion complete
     */
    private String url; 
    
    /**
     * les options à passer lors de la connextion (dotn user et password obligatoirement)
     */
    private Properties proprietes = new Properties();
    
    /**
     * Objet de connexion
     */
    private Connection  con;
   
    /**
     * Constructeur on indique le pilote, le chemin et les propriété (dont user et password)
     * on garde que celui ci ? les autres servent pas à grand chose ? on sais dès le départ toutes ces infos
     * @param String driver
     * @param String host
     * @param Properties p
     * @throws Exception 
     */
    public JDO (String s, String host, Properties p) throws Exception{
        if (!s.isEmpty() && (p != null) && !host.isEmpty()){
            this.sgbd = s;
            selectDriver(this.sgbd);
            this.proprietes = p;
            this.host = host;
        }
    }
    /**
     * indique la base à utiliser
     * @param String base 
     */
    public void setBase(String base) {
        this.base = base;
    }

    /**
     * Permet d'ajouter une propriété puor la connexion
     * @param String pName
     * @param String pValue
     * @return boolean
     * @throws Exception 
     */
    public boolean setProprietes(String pName, String pValue) throws Exception{
        if ((pName.isEmpty()== true) && (pValue.isEmpty()==true)){
            return false;
        }
        this.proprietes.setProperty(pName,pValue);
        return true;
    }
    /**
     * Pemrt de ce connecter au SGBD
     * @return boolean
     * @throws SQLException 
     */
    public boolean connect() throws Exception {
        //this.selectDriver(this.sgbd); // optionnel depuis la mise dans le 
        if (!this.host.isEmpty() && !this.driver.isEmpty() && !this.base.isEmpty()){
            this.url = "jdbc:" + this.driver + this.host + "/" + this.base;
            if ( this.proprietes.containsKey("user") == false &&  this.proprietes.containsKey("password") == false) {
                throw new Exception("Il faut indiquer au moins l'utilisateur et le mot de passe avant une connexion");
            }
            this.con = DriverManager.getConnection(this.url, this.proprietes);
            if (this.con != null)
                return true;
            else
                return false;
        }
        return false;
    }
    /**
     * Permet de fermet la connecxion au sgbd
     * @return
     * @throws Exception 
     */
    public boolean disconnect() throws Exception {
        if (this.con != null){
            this.con.close();
            return true;
        }
        return false;
    }
    /**
     * Permet de faire une requete SQL
     * @param String requete
     * @return ResultSet
     * @throws Exception 
     */
    public ResultSet query(String requete) throws Exception{
        if (requete.isEmpty()){
            throw new Exception("La requete ne peux être vide");
        }
        else {
            Statement stmt = this.con.createStatement();
            ResultSet data = stmt.executeQuery(requete);
            //if (stmt != null) stmt.close();
            return data;
        }
    }
    /**
     * Affiche la liste des drivers indiqués à la classe
     */
    public void listeDriver(){
        for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements();){
            Driver driver = (Driver)e.nextElement();
            int majorVersion = driver.getMajorVersion();
            int minorVersion = driver.getMinorVersion();
            System.out.println("Driver = "+driver.getClass()+
                " v"+majorVersion+"."+minorVersion);
        }
    }
    
    /**
     * Choix du driver en fonction du choix
     * @param drv
     * @throws Exception 
     */
    private void selectDriver(String drv) throws Exception{
        String nomDriver = "";
        switch (drv){
            case "mysql":
                nomDriver = "com.mysql.jdbc.Driver";
                this.driver = "mysql://";
                break;
            case "oracle":
                nomDriver = "oracle.jdbc.OracleDriver";
                this.driver = "oracle:thin:@";
                break;
            case "oci8":
                nomDriver = "oracle.jdbc.OracleDriver";
                this.driver = "oracle:oci8:@";//a vérifier
                break;
            case "postgresql":
                nomDriver = "org.postgresql.Driver";
                this.driver = "postgresql://";
                break;
            case "odbc":
            default:
                nomDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
                this.driver = "odbc:";
                break;
        }
        // chargement du driver pour le sgbd choisit
        try{
            Class.forName(nomDriver).newInstance(); 
        }catch(ClassNotFoundException cnfe){
            System.out.println("La classe "+nomDriver+" n'a pas été trouvée");
            cnfe.printStackTrace();
        }
    }
}

et op, faudra que je test sur oracle 8 et postgresql mais mysql et oracle fonctionne c'est déjà pas mal :)

prochaine étape un singleton ? !)