Quand on regrette php en Java

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 :)

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package toolsbox;
  6.  
  7. import java.sql.*;
  8. import java.util.Enumeration;
  9. import java.util.Properties;
  10. /**
  11.  * @author moogli
  12.  * @version 0.2
  13.  * @since 2011/12/07
  14.  */
  15. public class JDO {
  16. /*
  17.   * les infos pour la connexion au sgbd
  18.   */
  19. /**
  20.   * nom ou ip du serveur
  21.   */
  22. private String host;
  23.  
  24. /**
  25.   * chaine de connexion du sgdb choisit
  26.   */
  27. private String driver;
  28.  
  29. /**
  30.   * nom du SGBD à utiliser (oracle, mysql, postgre, oci8 etc etc)
  31.   */
  32. private String sgbd;
  33.  
  34. /**
  35.   * nom de la base que l'on va utiliser
  36.   */
  37. private String base;
  38.  
  39. /**
  40.   * url de connexion complete
  41.   */
  42. private String url;
  43.  
  44. /**
  45.   * les options à passer lors de la connextion (dotn user et password obligatoirement)
  46.   */
  47. private Properties proprietes = new Properties();
  48.  
  49. /**
  50.   * Objet de connexion
  51.   */
  52. private Connection con;
  53.  
  54. /**
  55.   * Constructeur on indique le pilote, le chemin et les propriété (dont user et password)
  56.   * on garde que celui ci ? les autres servent pas à grand chose ? on sais dès le départ toutes ces infos
  57.   * @param String driver
  58.   * @param String host
  59.   * @param Properties p
  60.   * @throws Exception
  61.   */
  62. public JDO (String s, String host, Properties p) throws Exception{
  63. if (!s.isEmpty() && (p != null) && !host.isEmpty()){
  64. this.sgbd = s;
  65. selectDriver(this.sgbd);
  66. this.proprietes = p;
  67. this.host = host;
  68. }
  69. }
  70. /**
  71.   * indique la base à utiliser
  72.   * @param String base
  73.   */
  74. public void setBase(String base) {
  75. this.base = base;
  76. }
  77.  
  78. /**
  79.   * Permet d'ajouter une propriété puor la connexion
  80.   * @param String pName
  81.   * @param String pValue
  82.   * @return boolean
  83.   * @throws Exception
  84.   */
  85. public boolean setProprietes(String pName, String pValue) throws Exception{
  86. if ((pName.isEmpty()== true) && (pValue.isEmpty()==true)){
  87. return false;
  88. }
  89. this.proprietes.setProperty(pName,pValue);
  90. return true;
  91. }
  92. /**
  93.   * Pemrt de ce connecter au SGBD
  94.   * @return boolean
  95.   * @throws SQLException
  96.   */
  97. public boolean connect() throws Exception {
  98. //this.selectDriver(this.sgbd); // optionnel depuis la mise dans le
  99. if (!this.host.isEmpty() && !this.driver.isEmpty() && !this.base.isEmpty()){
  100. this.url = "jdbc:" + this.driver + this.host + "/" + this.base;
  101. if ( this.proprietes.containsKey("user") == false && this.proprietes.containsKey("password") == false) {
  102. throw new Exception("Il faut indiquer au moins l'utilisateur et le mot de passe avant une connexion");
  103. }
  104. this.con = DriverManager.getConnection(this.url, this.proprietes);
  105. if (this.con != null)
  106. return true;
  107. else
  108. return false;
  109. }
  110. return false;
  111. }
  112. /**
  113.   * Permet de fermet la connecxion au sgbd
  114.   * @return
  115.   * @throws Exception
  116.   */
  117. public boolean disconnect() throws Exception {
  118. if (this.con != null){
  119. this.con.close();
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125.   * Permet de faire une requete SQL
  126.   * @param String requete
  127.   * @return ResultSet
  128.   * @throws Exception
  129.   */
  130. public ResultSet query(String requete) throws Exception{
  131. if (requete.isEmpty()){
  132. throw new Exception("La requete ne peux être vide");
  133. }
  134. else {
  135. Statement stmt = this.con.createStatement();
  136. ResultSet data = stmt.executeQuery(requete);
  137. //if (stmt != null) stmt.close();
  138. return data;
  139. }
  140. }
  141. /**
  142.   * Affiche la liste des drivers indiqués à la classe
  143.   */
  144. public void listeDriver(){
  145. for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements();){
  146. Driver driver = (Driver)e.nextElement();
  147. int majorVersion = driver.getMajorVersion();
  148. int minorVersion = driver.getMinorVersion();
  149. System.out.println("Driver = "+driver.getClass()+
  150. " v"+majorVersion+"."+minorVersion);
  151. }
  152. }
  153.  
  154. /**
  155.   * Choix du driver en fonction du choix
  156.   * @param drv
  157.   * @throws Exception
  158.   */
  159. private void selectDriver(String drv) throws Exception{
  160. String nomDriver = "";
  161. switch (drv){
  162. case "mysql":
  163. nomDriver = "com.mysql.jdbc.Driver";
  164. this.driver = "mysql://";
  165. break;
  166. case "oracle":
  167. nomDriver = "oracle.jdbc.OracleDriver";
  168. this.driver = "oracle:thin:@";
  169. break;
  170. case "oci8":
  171. nomDriver = "oracle.jdbc.OracleDriver";
  172. this.driver = "oracle:oci8:@";//a vérifier
  173. break;
  174. case "postgresql":
  175. nomDriver = "org.postgresql.Driver";
  176. this.driver = "postgresql://";
  177. break;
  178. case "odbc":
  179. default:
  180. nomDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
  181. this.driver = "odbc:";
  182. break;
  183. }
  184. // chargement du driver pour le sgbd choisit
  185. try{
  186. Class.forName(nomDriver).newInstance();
  187. }catch(ClassNotFoundException cnfe){
  188. System.out.println("La classe "+nomDriver+" n'a pas été trouvée");
  189. cnfe.printStackTrace();
  190. }
  191. }
  192. }

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 ? !)