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.
EXAMPLE
import java.io.IOException;
import java.io.PrintWriter;
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
{
rp.setContentType("text/html");
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;
try
{
Class.forName(driver);
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>");
while(rs.next())
{
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)
{
e.printStackTrace();
}
disp.close();
}
public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
{
doGet(rq,rp);
}
}
WEB.XML
<servlet>
<servlet-name>MySqlAliasServlet</servlet-name>
<servlet-class>MySqlAliasServlet</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
<servlet-name>MySqlAliasServlet</servlet-name>
<url-pattern>/MySqlAliasServlet</url-pattern>
</servlet-mapping>