Mysql MAX() function using Servlet

Mysql MAX() function using Servlet


Posted in : Java Posted on : July 16, 2011 at 4:46 PM Comments : [ 0 ]

In this section we will discuss how the max() function is used in mysql using servlet.

Mysql MAX() function using Servlet

In this section we will discuss how the max() function is used in mysql using servlet.

First of all lets know about the max() function of mysql that what it does and how it performs, MAX() function gives the highest number from the two given numbers. When this function is called over list of an integer numbers it starts the comparison between two numbers form the list that which one is greater between them, the greater number is returned in first comparison then it compares with the other numbers of a list and so on, this process is performed till all the number in a list is not compared then this function finally returns the greatest number among them. Now lets come to our goal that we have to use the max() function statement in java servlet. So at first I created a table named employee in mysql then created a class in java named MaxServlet which extends the class HttpServlet. Further I overridden the doGet() method into which HttpServletRequest and HttpServletResponse objects are created. Using the object of HttpServletResponse I called the getWriter() method of ServletResponse interface which is inherited by HttpServletResponse interface. Now since we have to use a SQL query in java so we should have a connection with database system, to solve such problem I used the java.sql package of java and established a connection with mysql through classes and interfaces of this package. After establishing a connection I used the query (which is our actual goal) into the prepareStatement() method of Connection interface which returns a PreparedStatement object then uses the executeQuery() method of PreparedStatement interface which returns an object of ResultSet through which I extracted the result.

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 MaxServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.10.13/data";
String uid = "root";
String psw = "root";
String sql = "SELECT MAX(Salary) AS MaxSalary FROM employee";
Connection con;
PreparedStatement ps;
ResultSet rs;
try
{
Class.forName(driver);
con = DriverManager.getConnection(url,uid,psw);
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
pw.println("<table><tr>");
pw.println("<td><b>MaxSalary</b></td>");
pw.println("</tr><tr>");
while(rs.next())
{
Integer maxsalary = rs.getInt(1);
pw.println("<td>"+maxsalary+"</td>");
pw.println("</tr>");
}
pw.println("</table>");
}
catch(SQLException sx)
{
pw.println(sx);
}
catch(ClassNotFoundException cx)
{
pw.println(cx);
}
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>join</display-name> 

<servlet>
<servlet-name>MaxServlet</servlet-name>
<servlet-class>MaxServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MaxServlet</servlet-name>
<url-pattern>/MaxServlet</url-pattern>
</servlet-mapping>

</web-app>

Output :

1. The employee table which I had created earlier :

2. When you will execute the query you will get the following output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics