MySql IS Operator With Servlet Example

MySql IS Operator With Servlet Example


Posted in : Java Posted on : November 16, 2011 at 6:55 PM Comments : [ 0 ]

In this tutorial you will learn about how to use mysql's is operator with servlet.

MySql IS Operator With Servlet Example

In this tutorial you will learn about how to use mysql's is operator with servlet.

IS NOT operator checks a value versus a boolean value i.e. it checks whether the value is true or not, false or not, unknown or not, null or not. If the statement gets satisfied it returns 1 otherwise returns 0.

Example :

To use this operator in the example first I created a table using the mysql database system named 'userInfo' and put the values in their respective fields. Since we have to use this operator with servlet so I created a servlet class named MysqlIsOprtr.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. And because of we have to use mysql's 'is' operator 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 operator I used the query "SELECT empId IS UNKNOWN, empId IS TRUE FROM userInfo".

MysqlIsOprtr.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 MysqlIsOprtr 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 IS UNKNOWN, empId IS TRUE FROM userInfo";

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

out.println("<html>");
out.println("<table border=1>");
out.println("<th>empId IS UNKNOWN</th><th>empId IS TRUE</th>");

while(rs.next())
{
String u = rs.getString("empId IS UNKNOWN");
String t = rs.getString("empId IS TRUE"); 
out.println("<tr>");
out.println("<td align=middle>"+u+"</td>");
out.println("<td align=middle>"+t+"</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>MysqlIsOprtr</servlet-name>
<servlet-class>MysqlIsOprtr</servlet-class>
</servlet>

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

</web-app>

Output :

Data in the database table is as :

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

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics