by Dinesh Thakur Category: Servlet with MySql Database

This function use to make a Alias name of any column to use as a reference in mySql.

First i import all the required packages from java Library. Then i made a class named 'MySqlAliasServlet' extends the 'HttpServlet'.Then i use serviceMethod() which will getting the request from doGet()Method for Output. Then load all the required Drivers. After then i declare some required variables for accessing database. Like Connection this variable use to use to make a link between the database and the actual code its will make a bridge between both procedures with i can access the database. With this i also declare the resultSet variable that will help user to fetch the value from required columns and rows with its object. The other one variable i declare is preparedStatement this will use to execute the Query like executeQuery() as (SELECT last_name, salary, salary*12 annum FROM worker).and in the last i call the doGet()Method which will bring the Output on the Web Browser.

While show Output on a Web Browser i use 'HTML' Code and some of these tags to get output in the tabular form it makes the output a manner  form.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlAliasServlet extends HttpServlet
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
          con = DriverManager.getConnection(url,uid,psw);
          ps=con.prepareStatement("SELECT last_name, salary, salary*12 annum FROM worker");
          rs = ps.executeQuery();
              String title = "Annual Salary ";
          String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
          "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Salary</th><th>Annual Salary</th>\n"+ "</body> </html>");
              String nm = rs.getString("last_name");
                  int sal = rs.getInt("salary");
                  int c=rs.getInt("annum");
              disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + sal +"<td align=\"center\">" + c +"</td></tr>" );  
                  catch(Exception e) 
                      public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
<!-- servlet mapping -->

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles