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
|
import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
//import javax.servlet.GenericServlet.*;
//import javax.servlet.http.HttpServlet.*;
public class TutoPool extends HttpServlet {
private DataSource ds = null; //la source de données
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head></head>");
out.println("<body>");
Connection con=null;
Statement s=null;
ResultSet rs=null;
try {
//récupération de la Connection depuis le DataSource
con = ds.getConnection();
s = con.createStatement();
rs = s.executeQuery("SELECT * FROM sca_dossier");
while (rs.next()) {
out.println(rs.getString(1) + " ");
out.println(rs.getString(2) + "<br/>");
}
} catch (SQLException e) {
response.sendError(500, "Exception sur l'accès à la BDD " + e);
}finally {
if (rs != null)
{
try {
rs.close();
} catch (SQLException e) {}
rs = null;
}
if (s != null) {
try {
s.close();
} catch (SQLException e) {}
s = null;
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {}
con = null;
}
}
out.println("</body>");
out.println("</html>");
out.close();
}
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
// TODO Raccord de méthode auto-généré
//super.doPost(arg0, arg1);
doGet(arg0, arg1);
}
/* (non-Javadoc)
* @see javax.servlet.GenericServlet#init()
*/
public void init() throws ServletException {
// TODO Raccord de méthode auto-généré
//super.init();
try {
//récupération de la source de donnée
//int un = 1;
Context initCtx = new InitialContext();
//recuperation du contexte standard J2EE
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//recuperation de la datasource
ds = (DataSource) envCtx.lookup("jdbc/servletBD");
//ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/servletBD");
} catch (Exception e) {
throw new UnavailableException(e.getMessage());
}
}
/* (non-Javadoc)
* @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
*/
//public void init(ServletConfig arg0) throws ServletException {
// TODO Raccord de méthode auto-généré
//super.init(arg0);
//}
} |