Précédent   Forum des professionnels en informatique > Bases de données > PostgreSQL
PostgreSQL Forum PostgreSQL. Avant de poster -> F.A.Q PostGreSQL Tutoriels PostGreSQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 24/06/2006, 20h29   #1
Candidat au titre de Membre du Club
 
Développeur informatique
Inscription : mars 2004
Messages : 35
Détails du profil
Informations personnelles :
Âge : 31

Informations professionnelles :
Activité : Développeur informatique

Informations forums :
Inscription : mars 2004
Messages : 35
Points : 11
Points : 11
Par défaut Connexion avec JDBC

Bonjour,

Je programme en java sous eclipse. Je voudrais savoir comment référencer le jar (le driver JDBC) avec mon projet en cours.

Pouriez-vous m'indiquer les étapes?

Merci d'avance.
volontier est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/07/2006, 12h01   #2
Membre du Club
 
Inscription : décembre 2005
Messages : 81
Détails du profil
Informations forums :
Inscription : décembre 2005
Messages : 81
Points : 66
Points : 66
Envoyer un message via MSN à jarod_ab
bonjour.

d'abord télécharger le pilote jdbc du SGBD Postgres.
puis dans eclipse ajoutes le dans la bibliothèque pour cela vas dans projet/java build path puis clic sur add external jars.

une fois fait, essaye la connexion, voici un exemple :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
import java.sql.*;
 
/*
 * Created on 24 févr. 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
 
/**
 * @author jarod
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class maclasse {
public static void main(String []argument){
	Connection connexion = NULL;
 
	try
	{
	Class.forName("org.postgresql.Driver");
 
	connexion = DriverManager.getConnection("jdbc:postgresql://localhost/mabase", "postgres", "postgres");
	//Statement instruction = connexion.createStatement();
	//ResultSet résultat = instruction.executeQuery("SELECT * FROM employer");
	Statement instruction = connexion.createStatement();
	//instruction.executeUpdate("INSERT INTO employer"
      //      +" VALUES (4, 'SCOTT','2/2/1973')");
 
	ResultSet résultat = instruction.executeQuery("select * from employer");
 
	while (résultat.next()){
		System.out.println("--------------------------------------");
		System.out.println("Matricule = "+résultat.getInt("code"));
		System.out.println("Nom : "+résultat.getString("nom"));
	}
	PreparedStatement instruction2 = connexion.prepareStatement("select * from employer where code = ?");
	instruction2.setInt(1,2);
	ResultSet res = instruction2.executeQuery();
	while (res.next()){
		System.out.println("--------------------------------------");
		System.out.println("Matricule = "+res.getInt("code"));
		System.out.println("Nom : "+res.getString("nom"));
	}
	IF (connexion!=NULL) connexion.close();
 
	}
	catch (ClassNotFoundException ex) { System.err.println("Erreur Driver");}
	catch (SQLException ex) { System.err.println("Erreur Localisation BD");}   
finally {
IF (connexion!=NULL) // libération de la connexion si elle existe
	try {
		connexion.close();
		System.out.print("connexion fermée");
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}	    
}
 
}
jarod_ab est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 23h32.


 
 
 
 
Partenaires

Hébergement Web