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();
}
}
}
} |
Partager