Advertisement
Guest User

Untitled

a guest
May 8th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package bdd;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6. public class BaseDeDonnees {
  7.  
  8.     /**
  9.      * URL de connection
  10.      */
  11.     private final static String URL = "jdbc:mysql://localhost:3306/projet_bdd?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  12.  
  13.     /**
  14.      * Nom du user
  15.      */
  16.     private final static String LOGIN = "root";
  17.  
  18.     /**
  19.      * Mot de passe du user
  20.      */
  21.     private final static String PASSWORD = "";
  22.  
  23.     public Connection getConnection() throws SQLException {
  24.         return DriverManager.getConnection(URL, LOGIN, PASSWORD);
  25.     }
  26.  
  27.     public void executerRequete(String req) {
  28.  
  29.         Connection con = null;
  30.         Statement stmt = null;
  31.  
  32.         try {
  33.             con = getConnection();
  34.             stmt = con.createStatement();
  35.             final ResultSet rset = stmt.executeQuery(req);
  36.  
  37.             while (rset.next()) {
  38.                 int n = rset.getMetaData().getColumnCount();
  39.  
  40.                 for(int i = 1; i <= n; i++)
  41.                     System.out.print(rset.getString(i) + " ");
  42.  
  43.                 System.out.print('\n');
  44.             }
  45.  
  46.         } catch (SQLException e) {
  47.             e.printStackTrace();
  48.         } finally {
  49.  
  50.             if (stmt != null) {
  51.                 try {
  52.                     stmt.close();
  53.                 } catch (SQLException e) {
  54.                     e.printStackTrace();
  55.                 }
  56.             }
  57.  
  58.             if (con != null) {
  59.                 try {
  60.                     con.close();
  61.                 } catch (SQLException e) {
  62.                     e.printStackTrace();
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement