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
| <%--
Document : AfficherEmploye
Created on : 1 avr. 2011, 10:26:42
Author : poste
--%>
<%@
page contentType="text/html"
pageEncoding="UTF-8"
import ="java.io.IOException"
import ="java.io.PrintWriter"
import ="javax.servlet.ServletException"
import ="javax.servlet.annotation.WebServlet"
import ="javax.servlet.http.HttpServlet"
import ="javax.servlet.http.HttpServletRequest"
import ="javax.servlet.http.HttpServletResponse"
import ="java.sql.*"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Affichage des employes</title>
</head>
<body>
<h1>Affichage des employes</h1>
<%
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception ex){
out.println("Erreur du chargement du Driver");
out.println(ex.getMessage());
}
try{
String URL = "jdbc:mysql://localhost/test?" +
"user=root&password=";
conn = DriverManager.getConnection(URL);
String sql = "SELECT * FROM employee";
stmt= conn.createStatement();
// Execution de la requette:
ResultSet res = stmt.executeQuery(sql);
// recupérer les MetaData;
ResultSetMetaData meta = res.getMetaData();
// les entêtes :
Object[] colonne = new Object[meta.getColumnCount()];
for(int i = 1 ; i <= meta.getColumnCount(); i++){
colonne[i-1] = meta.getColumnName(i);
}
//Petite manipulation pour obtenir le nombre de lignes
res.last();
int rowCount = res.getRow();
Object[][] data = new Object[rowCount][meta.getColumnCount()];
//On revient au départ
res.beforeFirst();
int j = 1;
//On remplit le tableau d'Object[][]
while(res.next()){
for(int i = 1 ; i <= meta.getColumnCount(); i++)
data[j-1][i-1] = res.getObject(i);
j++;
}
//on ferme le tout
res.close();
stmt.close();
}catch (SQLException e) {
out.println("Erreur SQl");
out.println("SQLException: " + e.getMessage());
}finally {
out.close();
}
%>
</body>
</html> |
Partager