Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.*;
- import javax.sql.*;
- import javax.naming.*;
- import javax.ejb.*;
- import java.util.Properties;
- @WebServlet("/Dbaccess.do")
- public class Dbaccess extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType ("text/html");
- PrintWriter out = response.getWriter ();
- out.println ("<?xml version=\"1.0\"?>");
- out.println ("<!DOCTYPE html PUBLIC \"-//W3C//DTD" + "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + "/TR/xhtml1-strict.dtd\">");
- out.println ("<html xmlns=\"http://www.w3.org/1999/html1\">");
- out.println ("<head>");
- out.println ("<title>DBCoffee Example</title>");
- out.println ("</head>");
- out.println ("<body>");
- out.println ("<h1> Coffees Table </h1>");
- out.println ("<table>");
- out.println ("<tr>");
- out.println ("<td>COF_NAME</td>");
- out.println ("<td>SUP_ID</td>");
- out.println ("<td>PRICE</td>");
- out.println ("<td>SALES</td>");
- out.println ("<td>TOTAL</td>");
- out.println ("</tr>");
- Connection con = null;
- Statement stmt = null;
- try{String driver = "com.mysql.jdbc.Driver";
- Class.forName(driver).newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/coffee?user=root&password=nikola94");
- stmt = con.createStatement ();
- ResultSet rs = stmt.executeQuery("select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES");
- while (rs.next()){
- String coffeeName = rs.getString("COF_NAME");
- int supplierID = rs.getInt("SUP_ID");
- float price = rs.getFloat("PRICE");
- int sales = rs.getInt("SALES");
- int total = rs.getInt("TOTAL");
- out.println ("<tr>");
- out.println ("<td>" + coffeeName + "</td>");
- out.println ("<td>" + supplierID + "</td>");
- out.println ("<td>" + price + "</td>");
- out.println ("<td>" + sales + "</td>");
- out.println ("<td>" + total + "</td>");
- out.println ("</tr>");
- }
- rs.close ();
- stmt.close ();
- con.close();
- }
- catch (SQLException e){System.err.println(e.getMessage());}
- catch (ClassNotFoundException e){System.err.println(e.getMessage());}
- catch (IllegalAccessException e){System.err.println(e.getMessage());}
- catch (InstantiationException e){System.err.println(e.getMessage());}
- out.println ("</table>");
- out.println ("</body>");
- out.println ("</html>");
- out.close ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement