Aujourd'hui je test la connexion aux SGBD avec Java.
Et la je regrette php et PDO.
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 */ /** * chaine de connexion du sgdb choisit */ /** * nom du SGBD à utiliser (oracle, mysql, postgre, oci8 etc etc) */ /** * nom de la base que l'on va utiliser */ /** * url de connexion complete */ /** * les options à passer lors de la connextion (dotn user et password obligatoirement) */ /** * Objet de connexion */ /** * 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 */ 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 */ this.base = base; } /** * Permet d'ajouter une propriété puor la connexion * @param String pName * @param String pValue * @return boolean * @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 */ //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"); } if (this.con != null) return true; else return false; } return false; } /** * Permet de fermet la connecxion au sgbd * @return * @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 */ if (requete.isEmpty()){ } else { //if (stmt != null) stmt.close(); return data; } } /** * Affiche la liste des drivers indiqués à la classe */ public void listeDriver(){ int majorVersion = driver.getMajorVersion(); int minorVersion = driver.getMinorVersion(); " v"+majorVersion+"."+minorVersion); } } /** * Choix du driver en fonction du choix * @param drv * @throws Exception */ 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(); 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 ? !)