AbdRoufBUET

database

May 4th, 2021
447
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DatabaseAccess;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.sql.*;
  11.  
  12. import javax.servlet.ServletException;
  13. import javax.servlet.annotation.WebServlet;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17.  
  18. /**
  19.  * Servlet implementation class database_servlet
  20.  */
  21. @WebServlet("/database_servlet")
  22. public class database_servlet extends HttpServlet {
  23.     private static final long serialVersionUID = 1L;
  24.        
  25.     /**
  26.      * @see HttpServlet#HttpServlet()
  27.      */
  28.     public database_servlet() {
  29.         super();
  30.         // TODO Auto-generated constructor stub
  31.     }
  32.  
  33.     /**
  34.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  35.      */
  36.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37.         // JDBC driver name and database URL
  38.           String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  39.           String DB_URL="jdbc:mysql://localhost/servlet_test";
  40.  
  41.           //  Database credentials
  42.           String USER = "root";
  43.           String PASS = "";
  44.  
  45.           // Set response content type
  46.           response.setContentType("text/html");
  47.           PrintWriter out = response.getWriter();
  48.           String title = "Database Result";
  49.          
  50.           String docType =
  51.              "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
  52.          
  53.           out.println(docType +
  54.              "<html>\n" +
  55.              "<head><title>" + title + "</title></head>\n" +
  56.              "<body bgcolor = \"#f0f0f0\">\n" +
  57.              "<h1 align = \"center\">" + title + "</h1>\n");
  58.           Connection conn = null;
  59.           Statement stmt = null;
  60.          
  61.           try {
  62.              // Register JDBC driver
  63.              Class.forName("com.mysql.jdbc.Driver");
  64.  
  65.              // Open a connection
  66.              conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/servlet_test","root","");
  67.  
  68.              // Execute SQL query
  69.              stmt = conn.createStatement();
  70.              String sql;
  71.              sql = "SELECT id, first, last, age FROM employees";
  72.              ResultSet rs = stmt.executeQuery(sql);
  73.  
  74.              // Extract data from result set
  75.              while(rs.next()){
  76.                 //Retrieve by column name
  77.                 int id  = rs.getInt("id");
  78.                 int age = rs.getInt("age");
  79.                 String first = rs.getString("first");
  80.                 String last = rs.getString("last");
  81.  
  82.                 //Display values
  83.                 out.println("ID: " + id + "<br>");
  84.                 out.println(", Age: " + age + "<br>");
  85.                 out.println(", First: " + first + "<br>");
  86.                 out.println(", Last: " + last + "<br>");
  87.              }
  88.              out.println("</body></html>");
  89.  
  90.              // Clean-up environment
  91.              rs.close();
  92.              stmt.close();
  93.              conn.close();
  94.           } catch(SQLException se) {
  95.              //Handle errors for JDBC
  96.              System.out.println(se);
  97.           } catch(Exception e) {
  98.              //Handle errors for Class.forName
  99.               //System.out.println("hello");
  100.              e.printStackTrace();
  101.           } finally {
  102.              //finally block used to close resources
  103.              try {
  104.                 if(stmt!=null)
  105.                    stmt.close();
  106.              } catch(SQLException se2) {
  107.              } // nothing we can do
  108.              try {
  109.                 if(conn!=null)
  110.                 conn.close();
  111.              } catch(SQLException se) {
  112.                 se.printStackTrace();
  113.              } //end finally try
  114.           } //end try
  115.     }
  116.  
  117.     /**
  118.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  119.      */
  120.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  121.         // TODO Auto-generated method stub
  122.         doGet(request, response);
  123.     }
  124.  
  125. }
  126.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×