Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package be.iulian.database;
  2. import java.sql.*;
  3. import javax.swing.JOptionPane
  4. ;
  5. public class Connection {
  6.  
  7.     public static void main(String[] args) throws ClassNotFoundException, SQLException{
  8.  
  9.         try{
  10.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  11.         }catch(ClassNotFoundException ex){
  12.             JOptionPane.showMessageDialog(null, "Classe de driver introuvable" + ex.getMessage());
  13.             System.exit(0);
  14.         }
  15.        
  16.         Connection connec = null;
  17.         Statement stmt = null;
  18.         ResultSet res = null;
  19.        
  20.         try{
  21.             String url = "jdbc:odbc:MuscleTou.accdb";
  22.             connec = DriverManager.getConnection(url);
  23.             String requete = "SELECT NomProduit, PrixVente FROM Produits";
  24.             stmt = connec.createStatement();
  25.            
  26.            
  27.             res = stmt.executeQuery(requete);
  28.             String nom;
  29.             int prix;
  30.             while(res.next()){
  31.                 nom = res.getString(1);
  32.                 prix = res.getInt(2);
  33.                 System.out.println(prix + " " + nom);
  34.             }
  35.         }catch(SQLException ex){
  36.             JOptionPane.showMessageDialog(null, "Erreur JDBC: " + ex.getMessage());
  37.         }
  38.        
  39.         finally{
  40.             try{
  41.                 if( res != null){
  42.                     res.close();
  43.                 }if (stmt != null){
  44.                     stmt.close();
  45.                 }
  46.                 if (connec!= null){
  47.                     connec.close();
  48.                 }
  49.             }catch(SQLException ex){
  50.                 ex.printStackTrace();
  51.             }
  52.            
  53.         }
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement