Подключение к базе
Окружить блок
Создаем сервлет
/WebApplicationBD/src/java/my/BDServlet.java |
@WebServlet(name = "BDServlet", urlPatterns = {"/BDServlet"})
public class BDServlet extends HttpServlet {
@Resource(name = "ds")
private DataSource ds;
Processes requests for both HTTP <code>GET</code> and <code>POST</code>
methods.
@param request
@param response
@throws
@throws
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Connection connection = ds.getConnection();
Statement query = connection.createStatement();
ResultSet rs = query.executeQuery("select * from APP.PHONE");
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BDServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet BDServlet at " + request.getContextPath() + "</h1>");
out.println("<table border=\"1\">");
while (rs.next()) {
int n = rs.getInt("n");
String name = rs.getString("name");
String phone = rs.getString("phone");
out.println("<tr> ");
out.println("<td>"+n+"</td>");
out.println("<td>"+name+"</td>");
out.println("<td>"+phone+"</td>");
out.println("</tr>");
}
rs.close();
connection.close();
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
} catch (SQLException ex) {
Logger.getLogger(BDServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}