Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. public void enregistrer(int a, int b) {
  2.         try {
  3.             Class.forName( "com.mysql.jdbc.Driver" ); // Chargement du driver JDBC pour MySQL
  4.         } catch (ClassNotFoundException e1) {
  5.             e1.printStackTrace();
  6.         }
  7.        
  8.         /* Connexion à la base de données */
  9.         String url = "jdbc:mysql://localhost:3306/jee";
  10.         String utilisateur = "root"; //sur phpMyAdmin, le nom d'utilisateur doit être "root"
  11.         String motDePasse = ""; // et il ne faut pas utiliser un mot de passe
  12.         Connection connexion = null;
  13.         Statement statement = null;
  14.         try {
  15.             connexion = DriverManager.getConnection( url, utilisateur, motDePasse );           
  16.             /* Création de l'objet gérant les requêtes */
  17.             statement = (Statement) connexion.createStatement();           
  18.             /* Exécution d'une requête d'insértion */
  19.             statement.executeUpdate("insert into tab(a,b) values("+a+","+b+");");
  20.         } catch ( SQLException e ) {
  21.             System.out.println(e.getMessage());
  22.         }
  23.         finally {
  24.             if ( statement != null ) {
  25.                 try {
  26.                     statement.close();
  27.                 } catch ( SQLException ignore ) {
  28.                 }
  29.             }
  30.            
  31.             if ( connexion != null ) {
  32.                 try {
  33.                     connexion.close();
  34.                 } catch ( SQLException ignore ) {
  35.                 }
  36.             }
  37.         }
  38.  
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement