In this tutorial you will learn how to use '<=' operator of mysql in servlet.
MySql LESS THAN OR EQUAL TO ( <= ) Operator Example
In this tutorial you will learn how to use '<=' operator of mysql in servlet.
<= (less than or equal to) operator of mysql compares the two operands for their less than or equal values and returns 1 if the statement is true otherwise returns 0 and null if both or either side of operand is declared to null. It returns all the value of a list which are less than or equals to the specified number.
Example :
In this example first I created a table named userInfo for fetching the data. To use this mysql operator in servlet I created a class named MysqlLessThanEqual.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 the mysql query so we are required to connect with a database package (here I am using MySql) that's why in further steps I perform the database connectivity with servlet using the java.sql package. In example to use the above operator I use the query "SELECT empId <= deptId FROM userInfo". This query will return the value 1 if the value of empId field is less than or equal to the value of deptId, else return 0 and null if both or either side of these operands value is getting null.
This operator can also be used as "select name from userInfo where empId <= 8". This query will return all the name from the table of which empId is less than or equal to the 8.
MysqlLessThanEqual.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 MysqlLessThanEqual 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/data";
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 empId <= deptId FROM userInfo";
ps= con.prepareStatement(sql);
rs= ps.executeQuery();
out.println("<html><table border=1>");
out.println("<tr><td align=left><b>empId <= deptId</b></td></tr>");
while(rs.next())
{
String le= rs.getString("empId <= deptId");
out.println("<tr><td align=middle>"+le+"</td>");
out.println("</tr>");
}
out.println("</table></html>");
rs.close();
ps.close();
con.close();
}
catch(ClassNotFoundException cx)
{
out.println(cx);
}
catch(SQLException sx)
{
out.println(sx);
}
}
}
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>MysqlLessThanEqual</servlet-name> <servlet-class>MysqlLessThanEqual</servlet-class> </servlet> <servlet-mapping> <servlet-name>MysqlLessThanEqual</servlet-name> <url-pattern>/MysqlLessThanEqual</url-pattern> </servlet-mapping> </web-app>
Output :
Data in the database table is as following :

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


[ 0 ] Comments