• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Servlet

MySql Period Diff Function Java Servlet

By Dinesh Thakur

This Function Will Returns the number of months between periods.

Firstly, i made a table in database named ‘dbase’ with required fields and values in it. I import all the required java packages from the java library. I made a class named ‘MySqlPeriodDiffFunc’, which extends the ‘HttpServlet’. After then i use serviceMethod() which will bring the request from doGet()method for output. Then after i loaded all required drivers for the database accessing. I declare some mandatory variable for database connecting as i declare ‘Connection’ (this variable will use to create a link between database and the java code or actual code). The next will be ‘resultSet’ the variable will be responsible for the getting values from required columns and rows. The other one variable will be ‘preparedStatement’ this variable will use to execute the Selected Query like executeQuery() as (Select PERIOD_DIFF(201405,201402)).i use doGet()Method which will bring the output on web browser.

While show Output on a Web Browser i use ‘HTML’ Code and some of these tags to get output in the tabular form it makes the output a manner form.

 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 MySqlPeriodDiffFunc extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
               {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("Select PERIOD_DIFF(201405,201402) AS pd");
                   rs = ps.executeQuery();
                               String title = "Using Period Diff Function";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                               disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                               "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Result</th>\n"+ "</body> </html>");
                   while(rs.next())
                       {
                            String curr = rs.getString(1);
                                        disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                       }     
               }          
                                       catch(Exception e) 
                               {
                                       e.printStackTrace();
                                           }
                                       disp.close();
         }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                  {
                                                 doGet(rq,rp);
                                                  }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlPeriodDiffFunc</servlet-name>
   <servlet-class>MySqlPeriodDiffFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlPeriodDiffFunc</servlet-name>
   <url-pattern>/MySqlPeriodDiffFunc</url-pattern>
</servlet-mapping>

MySql PeriodAdd Function in Java Servlet

By Dinesh Thakur

This Function Will Adds a period to a year-month.

Now i make a table into a database named ‘dbase’ within the reference of mySql(php myAdmin). I import all the required java packages from java library. Then i make a class named ‘MySqlPeriodAddFunc’, which extends the ‘HttpServlet’. Then i use serviceMethod() which will use to get the request from doGet() method for Output on web Browser. Then i loaded all the mandatory drivers for database accessing. After, that I’ll have to declare the variables like ‘connection’ (this variable will responsible for the creating a link between database and the java code (actual code). The other variable is ‘resultSet’ (that will help to fetch value from the selected columns and rows as required).The next will be ‘preparedStatement’ that will be used to execute the selected query like executeQuery() as (Select PERIOD_ADD(20144,3)).The last instance will be to call the doGet()Method that will bring Output on the web browser.

To make output Impressive i use ‘HTML’ tags to get Output in tabular form on a web browser.  

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 MySqlPeriodAddFunc extends HttpServlet
   {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
       {
          rp.setContentType("text/html");
          PrintWriter disp = rp.getWriter();
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://localhost/dbase";
          String uid = "root";
          String psw = "root";
          Connection con=null;
          PreparedStatement ps = null;
          ResultSet rs;
          try
           {
                Class.forName(driver);
                con = DriverManager.getConnection(url,uid,psw);
                ps=con.prepareStatement("Select PERIOD_ADD(20144,3) AS pa");
                rs = ps.executeQuery();
                            String title = "Using Period Add Function";
                String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                            disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                 "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Result</th>\n"+ "</body> </html>");
                while(rs.next())
                   {
                         String curr = rs.getString(1);
                                     disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                   }     
            }             
                                    catch(Exception e) 
                                         {
                                      e.printStackTrace();
                                         }
                                      disp.close();
       }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                  {
                                                 doGet(rq,rp);
                                                  }
   }
WEB.xml
<servlet>
   <servlet-name>MySqlPeriodAddFunc</servlet-name>
   <servlet-class>MySqlPeriodAddFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlPeriodAddFunc</servlet-name>
   <url-pattern>/MySqlPeriodAddFunc</url-pattern>
</servlet-mapping>

MySql Parantheses in Java Servlet

By Dinesh Thakur

The Expressions in Parenthesis (Brackets) will be executed.

I made a table named ‘worker’ into database named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all the required packages from java library. Then i made a class named ‘MySqlParanthesesJavaServlet’ extends the ‘HttpServlet’. Then i use serviceMethod() which will use to getting request from the doGet()Method for Output. Then load all the required Drivers. Then i declare some mandatory variables like connection (Connection variable is been used to make a bridge or link between database and actual code (java code)). As after creating link between both platforms, i declare resultSet, which will be use to fetch the values from required fields according to user. The other one variable is preparedStatement the object of this variable is ‘ps’ this will use to execute the query like executeQuery() as(SELECT name, salary, 12*(salary+100) cal FROM worker). To in the end i use doGet()Method for getting value on the web Browser.

For getting the output in the tabular form, i use ‘HTML’ code and tags that will show output in a manner way on web browser.  

 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 MySqlParanthesesJavaServlet extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
              {
                    Class.forName(driver);
                    con = DriverManager.getConnection(url,uid,psw);
                    ps=con.prepareStatement("SELECT name, salary, 12*(salary+100) cal FROM worker");
                    rs = ps.executeQuery();
                                String title = "Employee's Info With Using Of Parantheses";
                    String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                                disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                     "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                                 "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Name</th><th>Salary</th><th>Cal</th>\n"+ "</body> </html>");
                     while(rs.next())
                          {
                               String nm = rs.getString("name");
                                           int sal = rs.getInt("salary");
                                           int c=rs.getInt("cal");
                               disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + sal +"<td align=\"center\">" + c +"</td></tr>" );  
                          }     
               }          
                                          catch(Exception e) 
                                          {
                                               e.printStackTrace();
                                          }
                                               disp.close();
        }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                 {
                                                 doGet(rq,rp);
                                                 }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlParanthesesJavaServlet</servlet-name>
   <servlet-class>MySqlParanthesesJavaServlet</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlParanthesesJavaServlet</servlet-name>
   <url-pattern>/MySqlParanthesesJavaServlet</url-pattern>
</servlet-mapping>

MySql OR Operater in Java Servlet

By Dinesh Thakur

This is known To Logical operator, which checks the condition, and then execute.

I made a table named ‘org’ into a database named ‘dbase’ within the reference of mySql(php myAdmin). Now, i import all the java packages required by user. After calling all the packages i make a class named ‘MySqlOROperater’ extends the ‘HttpServlet’. Now to use serviceMethod() which will getting the request from doGet() for output. After then load all the required Drivers. Some mandatory variable also to define like resultset this variable will responsible for getting the value from desired Columns and rows. Connection variable, which will establish the link between the database and actual code (frontEnd and backEnd).  And the PreparedStatement this variable does execute the given query like executeQuery() as (SELECT emp_id, last_name,salary,job_id FROM org WHERE salary >=10000 OR job_id LIKE ‘%man%’).and in the end the doGet() method has to be used while getting the Output on the web Browser.

For getting value in a manner way, i use ‘HTML’ code tags which will present the Output on the Web Browser in a tabular form.

 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 MySqlOROperater extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
              rp.setContentType("text/html");
              PrintWriter disp = rp.getWriter();
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://localhost/dbase";
              String uid = "root";
              String psw = "root";
              Connection con=null;
              PreparedStatement ps = null;
              ResultSet rs;
              try
               {
                    Class.forName(driver);
                    con = DriverManager.getConnection(url,uid,psw);
                    ps=con.prepareStatement("SELECT emp_id, last_name,salary,job_id FROM org WHERE salary >=10000 OR job_id LIKE '%man%'");
                    rs = ps.executeQuery();
                                String title = "Employee's Info With Using of OR Query";
                    String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                                disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
                                "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Employee Id</th><th>Last Name</th><th>Salary</th><th>Job Id</th>\n"+ "</body> </html>");
                    while(rs.next())
                         {
                                int e_id = rs.getInt("emp_id");
                                    String l_name = rs.getString("last_name");
                                    int sal = rs.getInt("salary");
                                    String j_id=rs.getString("job_id");
                                    disp.println("<tr><td align=\"center\">" + e_id +  "<td align=\"center\">" + l_name +"<td align=\"center\">" + sal +"<td align=\"center\">" + j_id +"</td></tr>" );        
                         }     
               }          
                                            catch(Exception e) 
                                          {
                                               e.printStackTrace();
                                          }
                                               disp.close();
          }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                 {
                                                 doGet(rq,rp);
                                                 }
   }
WEB.XML
 
<servlet>
   <servlet-name>MySqlOROperater</servlet-name>
   <servlet-class>MySqlOROperater</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlOROperater</servlet-name>
   <url-pattern>/MySqlOROperater</url-pattern>
</servlet-mapping>

MySql ORDER Function in Java Servlet

By Dinesh Thakur

This Function Will return the leftmost character of the argument is a multi-byte character, returns the code for that character returns the code for that character.

First i import all the required java packages from java library. Then after, i made a class named ‘MySqlORDFunction’ extends the ‘HttpServlet’.i use the serviceMethod() which is responsible for the getting request from doGet() method. Then after, need to load all the drivers as required. And to declare the mandatory variables like Connection (the variable Connection will use to establish the link between database and java code (actual code). ResultSet the variable resultset does the job to fetch the value from required columns and rows. PreparedStatement this variable use to execute the query like executeQuery() as (Select ORD(‘mysql’)) and after then i use doGet() method to get the output on the web Browser.

To see the result in tabular form we use ‘HTML’ tags for Impressive looking Output on a web browser.

 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 MySqlORDFunction extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
              rp.setContentType("text/html");
              PrintWriter disp = rp.getWriter();
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://localhost/dbase";
              String uid = "root";
              String psw = "root";
              Connection con=null;
              PreparedStatement ps = null;
              ResultSet rs;
              try
               {
                    Class.forName(driver);
                    con = DriverManager.getConnection(url,uid,psw);
                    ps=con.prepareStatement("Select ORD('mysql')");
                    rs = ps.executeQuery();
                                String title = "Using ORD Function";
                    String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                                disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                                "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
                    while(rs.next())
                        {
                             String val = rs.getString(1);
                                         disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                        }     
               }          
                                         catch(Exception e) 
                                          {
                                               e.printStackTrace();
                                          }
                                               disp.close();
          }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                 {
                                                 doGet(rq,rp);
                                                 }
   }
WEB.XML
 
<servlet>
   <servlet-name>MySqlORDFunction</servlet-name>
   <servlet-class>MySqlORDFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlORDFunction</servlet-name>
   <url-pattern>/MySqlORDFunction</url-pattern>
</servlet-mapping>

MySql OrderBy Multi Columns in Java Servlet

By Dinesh Thakur

This function Will sort the value of selected Columns (Multiple Columns) Descending or Ascending.

First i made a table named ‘dept’ into a database named ‘dbase’ within the reference of mySql(php myAdmin). In the starting of Program, we need to import all the Required Packages from the java library. Then to declare a class named ‘MySqlOrderByMultiCol’, which is been extends ‘HttpServlet’. After then we have to use serviceMethod() which is responsible for the Getting request for Output. Then load drivers and create an Object of resultSet, which useful to show the table result as an Output. Connection variable also to be declare for interact between database and actual code as like in between frontend and backend. Now on other hand preparedStatement will be use for executing query like executeQuery() as (SELECT last_name, dept_id, salary FROM dept ORDER BY dept_id, salary DESC). And to fetch the result on web browser doGet() method will be initialize.

While getting Output on the Web Browser we use ‘HTML’ Code or some of these tags to present Output in tabular form.

 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 MySqlOrderByMultiCol extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
              {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("SELECT last_name, dept_id, salary FROM dept ORDER BY dept_id, salary DESC");
                   rs = ps.executeQuery();
                               String title = "Order By Multiple Columns";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                  "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Dept Id</th><th>Salary</th>\n"+ "</body> </html>");
                  while(rs.next())
                     {
                          String nm = rs.getString("last_name");
                                      int d_id=rs.getInt("dept_id");
                                      int sal=rs.getInt("salary");
                                      disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + d_id +"<td align=\"center\">" + sal +  "</td></tr>" );  
                     }     
               }          
                                      catch(Exception e) 
                                         {
                                     e.printStackTrace();
                                         }
                                     disp.close();
         }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                {
                                                 doGet(rq,rp);
                                                }
   }
WEB.xml
 
 
<servlet>
   <servlet-name>MySqlOrderByMultiCol</servlet-name>
   <servlet-class>MySqlOrderByMultiCol</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlOrderByMultiCol</servlet-name>
   <url-pattern>/MySqlOrderByMultiCol</url-pattern>
</servlet-mapping>

MySql OrderBy Column Alias in Java Servlet

By Dinesh Thakur

This Function is Been used to Sort the value Ascending or Descending order Within The Alias of a column reference.

First i made a table named ‘dept’ into a database named ‘dbase’ within the reference of mySql(php myAdmin). In the starting of Program, we need to import all the Required Packages from the java library. Then to declare a class named ‘MySqlOrderByColumnAlias’ which is been extends ‘HttpServlet’. After then we have to use serviceMethod() which is responsible for the Getting request for Output. Then load drivers and create an Object of resultSet, which will, useful to show the table result as an Output. Connection variable also to be declare for interact between database and actual code as like in between frontend and backend. Now on other hand preparedStatement will be use for executing query like executeQuery() as (SELECT name,job,salary, salary*12  annsal FROM dept ORDER BY annsal DESC). And to fetch the result on web browser doGet() method will be initialize.

For getting output in tabular form i use ‘HTML’ tags with that the output will looks in a manner way.

 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 MySqlOrderByColumnAlias extends HttpServlet
   {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
             {
                 Class.forName(driver);
                 con = DriverManager.getConnection(url,uid,psw);
                 ps=con.prepareStatement("SELECT name,job,salary, salary*12  annsal FROM dept ORDER BY annsal DESC");
                 rs = ps.executeQuery();
                             String title = "Employee's Info And Salary With Using Of Order By Query ";
                 String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                 "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Name</th><th>job</th><th>Salary</th><th>Annual Salary</th>\n"+ "</body> </html>");
                 while(rs.next())
                      {
                           String nm = rs.getString("name");
                                       String jb=rs.getString("jb");
                                       int sal=rs.getInt("salary");
                                       Double c=rs.getDouble("annsal");
                           disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + jb +"<td align=\"center\">" + sal +  "<td align=\"center\">" + c +"</td></tr>" );  
                      }     
             }            
                                        catch(Exception e) 
                                          {
                                              e.printStackTrace();
                                          }
                                              disp.close();
       }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                   {
                                                 doGet(rq,rp);
                                                   }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlOrderByColumnAlias</servlet-name>
   <servlet-class>MySqlOrderByColumnAlias</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlOrderByColumnAlias</servlet-name>
   <url-pattern>/MySqlOrderByColumnAlias</url-pattern>
</servlet-mapping>

MySql Operator Precedence in Java Servlet

By Dinesh Thakur

This Function will make a computation of given expressions.

I make a table named ‘worker’ into a database named ‘dbase’ within the reference of mySql(php myAdmin).Then We have to call The Packages From java Library. Then i Make a Class Named ‘MySqlOperatorPrecedence’ Extends “HttpServlet”. After, Initializing The Class i Declare The Methods Public void Service()Method (The service method call the doGet method for a GET request from User) and In That The Objects called “Response Object” and “Request Object Which will Work According To Work. After that i Load the Driver and Make Variables Of Resultset,Connection to establish The link between database and Query that Execute The Condition after Calling. After Declaring The Prepared statement i have to put the Query (“SELECT name, salary, 12*salary+100 cal FROM worker”).After that i Use executeQuery() Method to Extract the Result On a Web Browser.

I Use ‘HTML’ code and some tags, which will show the output in tabular form on the web browser.

 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 MySqlOperatorPrecedence extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
              {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("SELECT name, salary, 12*salary+100 cal FROM worker");
                   rs = ps.executeQuery();
                               String title = "Employee's Info With Using Of Operator Precedence";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                               disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                               "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Name</th><th>Salary</th><th>Calculation</th>\n"+ "</body> </html>");
                   while(rs.next())
                       {
                            String nm = rs.getString("name");
                                        int sal = rs.getInt("salary");
                                        int c=rs.getInt("cal");
                            disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + sal +"<td align=\"center\">" + c +"</td></tr>" );  
                       }     
               }          
                                        catch(Exception e) 
                                           {
                                       e.printStackTrace();
                                           }
                                       disp.close();
         }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                   {
                                                 doGet(rq,rp);
                                                   }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlOperatorPrecedence</servlet-name>
   <servlet-class>MySqlOperatorPrecedence</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlOperatorPrecedence</servlet-name>
   <url-pattern>/MySqlOperatorPrecedence</url-pattern>
</servlet-mapping>

MySql OCT Function in Java Servlet

By Dinesh Thakur

This Function will return a string representation of the octal argument.

First i made a database named ‘dbase’ within the reference of mySql(php myAdmin).Then i import all the required java package from java library according to  need of program. Then i made a class named ‘MySqlOCTFunction’, which extends ‘HTTPSERVLET’ completely. Then after i use service()Method that will bring the request from doGet()Method for output on the web browser.

Then after, i loaded al the required drivers for database accessing. Here i declare variables for next use of program i declare ‘connection’ that will use to create a link between the database and the java code. Here i use ‘resultSet’ that will use to get the value from the selected columns and the rows. At the last instance of declaring variable i use ‘preparedStatement’ that will use to executing the selected Query like executeQuery as (Select OCT(18)). I use doGet()Method for output on web browser.

To get output on the web browser in a tabular form i use ‘HTML’ code and tag which bring output in a manner way.

 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 MySqlOCTFunction extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
             {
                  Class.forName(driver);
                  con = DriverManager.getConnection(url,uid,psw);
                  ps=con.prepareStatement("Select OCT(18)");
                  rs = ps.executeQuery();
                              String title = "Using OCT Function";
                  String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                               "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
                  while(rs.next())
                     {
                          String val = rs.getString(1);
                                      disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                     }     
             }            
                                      catch(Exception e) 
                             {
                                      e.printStackTrace();
                                         }
                                      disp.close();
       }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                   {
                                                 doGet(rq,rp);
                                                   }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlOCTFunction</servlet-name>
   <servlet-class>MySqlOCTFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlOCTFunction</servlet-name>
   <url-pattern>/MySqlOCTFunction</url-pattern>
</servlet-mapping>

MySql OCTET LENGTH Function in Java Servlet

By Dinesh Thakur

This function Is a synonym for LENGTH ().

First I made a table in database named ‘dbase’ within the reference of mySql(Php myAdmin). I import all the required java packages from java library. After then i declare a class named ‘MySqlOCTETLENGTHFunction’, which extends the ‘HttpServlet’. After i use serviceMethod() responsible for the getting request from doGet() method. Then, i load all required drivers for database accessing. Then i declare some variables like connection (this will create a bridge between the database and the java code or in other words Frontend and Backend with the help of loaded drivers. The other variable is to declare will be resultset it will use to get the value from required columns and rows into on to the database. The next one will be the preparedStatement the object of the variable is ps. this will be responsible for executing the selected query like executeQuery() as (Select OCTET_LENGTH(‘mysql’)).The next step to use doGet() method which will bring the output on a web browser.

On the web browser for making Output a effective look we will use ‘HTML’ code and tags to bring the Output in tabular form as required.

 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 MySqlOCTETLENGTHFunction extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
              rp.setContentType("text/html");
              PrintWriter disp = rp.getWriter();
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://localhost/dbase";
              String uid = "root";
              String psw = "root";
              Connection con=null;
              PreparedStatement ps = null;
              ResultSet rs;
              try
                {
                     Class.forName(driver);
                     con = DriverManager.getConnection(url,uid,psw);
                     ps=con.prepareStatement("Select OCTET_LENGTH('mysql')");
                     rs = ps.executeQuery();
                                String title = "Using OCTET_LENGTH Function";
                    String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                               disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                               "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
                   while(rs.next())
                       {
                             String val = rs.getString(1);
                                         disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                       }     
                            }     
                                         catch(Exception e) 
                                     {
                                         e.printStackTrace();
                                     }
                                         disp.close();
        }
                                          public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                   {
                                                 doGet(rq,rp);
                                                   }
   }
Web.xml
 
<servlet>
   <servlet-name>MySqlOCTETLENGTHFunction</servlet-name>
   <servlet-class>MySqlOCTETLENGTHFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlOCTETLENGTHFunction</servlet-name>
   <url-pattern>/MySqlOCTETLENGTHFunction</url-pattern>
</servlet-mapping>

MySql NULLSAFE Function in Java Servlet

By Dinesh Thakur

This Function Will Show the ‘NULL value is equal or not’.

Then first i made a database named ‘dbase’ within the reference of mySql(php myAdmin). After i import all required java packages from java library. Then i made a class named ‘ MySqlNULLSAFEFunction’, which extends ‘HttpServlet’. After that i use service()Method that will bring the request from doGet()Method for output on the web browser. Then i loaded all the required drivers that are mandatory for database accessing. Then i use variable ‘connection’ that will use to create a link between the database and the java code. After i use ‘resultSet’ this variable will use to get the value from the columns and the rows. Then i use ‘preparedStatement’ that will use executing the selected Query like executeQuery as (SELECT NULL <=> 1, NULL <=> NULL, 7 <=> NULL).I use in the end doGet()Method that bring the output on the web browser.

Now i used some kind of ‘HTML’ code and tags that will present the output in a tabular form on a web browser.

 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 MySqlNULLSAFEFunction extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
              {
                  Class.forName(driver);
                  con = DriverManager.getConnection(url,uid,psw);
                  ps=con.prepareStatement("SELECT NULL <=> 1, NULL <=> NULL, 7 <=> NULL");
                  rs = ps.executeQuery();
                              String title = "Using NULL SAFE EQUAL TO Function";
                  String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                  "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
                  while(rs.next())
                      {
                             String val = rs.getString(1);
                                         disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                      }      
               }          
                                         catch(Exception e) 
                                    {
                                         e.printStackTrace();
                                    }
                                         disp.close();
         }
                                         public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                  {
                                                 doGet(rq,rp);
                                                  }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlNULLSAFEFunction</servlet-name>
   <servlet-class>MySqlNULLSAFEFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlNULLSAFEFunction</servlet-name>
   <url-pattern>/MySqlNULLSAFEFunction</url-pattern>
</servlet-mapping>

MySql Null Condition in Java Servlet

By Dinesh Thakur

This Operator used when a NULL value has to retrieve from table or Database.

I Made a table Named ‘mgr’ Into a database named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all the mandatory java packages from the java library. Then i made a class named ‘MySqlNullCondition’, which extends the ‘HttpServlet’. I use serviceMethod() which is responsible for getting request from the doGet() method for Output on a web browser. After, then i loaded all required drivers that necessary for database accessing. The next move i declare variable those objects will create a criteria to use database i use ‘connection’ variable (which will be use to create a connection between database and the java code (actual code). The next variable will be the ‘resulSet’ (this will be responsible for getting value from the desired column and rows as required). And the next one will be the ‘preparedStatement’ that will use to executing the query like executeQuery() as (SELECT name,salary,manager_id FROM mgr WHERE manager_id is NULL). At the last instance i use doGet()Method which will use to get output on a web Browser.

We use a tabular form concept. For this kind of requirement, we use ‘HTML’ coding and some of tags to use Output in Designer look to show on a web browser.

 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 MySqlNullCondition extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
              {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("SELECT name,salary,manager_id FROM mgr WHERE manager_id is NULL");
                   rs = ps.executeQuery();
                               String title = "Employee's Info With Using of Null Condition";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                               disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
                   "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Name</th><th>Salary</th><th>Manager Id</th>\n"+ "</body> </html>");
                   while(rs.next())
                      {
                           String nm = rs.getString("name");
                                       int sal = rs.getInt("salary");
                                       int m_id=rs.getInt("manager_id");
                                      disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + sal +"<td align=\"center\">" + m_id +"</td></tr>" );       
                      }     
               }          
                                      catch(Exception e) 
                                          {
                                      e.printStackTrace();
                                          }
                                      disp.close();
        }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                 {
                                                 doGet(rq,rp);
                                                 }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlNullCondition</servlet-name>
   <servlet-class>MySqlNullCondition</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlNullCondition</servlet-name>
   <url-pattern>/MySqlNullCondition</url-pattern>
</servlet-mapping>

MySql Now Function in Java Servlet

By Dinesh Thakur

This function will Returns the current date and time.

I made a table in database named ‘dbase’ with required fields and values in it and within the reference of mySql(Php Admin). Then i import all the required java package from java library. I made a class named ‘MySqlNowFunc’ extends the ‘HttpServlet’. Then i use serviceMethod() which will bring the request from doGet()Method for Output. Then i load all the drivers for database accessing. After then, i declare variables, which are necessary for database conditions as i declare connection (this variable will use to make a link between database and java code or actual code). After then i declare resultset (this variable will use to get the values from desired columns and rows as required from query).the next one will be the preparedStatement (this variable will responsible for the executing query like executeQuery() as (Select NOW()).In the last i used the doGet()Method which will bring the output on the web Browser.

On to the web browser, we need to use ‘HTML’ code and its tags to get Output in Tabular form.

 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 MySqlNowFunc extends HttpServlet
   {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
       {
           rp.setContentType("text/html");
           PrintWriter disp = rp.getWriter();
           String driver = "com.mysql.jdbc.Driver";
           String url = "jdbc:mysql://localhost/dbase";
           String uid = "root";
           String psw = "root";
           Connection con=null;
           PreparedStatement ps = null;
           ResultSet rs;
           try
             {
                 Class.forName(driver);
                 con = DriverManager.getConnection(url,uid,psw);
                 ps=con.prepareStatement("Select NOW()");
                 rs = ps.executeQuery();
                             String title = "Using Now Function";
                 String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                 "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Current Date And Time  </th>\n"+ "</body> </html>");
                 while(rs.next())
                    {
                          String curr = rs.getString(1);
                                      disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                    }     
             }            
                                      catch(Exception e) 
                                          {
                                      e.printStackTrace();
                                          }
                                      disp.close();
       }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                               {
                                           doGet(rq,rp);
                                               }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlNowFunc</servlet-name>
   <servlet-class>MySqlNowFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlNowFunc</servlet-name>
   <url-pattern>/MySqlNowFunc</url-pattern>
</servlet-mapping>

MySql Not Operater in Java Servlet

By Dinesh Thakur

This Function will execute the query of NOT OPERATOR.

I made a table Named ‘reg’ Into a database named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all the mandatory java packages from the java library. Then, i made a class named ‘MySqlNotOperater’, which extends the ‘HttpServlet’. I use serviceMethod() which is responsible for getting request from the doGet() method for Output on a web browser. After, then i loaded all required drivers that necessary for database accessing. At the next move I declare variable those objects will create a criteria to use database I use ‘connection’ variable (which will be use to create a connection between database and the java code (actual code). The next variable will be the ‘resulSet’ (this will be responsible for getting value from the desired column and rows as required).and the next one will be the ‘preparedStatement’ that will use to executing the query like executeQuery() as (SELECT last_name, job_id FROM reg WHERE job_id NOT IN (‘it_prog’, ‘clerk’, ‘manager’)). At the last instance i use doGet()Method which will use to get output on a web Browser.

For getting output in tabular form i use ‘HTML’ tags with that the output will looks in a manner way.

 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 MySqlNotOperater extends HttpServlet
   {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
       {
           rp.setContentType("text/html");
           PrintWriter disp = rp.getWriter();
           String driver = "com.mysql.jdbc.Driver";
           String url = "jdbc:mysql://localhost/dbase";
           String uid = "root";
           String psw = "root";
           Connection con=null;
           PreparedStatement ps = null;
           ResultSet rs;
           try
             {
                  Class.forName(driver);
                  con = DriverManager.getConnection(url,uid,psw);
                  ps=con.prepareStatement("SELECT last_name, job_id FROM reg WHERE job_id NOT IN ('it_prog', 'clerk', 'manager')");
                  rs = ps.executeQuery();
                              String title = "Employee's Info With Using of NOT In Query";
                  String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                  "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
                              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Job Id</th>\n"+ "</body> </html>");
                  while(rs.next())
                       {
                             String l_name = rs.getString("last_name");
                                         String j_id=rs.getString("job_id");
                                         disp.println("<tr><td align=\"center\">" + l_name +"<td align=\"center\">" + j_id +"</td></tr>" );     
                      }     
             }            
                                         catch(Exception e) 
                                              {
                                              e.printStackTrace();
                                      }
                                              disp.close();
        }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                {
                                                 doGet(rq,rp);
                                                }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlNotOperater</servlet-name>
   <servlet-class>MySqlNotOperater</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlNotOperater</servlet-name>
   <url-pattern>/MySqlNotOperater</url-pattern>
</servlet-mapping>

MySql NOTNULL Function in Java Servlet

By Dinesh Thakur

This Function will tell that the Given Expression is Null or Not.

I make a table in database named ‘dbase’ with required fields and values in it. Database is been created in the reference of mySql(php myAdmin).Here i import all the java packages required during the program from java library. Then i make a class named ‘MySqlNOTNULLFunction’ extends the ‘HttpServlet’. Here I use serviceMethod() which will bring the request from doGet()Method for Output. Here the next step will be to load all required drivers for database accessing. Now, i will declare the mandatory variables like ‘connection’ this variable will take over to create a link between the database and the java code. The next one will be the ‘resultSet’ this variable will use to retrieve the value from columns and rows as required the query. The other one will be the ‘preparedStatement’ this variable is must use for the executing selected query like executeQuery as (SELECT 7 IS NOT NULL). Here i use doGet()Method for getting output on the web Browser.

While getting Output on the Web Browser we use ‘HTML’ Code or some of these tags to present Output in tabular form.

 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 MySqlNOTNULLFunction extends HttpServlet
   {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
              {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("SELECT 7 IS NOT NULL");
                   rs = ps.executeQuery();
                               String title = "Using NOT NULL() Function";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                  "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
                 while(rs.next())
                     {
                            String val = rs.getString(1);
                                        disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                     }     
             }            
                                        catch(Exception e) 
                                    {
                                       e.printStackTrace();
                                    }
                                       disp.close();
          }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                  {
                                                 doGet(rq,rp);
                                                  }
   }
 
WEB.xml
 
<servlet>
   <servlet-name>MySqlNOTNULLFunction</servlet-name>
   <servlet-class>MySqlNOTNULLFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlNOTNULLFunction</servlet-name>
   <url-pattern>/MySqlNOTNULLFunction</url-pattern>
</servlet-mapping>

MySql MonthName Function in java Servlet

By Dinesh Thakur

This Function Will Returns the name of the month.

We have created database named ‘dbase’. After that, a class is been declared named ‘MySqlMonthNameFunc’ extends the ‘HttpServlet’. With that kind of efforts, we need to define some methods, which do their job for exporting output. After we just declare Service method()(service method does the job of calling doGet method for getting request).Some variables also define as Connection,Resultset,PreparedStatement.Connection variable use to link between database and actual code(FrontEnd and BackEnd).preparedStatement will use to send the query to database like (Select MONTHNAME(now()) AS mn)). This statement will work according to condition it shows. Now the next method executeQuery() will execute desired query and statement. And in the end doGet() method will be used to fetch the result on a web browser.

To get output in tabular form on the web browser I use ‘HTML’ code and some tags.

 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 MySqlMonthNameFunc extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
               {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("Select MONTHNAME(now()) AS mn");
                   rs = ps.executeQuery();
                               String title = "Using Month Name Function";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                               disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                               "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Month Name</th>\n"+ "</body> </html>");
                   while(rs.next())
                      {
                           String curr = rs.getString(1);
                                       disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                      }     
               }          
                                       catch(Exception e) 
                                           {
                                       e.printStackTrace();
                                           }
                                       disp.close();
         }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                {
                                                 doGet(rq,rp);
                                                }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMonthNameFunc</servlet-name>
   <servlet-class>MySqlMonthNameFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMonthNameFunc</servlet-name>
   <url-pattern>/MySqlMonthNameFunc</url-pattern>
</servlet-mapping>

MySql Month Function in Java Servlet

By Dinesh Thakur

This Function Will Returns the month from the date passed.

Then I use to make a database named ‘dbase’ that is been built in the reference of mySql(php ,yAdmin). Then i import all the required java packages from java library. Then i made a class named ‘MySqlMonthFunc’, which extends ‘HttpServlet’. Then i use service()Method that will use to getting the request from doGet()Method for output on the web browser. Then i loaded all the required drivers for the database accessing. Then i declare mandatory variables i use ‘connection’ this variable will use to create a link between database and the java code. Then i use ‘resultSet’ that return the value from selected columns and rows from table as required. Other i declare ‘preparedStatement’ that will use to executing the selected query like executeQuery() as (Select MONTH(now()) AS mn). I use doGet()Method for output on the web browser.

I use here ‘HTML’ code and some of these tags to present output in the tabular form on the web browser for manner way.

 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 MySqlMonthFunc extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
               {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("Select MONTH(now()) AS mn");
                   rs = ps.executeQuery();
                               String title = "Using Month Function";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                  "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Result</th>\n"+ "</body> </html>");
                  while(rs.next())
                       {
                            String curr = rs.getString(1);
                                        disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                       }      
               }          
                                        catch(Exception e) 
                                    {
                                        e.printStackTrace();
                                    }
                                        disp.close();
     }
                                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                                  {
                                                 doGet(rq,rp);
                                                  }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMonthFunc</servlet-name>
   <servlet-class>MySqlMonthFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMonthFunc</servlet-name>
   <url-pattern>/MySqlMonthFunc</url-pattern>
</servlet-mapping>

MySql MOD Function in Java Servlet

By Dinesh Thakur

This function will Returns the remainder of one expression by diving by another expression.

First i made a database named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all the required java packages from java library as program essentials. I made a class named ‘MySqlMODFunction’, which extends ‘HttpServlet’. Then i use service()Method which will use to get the request from the doGet()Method for output on the browser. I loaded all the required drivers for accessing database as required. Then I use to declare the variable as I use ‘connection’ this will use to create a link between the database and the java code. Then i declare ‘resultSet’ this will responsible for get the value from selected columns and the rows. I use here now ‘preparedStatement’ this will execute the selected Query like executeQuery as (Select MOD(18,4)). Then i use doGet()Method that will bring the output on the web browser.

To Getting Output on web browser in a tabular form I use ‘HTML’ code and tags.

 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 MySqlMODFunction extends HttpServlet
   {
        public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
          {
              rp.setContentType("text/html");
              PrintWriter disp = rp.getWriter();
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://localhost/dbase";
              String uid = "root";
              String psw = "root";
              Connection con=null;
              PreparedStatement ps = null;
              ResultSet rs;
              try
                {
                    Class.forName(driver);
                    con = DriverManager.getConnection(url,uid,psw);
                    ps=con.prepareStatement("Select MOD(18,4)");
                    rs = ps.executeQuery();
                String title = "Using MOD Function";
                    String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
                    while(rs.next())
                       {
                           String val = rs.getString(1);
                       disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                       }      
             }        
                       catch(Exception e) 
                         {
                           e.printStackTrace();
                         }
                           disp.close();
            }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                 {
                                     doGet(rq,rp);
                                 }
   }
                           
WEB.xml
 
<servlet>
   <servlet-name>MySqlMODFunction</servlet-name>
   <servlet-class>MySqlMODFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMODFunction</servlet-name>
   <url-pattern>/MySqlMODFunction</url-pattern>
</servlet-mapping>

MySql Minute Function in Java Servlet

By Dinesh Thakur

The function Returns the minute from the argument.

I made a table in database named ‘dbase’ with required fields and within the reference of mySql(php myAdmin).Then i import all the required java packages from java library. Then i made class, which extends ‘HttpServlet’ named ‘MySqlMinuteFunc’. I use here serviceMethod() that will use to getting the request from the doGet ()method. Before declaring desired variables, I loaded all the required drivers for database accessing. Then after, i declare variable like ‘connection’ that will make a link between the database and the actual code. Here i declare ‘resultSet’ Also that is been responsible for the fetching value from the columns and rows. I declare ‘preparedStatement’ that will use to executing selected query like executeQuery() as (Select MINUTE(now()) AS mn)).I use doGet()Method that will get Output on the web browser.

While getting the output on the web browser I use ‘HTML’ code and tags that will bring output in tabular form.

 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 MySqlMinuteFunc extends HttpServlet
   {
        public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
           {
                rp.setContentType("text/html");
                PrintWriter disp = rp.getWriter();
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost/dbase";
                String uid = "root";
                String psw = "root";
                Connection con=null;
                PreparedStatement ps = null;
                ResultSet rs;
                try
                  {
                      Class.forName(driver);
                      con = DriverManager.getConnection(url,uid,psw);
                      ps=con.prepareStatement("Select MINUTE(now()) AS mn");
                      rs = ps.executeQuery();
                 String title = "Using Minute Function";
                     String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                 disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                     "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                 "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Result</th>\n"+ "</body> </html>");
                     while(rs.next())
                         {
                             String curr = rs.getString(1);
                         disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                         }      
             }        
                         catch(Exception e) 
                         {
                                e.printStackTrace();
                         }
                                disp.close();
            }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                 {
                                     doGet(rq,rp);
                                 }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMinuteFunc</servlet-name>
   <servlet-class>MySqlMinuteFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMinuteFunc</servlet-name>
   <url-pattern>/MySqlMinuteFunc</url-pattern>
</servlet-mapping>

MySql MIN Function in Java Servlet

By Dinesh Thakur

This function to use when a Minimum value of a selected Field required (columns and rows).

I make a table named ‘worker’ into a database named ‘dbase’ within the reference of mySql(php my Admin). I import all the required java packages from java library. I use to make a class named ‘MySqlMINFunction’, which extends the ‘HttpServlet’. I use here service()Method which will use to get request from the doGet()Method. Then, i loaded all the required drivers for database accessing. Then after, i declare the variables as first i declare ‘connection’ this variable will be responsible for the creating link between the database and the java code. The other variable will be ‘resultSet’ this will does the job to fetching value from the desired column and rows as required. Then other one i declare ‘preparedStatement’ that will be responsible for executing the selected query like executeQuery() as (SELECT MIN(Salary) AS maxsal FROM worker)). After declaring all the variables i use doGet()Method which will use to get output on the web Browser.

I use ‘HTML’ code and tag in to the java code for output that will present the desired output in a tabular form on the web browser.

 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 MySqlMINFunction extends HttpServlet
   {
       public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
          {
              rp.setContentType("text/html");
              PrintWriter disp = rp.getWriter();
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://localhost/dbase";
              String uid = "root";
              String psw = "root";
              Connection con=null;
              PreparedStatement ps = null;
              ResultSet rs;
              try
               {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("SELECT MIN(Salary) AS minsal FROM worker");
                   rs = ps.executeQuery();
               String title = "Using MIN(Minimum) Function";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
               disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
               "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Salary</th>\n"+ "</body> </html>");
                   while(rs.next())
                      {
                           String mn = rs.getString("minsal");
                       disp.println("<tr><td align=\"center\">" + mn +"</td></tr>" );  
                      }     
            }         
                      catch(Exception e) 
                          {
                         e.printStackTrace();
                          }
                         disp.close();
          }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                            doGet(rq,rp);
                           }
   }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMINFunction</servlet-name>
   <servlet-class>MySqlMINFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMINFunction</servlet-name>
   <url-pattern>/MySqlMINFunction</url-pattern>
</servlet-mapping>

MySql MicroSecond Function in Java Servlet

By Dinesh Thakur

This function will Returns the microseconds from argument.

First, we make a table into database named ‘dbase’ within the reference of mySql(php Myadmin). Now To make it happen first we need to import all required java packages from java library. Then to make a class named ‘MySqlMicroSecondFunc’,which is been extends the ‘HttpServlet’. Now we need to declare serviceMethod() which getting the request from doGet() for Output. And after that we need to load all the drivers for mandatory Conditions. After that all the required variable must have to be declared like connection,resultset,preparedStatement. Connection variable use to establish connection between database and java code (frontend and Backend). The variable named resultset interface provides getting methods for retrieving column values from the given rows. The variable. preparedStatement will execute the given query like executeQuery() like as (‘Select MICROSECOND(’15:23:21.24578′) AS ms’). And now we will use the doGet() method to fetch the result on a web Browser.

For getting output in tabular form we have to use ‘HTML’ tags with that the output will looks in a manner way.

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 MySqlMicroSecondFunc extends HttpServlet
 {
    public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
      {
         rp.setContentType("text/html");
         PrintWriter disp = rp.getWriter();
         String driver = "com.mysql.jdbc.Driver";
         String url = "jdbc:mysql://localhost/dbase";
         String uid = "root";
         String psw = "root";
         Connection con=null;
         PreparedStatement ps = null;
         ResultSet rs;
         try
           {
                Class.forName(driver);
                con = DriverManager.getConnection(url,uid,psw);
                ps=con.prepareStatement("Select MICROSECOND('15:23:21.24578') AS ms");
                rs = ps.executeQuery();
             String title = "Using Micro Second Function";
                String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>The Desired Result Is </th>\n"+ "</body> </html>");
               while(rs.next())
                     {
                         String curr = rs.getString(1);
                      disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                     }     
            }         
                      catch(Exception e) 
                        {
                            e.printStackTrace();
                        }
                            disp.close();
    }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                {
                                     doGet(rq,rp);
                                }
 }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMicroSecondFunc</servlet-name>
   <servlet-class>MySqlMicroSecondFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMicroSecondFunc</servlet-name>
   <url-pattern>/MySqlMicroSecondFunc</url-pattern>
</servlet-mapping>

MySql MAX Function in Java Servlet

By Dinesh Thakur

This function to Use when a Maximum value of a selected field required (columns and rows).

I make a table named ‘worker’ into a database named ‘dbase’ within the reference of mySql(php my Admin). I import all the required java packages from java library. I use to make a class named ‘MySqlMAXFunction’, which extends the ‘HttpServlet’. I use here service()Method which will use to get request from the doGet()Method. Then, i loaded all the required drivers for database accessing. Then, after i declare the variables like first i declare ‘connection’ this variable will be responsible for the creating link between the database and the java code. The other variable will be ‘resultSet’ this will does the job to fetching value from the desired column and rows as required. Then other one i declare ‘preparedStatement’ that will be responsible for executing the selected query like executeQuery() as (SELECT MAX(Salary) AS maxsal FROM worker)). After declaring all the variables i use doGet()Method which will use to get output on the web Browser.

I use ‘HTML’ code and tag in to the java code for output that will present the desired output in a tabular form on the web browser.

 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 MySqlMAXFunction extends HttpServlet
 {
     public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
       {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
              {
                  Class.forName(driver);
                  con = DriverManager.getConnection(url,uid,psw);
                  ps=con.prepareStatement("SELECT MAX(Salary) AS maxsal FROM worker");
                  rs = ps.executeQuery();
               String title = "Using MAX(Maximum) Function";
                 String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                 "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Salary</th>\n"+ "</body> </html>");
                 while(rs.next())
                     {
                          String mx = rs.getString("maxsal");
                       disp.println("<tr><td align=\"center\">" + mx +"</td></tr>" );  
                     }     
              }       
                       catch(Exception e) 
                         {
                           e.printStackTrace();
                         }
                           disp.close();
     }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                            doGet(rq,rp);
                           }
  }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMAXFunction</servlet-name>
   <servlet-class>MySqlMAXFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMAXFunction</servlet-name>
   <url-pattern>/MySqlMAXFunction</url-pattern>
</servlet-mapping>

MySql MakeTime Function in Java Servlet

By Dinesh Thakur

This Function is been Used To set a time (Make time) according to Required Condition.

First, we make a table in database named ‘dbase’ with the reference of mySql (php Myadmin). After then we import all required packages from the Java Library. We make a class named ‘MySqlMakeTimeFunc’ extends the ‘HttpServlet’. Then after we use service()Method which is use to getting request form doGet() method for output. Then load all the required Drivers. The Next step to declare the variables like connection (this variable will use to create a link between database and actual code (java code). While linking the database with code we need resultSet variable, which will be the responsible for fetching the value from the desired columns and rows. The other variable will be the preparedStatement which will use to execute the query like executeQuery() as (Select MAKETIME(15,23,21) AS tm)).and after using all the variables we use doGet()Method for show output on a web browser.

To get output on the web browser i use ‘HTML’ code and tags which present it in tabular form.

 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 MySqlMakeTimeFunc extends HttpServlet
 {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
       {
           rp.setContentType("text/html");
           PrintWriter disp = rp.getWriter();
           String driver = "com.mysql.jdbc.Driver";
           String url = "jdbc:mysql://localhost/dbase";
           String uid = "root";
           String psw = "root";
           Connection con=null;
           PreparedStatement ps = null;
           ResultSet rs;
           try
            {
                  Class.forName(driver);
                  con = DriverManager.getConnection(url,uid,psw);
                  ps=con.prepareStatement("Select MAKETIME(15,23,21) AS tm");
                  rs = ps.executeQuery();
               String title = "Using Make Time Function";
                 String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                 "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>The Desired Result Is </th>\n"+ "</body> </html>");
                while(rs.next())
                    {
                         String curr = rs.getString(1);
                      disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                    }     
            }         
                      catch(Exception e) 
                          {
                           e.printStackTrace();
                        }
                           disp.close();
         }
                         public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                               {
                                     doGet(rq,rp);
                               }
  }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMakeTimeFunc</servlet-name>
   <servlet-class>MySqlMakeTimeFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMakeTimeFunc</servlet-name>
   <url-pattern>/MySqlMakeTimeFunc</url-pattern>
</servlet-mapping>

MySql MAKE_SET Function in Java Servlet

By Dinesh Thakur

This Function will return a set of comma-separated strings that have the corresponding bits.

Then i made a database named ‘dbase’ within the reference of mySql(php myAdmin). Here i call or import all the required java packages from java library. That after i made a class named ‘MySqlMAKESETFunction’ extends ‘HttpServlet’. I use here serviceMethod() that is responsible for the getting request from doGet()Method for output on the web browser. Here after, I loaded all the required drivers for database accessing. Then after i need to be declare variables I use first ‘connection’ this variable is responsible for the creating a link between the database and the actual code. The second one i declare ‘resultSet’ this will use to get value from the selected columns and the rows as well. The next one i declare is to ‘preparedStatement’ this will be act as to executing the selected query like executeQuery() as (Select MAKE_SET(‘3′,’X’,’Y’,’Z’) as makeset). In the last phase of coding i use doGet()Method that will be responsible for the output on web browser.

I use here ‘HTML’ code and some of these tags to present output in the tabular form on the web browser for manner way.

 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 MySqlMAKESETFunction extends HttpServlet
 {
     public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
       {
           rp.setContentType("text/html");
           PrintWriter disp = rp.getWriter();
           String driver = "com.mysql.jdbc.Driver";
           String url = "jdbc:mysql://localhost/dbase";
           String uid = "root";
           String psw = "root";
           Connection con=null;
           PreparedStatement ps = null;
           ResultSet rs;
           try
            {
                 Class.forName(driver);
                 con = DriverManager.getConnection(url,uid,psw);
                 ps=con.prepareStatement("Select MAKE_SET('3','X','Y','Z') as makeset");
                 rs = ps.executeQuery();
              String title = "Using MakeSet Function";
                String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
               while(rs.next())
                   {
                       String val = rs.getString(1);
                    disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                   }     
            }         
                    catch(Exception e) 
                       {
                         e.printStackTrace();
                       }
                         disp.close();
     }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                              {
                                     doGet(rq,rp);
                              }
  }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMAKESETFunction</servlet-name>
   <servlet-class>MySqlMAKESETFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMAKESETFunction</servlet-name>
   <url-pattern>/MySqlMAKESETFunction</url-pattern>
</servlet-mapping>

MySql MakeDate Function in Java Servlet

By Dinesh Thakur

This function is Been used to Make the date of Particular day of a year.

First, we make a table in database named ‘dbase’ with the reference of mySql (php Myadmin). After then we import all required packages from the Java Library. We make a class named ‘MySqlMakeDateFunc’ extends the ‘HttpServlet’. Then after we use service()Method which is use to getting request form doGet() method for output. Then load all the required Drivers. The next step to declare the variables like connection(this variable will use to create a link between database and actual code(java code). While linking the database with code we need resultSet variable, which will be the responsible for fetching the value from the desired columns and rows. The other variable will be the preparedStatement which will use to execute the query like executeQuery() as (Select MAKEDATE(2014,233))).and after using all the variables we use doGet()Method for show output on a web browser.

To get Output in a manner form i use ‘Html’ tags, which will appear Output in a tabular form on the web browser.

 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 MySqlMakeDateFunc extends HttpServlet
 {
     public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
       {
          rp.setContentType("text/html");
          PrintWriter disp = rp.getWriter();
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://localhost/dbase";
          String uid = "root";
          String psw = "root";
          Connection con=null;
          PreparedStatement ps = null;
          ResultSet rs;
          try
            {
                  Class.forName(driver);
                  con = DriverManager.getConnection(url,uid,psw);
                  ps=con.prepareStatement("Select MAKEDATE(2014,233) AS fd");
                  rs = ps.executeQuery();
               String title = "Using Make Date Function";
                 String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
               disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                  "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>The Desired Result Is </th>\n"+ "</body> </html>");
                 while(rs.next())
                     {
                           String curr = rs.getString(1);
                         disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
                     }     
            }         
                        catch(Exception e) 
                         {
                                e.printStackTrace();
                         }
                                disp.close();
        }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                             doGet(rq,rp);
                           }
  }
WEB.xml
 
<servlet>
   <servlet-name>MySqlMakeDateFunc</servlet-name>
   <servlet-class>MySqlMakeDateFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlMakeDateFunc</servlet-name>
   <url-pattern>/MySqlMakeDateFunc</url-pattern>
</servlet-mapping>

MySql LowerCase Function in Java Servlet

By Dinesh Thakur

This Function Will Show the lower case String.

First i made a database named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all the required java packages from java library as program essentials. I made a class named ‘MySqlLowerCaseFunction’, which extends ‘HttpServlet’. Then i use service()Method which will use to get the request from the doGet()Method for output on the browser. I loaded all the required drivers for accessing database as required. Then i use to declare the variable as i use ‘connection’ this will use to create a link between the database and the java code. Then i declare ‘resultSet’ this will responsible for get the value from selected columns and the rows. I use here now ‘preparedStatement’ this will execute the selected Query like executeQuery as (Select LCASE(‘MySql’)). Then I use doGet()Method that will bring the output on the web browser.

To get output on the web browser in a tabular form i use ‘HTML’ code and tag, which bring output in a manner way.

 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 MySqlLowerCaseFunction extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
      {
          rp.setContentType("text/html");
          PrintWriter disp = rp.getWriter();
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://localhost/dbase";
          String uid = "root";
          String psw = "root";
          Connection con=null;
          PreparedStatement ps = null;
          ResultSet rs;
          try
           {
                Class.forName(driver);
                con = DriverManager.getConnection(url,uid,psw);
                ps=con.prepareStatement("Select LCASE('MySql') as lwrcase");
                rs = ps.executeQuery();
             String title = "Using LowerCase Function";
               String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
                while(rs.next())
                     {
                          String val = rs.getString(1);
                       disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                     }     
           }          
                       catch(Exception e) 
                         {
                           e.printStackTrace();
                         }
                           disp.close();
      }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                             doGet(rq,rp);
                           }
  }
WEB.xml
 
<servlet>
   <servlet-name>MySqlLowerCaseFunction</servlet-name>
   <servlet-class>MySqlLowerCaseFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlLowerCaseFunction</servlet-name>
   <url-pattern>/MySqlLowerCaseFunction</url-pattern>
</servlet-mapping>

MySql LOG Function in Java Servlet

By Dinesh Thakur

This Function Will Returns the natural logarithm of the passed numeric expression.

I use to made a database first named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all important java packages from java library. Then i made a class named ‘MySqlLOGFunction’ extends ‘HttpServlet’. Then i use service()Method this will be responsible for the getting the request from the doGet()Method for output. Then i loaded all the required drivers for the database accessing. Then i use to set variables like I use first variable ‘connection’ that will be responsible for the creating a link between database and the java code. Then i use ‘resultSet’ this will use to get value from the selected columns and the rows from table. I use here ‘preparedStatement’ that use to executing the selected query like executeQuery() as (Select LOG(512))). Then in last phase i use doGet()Method that will provide the output on the web browser.

I use ‘HTML’ code and tags to bring output on the web browser in tabular form.

 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 MySqlLOGFunction extends HttpServlet
 {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
              {
                     Class.forName(driver);
                     con = DriverManager.getConnection(url,uid,psw);
                     ps=con.prepareStatement("Select LOG(5)");
                     rs = ps.executeQuery();
                  String title = "Using LOG(LOGRITHM) Function";
                    String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                 disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                  "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
                    while(rs.next())
                      {
                           String val = rs.getString(1);
                         disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                      }     
             }        
                         catch(Exception e) 
                              {
                                   e.printStackTrace();
                              }
                                   disp.close();
      }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                             doGet(rq,rp);
                           }
  }
 
WEB.xml
 
<servlet>
   <servlet-name>MySqlLOGFunction</servlet-name>
   <servlet-class>MySqlLOGFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlLOGFunction</servlet-name>
   <url-pattern>/MySqlLOGFunction</url-pattern>
</servlet-mapping>

MySql LOG10 Function in Java Servlet

By Dinesh Thakur

This Function Will Returns the base-10 logarithm of the passed numeric expression.

I use to made a database first named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all important java packages from java library. Then i made a class named ‘ MySqlLOG10Function’ extends ‘HttpServlet’. Then i use service ()Method this will be responsible for the getting the request from the doGet()Method for output. Then i loaded all the required drivers for the database accessing. Then i use to set variables like i use first variable ‘connection’ that will be responsible for the creating a link between database and the java code. Then i use ‘resultSet’ this will use to get value from the selected columns and the rows from table. I use here ‘preparedStatement’ that use to executing the selected query like executeQuery() as (Select LOG10(142)). Then in last phase i use doGet()Method that will provide the output on the web browser.

To feel look good the result we use a tabular form concept. For this kind of requirement, we use ‘HTML’ coding and some of tags to use Output in Designer look to show on a web browser.

 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 MySqlLOG10Function extends HttpServlet
  {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
              {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("Select LOG10(128)");
                   rs = ps.executeQuery();
                String title = "Using LOG10(LOGRITHM) Function";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                 "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
                   while(rs.next())
                       {
                             String val = rs.getString(1);
                          disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                       }     
             }        
                          catch(Exception e) 
                              {
                                  e.printStackTrace();
                              }
                                  disp.close();
      }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                             doGet(rq,rp);
                           }
  }
WEB.xml
 
<servlet>
   <servlet-name>MySqlLOG10Function</servlet-name>
   <servlet-class>MySqlLOG10Function</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlLOG10Function</servlet-name>
   <url-pattern>/MySqlLOG10Function</url-pattern>
</servlet-mapping>

MySql LOG2 Function in Java Servlet

By Dinesh Thakur

This Function Will Returns the base-2 logarithm of the passed numeric expression.

I use to made a database first named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all important java packages from java library. Then i made a class named ‘ MySqlLOG2Function’ extends ‘HttpServlet’. Then i use service()Method this will be responsible for the getting the request from the doGet()Method for output. Then i loaded all the required drivers for the database accessing. Then i use to set variables like i use first variable ‘connection’ that will be responsible for the creating a link between database and the java code. Then i use ‘resultSet’ this will use to get value from the selected columns and the rows from table. I use here ‘preparedStatement’ that use to executing the selected query like executeQuery() as (Select LOG2(512)). Then in last phase i use doGet()Method that will provide the output on the web browser.

To feel look good the result we use a tabular form concept. For this kind of requirement, we use ‘HTML’ coding and some of tags to use Output in Designer look to show on a web browser.

 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 MySqlLOG2Function extends HttpServlet
 {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
            rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
              {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                   ps=con.prepareStatement("Select LOG2(512)");
                   rs = ps.executeQuery();
                String title = "Using LOG2(LOGRITHM) Function";
                  String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                   "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
                  while(rs.next())
                      {
                          String val = rs.getString(1);
                       disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                      }     
              }       
                        catch(Exception e) 
                        {
                            e.printStackTrace();
                        }
                            disp.close();
      }
                         public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                              {
                               doGet(rq,rp);
                              }
  }
WEB.xml
 
<servlet>
   <servlet-name>MySqlLOG2Function</servlet-name>
   <servlet-class>MySqlLOG2Function</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlLOG2Function</servlet-name>
   <url-pattern>/MySqlLOG2Function</url-pattern>
</servlet-mapping>

MySql LOCATE Function in Java Servlet

By Dinesh Thakur

This Function Will Return the position of the first occurrence of substring LOWER () Return.

Firstly, i made a table in database named ‘dbase’ with required fields and values in it. I import all the required java packages from the java library. I made a class named ‘MySqlLOCATEFunction’, which extends the ‘HttpServlet’. After, then i use serviceMethod() which will bring the request from doGet()method for output. Then after, i loaded all required drivers for the database accessing declare some mandatory variable for database connecting like i declare ‘Connection’ (this variable will use to create a link between database and the java code or actual code). The next will be ‘resultSet’ the variable will be responsible for the getting values from required columns and rows. The other one variable will be ‘preparedStatement’ this variable will use to execute the Selected Query like executeQuery() as (Select LOCATE(‘Sql’,’MySqlSql’)).I use doGet()Method which will bring the output on web browser.

To make output efficient we use ‘HTML’ Code or some of its tag which will bring the output in tabular form or in a manner way on the web Browser.

 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 MySqlLOCATEFunction extends HttpServlet
 {
    public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
      {
          rp.setContentType("text/html");
          PrintWriter disp = rp.getWriter();
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://localhost/dbase";
          String uid = "root";
          String psw = "root";
          Connection con=null;
          PreparedStatement ps = null;
          ResultSet rs;
          try
           {
                Class.forName(driver);
                con = DriverManager.getConnection(url,uid,psw);
                ps=con.prepareStatement("Select LOCATE('Sql','MySqlSql') as lct");
                rs = ps.executeQuery();
             String title = "Using LOCATE Function";
               String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
                while(rs.next())
                    {
                         String val = rs.getString(1);
                      disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
                    }     
           }          
                      catch(Exception e) 
                         {
                           e.printStackTrace();
                         }
                           disp.close();
      }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                            doGet(rq,rp);
                           }
 }
WEB.xml
 
<servlet>
   <servlet-name>MySqlLOCATEFunction</servlet-name>
   <servlet-class>MySqlLOCATEFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlLOCATEFunction</servlet-name>
   <url-pattern>/MySqlLOCATEFunction</url-pattern>
</servlet-mapping>
« Previous Page
Next Page »

Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW