In this tutorial you will learn how to use '>' operator in sql queries in mysql using servlet.
MySql GREATER ( > ) Operator Example
In this tutorial you will learn how to use '>' operator in sql queries in mysql using servlet.
> (greater) operator in mysql compares the two operands to find the greater value from one of them 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 greater value of a list form 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 MysqlGreater.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 greater than 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, empId FROM userInfo WHERE empId > 5". This query will return list of names and empId from the table userInfo of which empId is greater than the 5.
MysqlGreater.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 MysqlGreater 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>"); out.println("<table>"); out.println("<th>empId > deptId</th>"); while(rs.next()) { String em= rs.getString("empId > deptId"); out.println("<tr>"); out.println("<td align= middle>"+em+"</td>"); out.println("</tr>"); } out.println("</table>"); out.println("</html>"); } 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>MysqlGreater</servlet-name> <servlet-class>MysqlGreater</servlet-class> </servlet> <servlet-mapping> <servlet-name>MysqlGreater</servlet-name> <url-pattern>/MysqlGreater</url-pattern> </servlet-mapping> </web-app>
Output :
Database table userInfo has the following data following :
When you will execute the servlet example using the query 'select empId > deptId from userInfo' you will get the output like as :
But when you will execute the servlet example using the query 'select name,empId from userInfo where empId > 5 ' you will get the value as :
[ 0 ] Comments