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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Statistiques extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
//tableau conteant les objets CALL
ArrayList<Call> calls=new ArrayList<Call>();
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
ArrayList<String> datatab= new ArrayList<String>();
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://192.168.1.48:5432/asterisk", "asterisk",
"proformatique");
ResultSet resultats = null;
String requete = "SELECT * FROM call_log";
try {
Statement stmt = connection.createStatement();
resultats = stmt.executeQuery(requete);
Call acall=new Call();
while(resultats.next()) {
System.out.println(resultats.getString("id"));
System.out.println(resultats.getString("date"));
System.out.println(resultats.getString("duration"));
datatab.add(resultats.getString("id"));
acall.setID(resultats.getString("id"));
datatab.add(resultats.getString("date"));
acall.setDate(resultats.getString("date"));
datatab.add(resultats.getString("duration"));
acall.setDuration(resultats.getString("duration"));
calls.add(acall);
}
for (Call asTring: calls) {
System.out.println(asTring.toString());
}
} catch (SQLException e) {
//traitement de l'exception
}
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
Statistiques fen = new Statistiques(datatab, calls);
fen.setVisible(true);
}
public Statistiques(ArrayList<String> datatab, ArrayList<Call> calls){
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTable");
this.setSize(300, 120);
for (String string : datatab) {
System.out.println("-------"+string);
}
String Stringtableau="{";
for (Call acall : calls) {
Stringtableau+= acall.toString()+ "\n" ;
}
//Stringtableau+="}";
System.out.println(Stringtableau);
//Les données du tableau
Object[][] data = {datatab.toArray()
//{resultats.getString("id"), resultats.getString("date"), resultats.getString("duration")}
};
//Les titres des colonnes
String title[] = {"ID", "Date", "Durée"};
JTable tableau = new JTable(data, title);
// ArrayList tableauDynamique = new ArrayList(Arrays.asList(tableauSimple));
//Nous ajoutons notre tableau à notre contentPane dans un scroll
//Sinon les titres des colonnes ne s'afficheront pas !
this.getContentPane().add(new JScrollPane(tableau));
}
} |
Partager