In this section we will discuss how the min() function is used in mysql using servlet.
Mysql MIN() function using Servlet
In this section we will discuss how the min() function is used in mysql using servlet.
First of all lets know about the min() function of mysql that what it does and how it performs, MIN() function gives the smallest number from the two given number. 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 smaller between them, the smaller 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 smallest number among them. Now lets come to our goal that we have to use the min() function statement in java servlet. So at first I created a table named employee in mysql then created a class in java named MinServlet 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 :
MinServlet.java
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 MinServlet 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 MIN(Salary) AS MinSalary 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>MinSalary</b></td>"); pw.println("</tr><tr>"); while(rs.next()) { Integer minsalary = rs.getInt(1); pw.println("<td>"+minsalary+"</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>MinServlet</servlet-name> <servlet-class>MinServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MinServlet</servlet-name> <url-pattern>/MinServlet</url-pattern> </servlet-mapping> </web-app>
Output :
The 'employee' table which I had created earlier :
When you will execute the above example you will get the following output :
[ 0 ] Comments