In this section we will discuss about the reverse() function of mysql using java servlet.
Mysql REVERSE function using Servlet
In this section we will discuss about the reverse() function of mysql using java servlet.
Let's first I want to give a brief description of REVERSE function of mysql, this function reversed the all characters of a string from its original given string. The string which we have to reverse is to be passed into its parameter. This function is used with the SELECT query. Since I have to use this mysql function in my java program so I created a java class named MysqlReverseFunctionServlet which extends the class HttpServlet. Inside the class I overridden the doGet() method into which created an objects of HttpServletRequest and HttpServletResponse interfaces. In the body of the doGet() method set the mime type of response that is into which format the browser will show the output. In next line used the getWriter() method of ServletResponse with the object of HttpServletResponse (inherits this method from ServletResponse) which returns an object of PrintWriter class that helps in to show the output on browser. Now since I have to use a SQL query therefore I will have to make a connection with the database system, so I connected the mysql database system. First I loaded the mysql driver then created an object of Connection interface. Using this object I passed the query "SELECT REVERSE('DEVMANUALS.COM');" into the prepareStatement() method of Connection interface which gives an object of PreparedStatement interface. Using the object of PreparedStatement I used the executeQuery() method of PreparedStatement which returns an object of ResultSet interface that holds the result of the executed query. From which I will extract the result.
Example :
MysqlReverseFunctionServlet.java
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 MysqlReverseFunctionServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://192.168.10.13/data"; String uid = "root"; String psw = "root"; String sql = "SELECT REVERSE('DEVMANUALS.COM');"; Connection con; PreparedStatement ps; ResultSet rs; try { Class.forName(driver); con = DriverManager.getConnection(url,uid,psw); ps = con.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()) { String rev = rs.getString(1); pw.println(rev); } } catch(SQLException sx) { pw.println(sx); } catch(ClassNotFoundException cx) { pw.println(cx); } } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>join</display-name> <servlet> <servlet-name> MysqlReverseFunctionServlet</servlet-name> <servlet-class>MysqlReverseFunctionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MysqlReverseFunctionServlet</servlet-name> <url-pattern>/MysqlReverseFunctionServlet</url-pattern> </servlet-mapping> </web-app>
Output :
When you will execute the above example you will get the output as :
[ 0 ] Comments