Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package be.iulian.database;
- import java.sql.*;
- import javax.swing.JOptionPane
- ;
- public class Connection {
- public static void main(String[] args) throws ClassNotFoundException, SQLException{
- try{
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- }catch(ClassNotFoundException ex){
- JOptionPane.showMessageDialog(null, "Classe de driver introuvable" + ex.getMessage());
- System.exit(0);
- }
- Connection connec = null;
- Statement stmt = null;
- ResultSet res = null;
- try{
- String url = "jdbc:odbc:MuscleTou.accdb";
- connec = DriverManager.getConnection(url);
- String requete = "SELECT NomProduit, PrixVente FROM Produits";
- stmt = connec.createStatement();
- res = stmt.executeQuery(requete);
- String nom;
- int prix;
- while(res.next()){
- nom = res.getString(1);
- prix = res.getInt(2);
- System.out.println(prix + " " + nom);
- }
- }catch(SQLException ex){
- JOptionPane.showMessageDialog(null, "Erreur JDBC: " + ex.getMessage());
- }
- finally{
- try{
- if( res != null){
- res.close();
- }if (stmt != null){
- stmt.close();
- }
- if (connec!= null){
- connec.close();
- }
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement