by Dinesh Thakur Category: Servlet with MySql Database

The query 'Reverse' will reverse all the selected content or String(Column).

 

In the first Instance we made a table named 'worker' into the database named 'dbase' in the reference of MySql(php MyAdmin).Then We have to import all The Required Packages From java Library. now to make a class named 'JavaServletREVERSE' extends 'HttpServlet'. use the service Method() which will bring the Request from doGet() for Output generating. Load the mandatory Drivers. now to declare some mandatory variables like Resultset,Connection,preparedStatement the objects of these variable will be used to access them all like connection variable will be used to make connection between Databse and actual code(Java code).the variable named result set will represents the output resulted from a given query. Now to preparedStatement which will help to execute query like executeQuery() like as (Select name,REVERSE(name) as UC from worker). and in the last step the doGet() method will be declared to fetch the Output on Web Browser.

Now on web browser to present the output in efficient look we need to use 'HTML' code and some of it's tag to present Output in a tabular form. which make Output in a designer preference to show on a web browser. 

EXAMPLE

JavaServletREVERSE
 
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 JavaServletREVERSE 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,REVERSE(name) as UC from worker");
                 rs = ps.executeQuery();
                          String title = "Employee's Name in Reverse Order Case";
                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=\"20%\" border=\"1\" align=\"center\">\n" + "</body> </html>");
                while(rs.next())
                 {
                    String nm = rs.getString("UC");
                             disp.println("<tr><td align=\"center\">"+ nm +    "</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>JavaServletREVERSE </servlet-name>
   <servlet-class>JavaServletREVERSE </servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletREVERSE</servlet-name>
   <url-pattern>/JavaServletREVERSE </url-pattern>
</servlet-mapping>




About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles