Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Pascal Surget
Added: Apr 14, 2018 1:54 AM
Views: 13
  1.  
  2. package ca.qc.cgodin.models;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10.  
  11. public class DBManager {
  12.     private static Connection con;
  13.     private static Statement stm;
  14.     private static String serverName;
  15.     private static String database;
  16.     private static String user;
  17.     private static String password;
  18.     private static int port;
  19.    
  20.     public static void createConnection(String serverName, String database, String user, String password, int port)throws ClassNotFoundException, SQLException{
  21.         DBManager.serverName=serverName;
  22.         DBManager.database=database;
  23.         DBManager.user=user;
  24.         DBManager.password=password;
  25.         DBManager.port=port;
  26.         Class.forName("com.mysql.jdbc.Driver");
  27.         String connectionURL = "jdbc:mysql://"+serverName + ":"+port+"/"+ database+ "?user="+user+"&password="+password;
  28.         con=DriverManager.getConnection(connectionURL);
  29.     }
  30.    
  31.     public static int executeUpdate(String requete) throws SQLException{
  32.         Statement stm=con.createStatement();
  33.         int nbLigne=stm.executeUpdate(requete);
  34.         stm.close();
  35.         return nbLigne;
  36.     }
  37.    
  38.     public static ResultSet executeQuery(String requete)throws SQLException{
  39.         Statement stm=con.createStatement();
  40.         ResultSet res=stm.executeQuery(requete);
  41.         return res;
  42.     }
  43.    
  44.     public static void fermerConnection()throws SQLException{
  45.         con.close();
  46.     }
  47.    
  48. }
  49.