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
|
package servlet;
import org.jfree.data.jdbc.JDBCPieDataset;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
public class Graphique extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/Baignade?user=root&password=root");
} catch (SQLException e) {
e.printStackTrace();
}
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
JDBCPieDataset dataset = new JDBCPieDataset(connection);
try {
dataset.executeQuery("SELECT `id`, `Numero` From Technicien order by id desc");
JFreeChart chart = ChartFactory.createPieChart("Pie Chart", dataset, true, true, false);
if (chart != null) {
response.setContentType("image/png");
OutputStream out = response.getOutputStream();
ChartUtilities.writeChartAsPNG(out, chart, 450, 400);
}
}
catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection != null){connection.close();}
}
catch (SQLException e) {e.printStackTrace();}
}
} |
Partager