Mysql LCASE function using Servlet

Mysql LCASE function using Servlet


Posted in : Java Posted on : July 19, 2011 at 5:36 PM Comments : [ 0 ]

In this section we will discuss about the lcase() function of mysql using java servlet.

Mysql LCASE function using Servlet

In this section we will discuss about the lcase() function of mysql using java servlet.

Let's first I want to give a brief description of LCASE function of mysql, this function is used for converting all the upper case characters of a string into lower case character. This function is used with the SELECT query. Since I have to use this mysql function in my java program so at first I created a table 'employee' in database into which I keep some records as Id, Name, Department, Salary, etc. Then I made a class in java named UcaseServlet 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 load the driver then created an object of Connection interface. Using this object I passed the query "SELECT Name, LCASE(Name) FROM employee" 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 :

LcaseServlet.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 LcaseServlet 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 sql2 = "SELECT Name, LCASE(Name) FROM employee";
Connection con;
PreparedStatement ps = null;
ResultSet rs;
try
{
Class.forName(driver);
con = DriverManager.getConnection(url,uid,psw);

ps = con.prepareStatement(sql2);
rs = ps.executeQuery(sql2);
pw.println("<b>Applying LCASE function</b><br>");
pw.println("<table><tr><td><b>Name_Before</b></td><td><b>Name_After</b></td></tr>");
while(rs.next()){
String name = rs.getString(1);
String name1 = rs.getString(2);
pw.println("<tr><td>");
pw.println(name);
pw.println("</td>");
pw.println("<td>");
pw.println(name1);
pw.println("</td></tr>");
}
pw.println("</table>");
}
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>LcaseServlet</servlet-name>
<servlet-class>LcaseServlet</servlet-class>
</servlet>

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

</web-app>

Output :

Table that I had created earlier :

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

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics