Guest User

Untitled

a guest
Apr 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class Connecter {
  5.     static final String CONN_URL = "jdbc:oracle:thin:@ensibm.imag.fr:1521:ensi2";
  6.     static final String USER = "leroyo";
  7.     static final String PASSWD = "leroyo";
  8.     static Connection conn;
  9.     protected static PreparedStatement stmt;
  10.     protected static ResultSet rset;
  11.  
  12.  
  13.     public Connecter() {
  14.         try {
  15.             // Enregistrement du driver Oracle
  16.             System.out.print("Loading Oracle driver... ");
  17.             DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  18.             System.out.println("loaded");
  19.  
  20.             // Etablissement de la connection
  21.             System.out.print("Connecting to the database... ");
  22.             conn = DriverManager.getConnection(CONN_URL, USER, PASSWD);
  23.             System.out.println("connected");
  24.  
  25.             // Desactivation des commits auto
  26.             conn.setAutoCommit(false);
  27.             conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
  28.  
  29.         } catch (SQLException e) {
  30.             System.err.println("Connexion failed");
  31.             e.printStackTrace(System.err);
  32.         }
  33.     }
  34.  
  35.     public static Connection getConnec(){
  36.         return conn;
  37.     }
  38.  
  39.     public void fermer() {
  40.         try {
  41.             conn.close();
  42.         } catch (SQLException e) {
  43.             System.err.println("Connexion failed");
  44.             e.printStackTrace(System.err);
  45.         }
  46.     }
  47.  
  48.     public static void dumpResultSet(ResultSet rset) throws SQLException {
  49.         ResultSetMetaData rsetmd = rset.getMetaData();
  50.         int i = rsetmd.getColumnCount();
  51.         for (int k=1;k<=i;k++)
  52.             System.out.print(rsetmd.getColumnName(k) + "\t");
  53.         System.out.println();
  54.         while (rset.next()) {
  55.             for (int j = 1; j <= i; j++) {
  56.                 System.out.print(rset.getString(j) + "\t");
  57.             }
  58.             System.out.println();
  59.         }
  60.         rset.close();
  61.     }
  62.  
  63.  
  64.     public static void main(String args[]) {
  65.         new Connecter();
  66.         System.out.println("");
  67.         System.out.println("Que voulez-vous faire ?");
  68.         System.out.println("1 : Initialiser la base");
  69.         System.out.println("2 : Gérer les restaurants");
  70.         System.out.println("3 : Gérer les cartes");
  71.         System.out.println("4 : Gérer les réservations");
  72.         System.out.println("Veuillez saisir votre choix");
  73.         Scanner sc = new Scanner(System.in);
  74.         int choix = sc.nextInt();
  75.         sc.nextLine();
  76.         if(choix ==1){
  77.         String path = "/home/ensi2a/leroy/Ensimag_2B";
  78.                     String query = "start " + path +"/projetBD/creationTables.sql";
  79.         System.out.println(query);
  80.         try {
  81.             stmt = getConnec().prepareStatement(query);
  82.             stmt.executeUpdate();
  83.             stmt.close();
  84.         } catch (SQLException e) {
  85.             // TODO Auto-generated catch block
  86.             e.printStackTrace();
  87.         }
  88.         query = "start " + path +"/projetBD/creationSequences.sql";
  89.         System.out.println(query);
  90.         try {
  91.             stmt = getConnec().prepareStatement(query);
  92.             stmt.execute();
  93.             stmt.close();
  94.         } catch (SQLException e) {
  95.             // TODO Auto-generated catch block
  96.             e.printStackTrace();
  97.         }
  98.         query = "start " + path +"/projetBD/insertionExemple.sql";
  99.         System.out.println(query);
  100.         try {
  101.             stmt = getConnec().prepareStatement(query);
  102.             stmt.execute();
  103.             stmt.close();
  104.         } catch (SQLException e) {
  105.             // TODO Auto-generated catch block
  106.             e.printStackTrace();
  107.         }
  108.        
  109.         }
  110.         else if(choix ==2){
  111.             //CreerRestaurants.main(null);
  112.         }
  113.         else if (choix == 3){
  114.             System.out.println("TO DO");
  115.         }
  116.         else if (choix == 4){
  117.             System.out.println("TO DO");
  118.         }
  119.         //Puis on ferme la connexion
  120.         try {
  121.             conn.close();
  122.         } catch (SQLException e) {
  123.             System.err.println("Connexion failed");
  124.             e.printStackTrace(System.err);
  125.         }
  126.     }
  127. }
Add Comment
Please, Sign In to add comment