INTERVAL() Function MySql Example

INTERVAL() Function MySql Example


Posted in : Java Posted on : November 15, 2011 at 7:25 PM Comments : [ 0 ]

In this tutorial you will learn how to use mysql's "interval()" function in servlet.

INTERVAL() Function MySql Example

In this tutorial you will learn how to use mysql's "interval()" function in servlet.

INTERVAL() function in mysql compares the first number with the rest of the number of a list and returns the index (started from the first number by 0) of the last number if list is in descending order, returns 0 if the list is in ascending order, returns the index of the number which is greater than the first number if rest of the number in a list are smaller than the first number in such case indexing is started from the second number of list by 0, and returns -1 if the first number of list is null.

Example :

In this example first I created a servlet class named MysqlInterval.java which extends the class HttpServlet to support the http functionality to servlet, into which I defined the doGet() method into which the object's of HttpServeltRequest and HttpServletResponse are created. Since we have to use mysql's interval() function in servlet so we are required to connect with the database package (here I am using MySql)  that's why in this example I perform the database connectivity with servlet using the java.sql package in java. To use this function I used the query "SELECT INTERVAL(81,80,79)". This query will return the index 2.

If you will use the queries select interval(81,80,83) and select interval(81,82,83) then it will return the value 1 and 0 respectively.

MysqlInterval.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MysqlInterval extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();

String className= "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://192.168.10.13";
String user= "root";
String password= "root";

Connection con;
PreparedStatement ps;
ResultSet rs;

try
{
Class.forName(className);
con= DriverManager.getConnection(url, user, password);

String sql= "SELECT INTERVAL(81,80,79)";

ps= con.prepareStatement(sql);
rs= ps.executeQuery();

while(rs.next())
{
String in = rs.getString(1);
out.println("index value is : "+in);
}
}
catch(SQLException sx)
{
out.println(sx);
}
catch(ClassNotFoundException cx)
{
out.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>mysqlComparisonFunctionAndOperator</display-name>
<servlet>
<servlet-name>MysqlInterval</servlet-name>
<servlet-class>MysqlInterval</servlet-class>
</servlet>

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

</web-app>

Output :

When you will execute the example you will get the output like as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics