Mysql UNIQUE key using servlet

Mysql UNIQUE key using servlet


Posted in : Java Posted on : July 18, 2011 at 7:12 PM Comments : [ 0 ]

In this section we will discuss how an unique key is used in java servlet.

Mysql UNIQUE key using servlet

In this section we will discuss how an unique key is used in java servlet.

The UNIQUE constraint identifies the each record unambiguously i.e duplicity is not allowed where ever the unique key is applied. Primary key is also an unique key but there is a basic difference between the primary key and unique key is that primary key does not support null value because it has implicit NOT NULL constraint whereas unique key supports the null value even multiple null values. To use the unique key we will have first create a table in database so I first created a table in mysql. Now I have to use it in the java servlet so, I created a class named UniqueServlet which extends the class HttpServlet. Inside the class I overridden the doGet() method into which HttpServletRequest and HttpServletResponse objects are created. In the body of doGet() method I set the response type into which the server will show the output. Then in the further line I used the getWriter() method of HttpServletResponse that inherit this method from ServletResponse interface which returns an object of PrintWriter class which helps in to show the output on browser. Now since we have to use a SQL query so I established a connection between java and mysql using classes and interfaces of java.sql package. When the connection is established I passed the query "Alter table vegetable add unique (Weight)" into the parameter of prepareStatement() method of Connection interface which returns an object of PreparedStatement interface. Now I used the the executeQuery() method of PreparedStatement which returns an object of ResultSet which stores the result of executed query. Finally I have extracted the result from ResultSet object.

Example :

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 UniqueServlet 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 = "Alter table vegetable add unique (Weight)";
Connection con;
PreparedStatement ps;
ResultSet rs;
try
{
Class.forName(driver);
con = DriverManager.getConnection(url,uid,psw);
ps = con.prepareStatement(sql);
int i = ps.executeUpdate();
pw.println(i+" row(s) are affected");
}
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>UniqueServlet</servlet-name>
<servlet-class>UniqueServlet</servlet-class>
</servlet>

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

</web-app>

Output :

Table of which on to the column I have applied to add an unique key

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