Mysql Remove UNIQUE key using servlet

Mysql Remove UNIQUE key using servlet


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

In this section we will discuss how can we remove the unique key in database using java servlet.

Mysql Remove UNIQUE key using servlet

In this section we will discuss how can we remove the unique key in database using java servlet.

The UNIQUE constraint identifies the each record unambiguously i.e duplicity is not allowed where ever the unique key is applied. After removing an unique constraint where ever it has/had applied the duplicity will be allowed again i.e. we can use the duplicate value there where we did restrict for using of duplicate values. Since I have to use it in the java servlet so, at first I created a table in mysql and applied the unique key on column 'weight' and then made a servlet named RemoveUniqueServlet in java 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 with HttpServletResponse object that inherits 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 drop index Weight" into the parameter of prepareStatement() method of Connection interface which returns an object of PreparedStatement interface. Now I used the the executeUpdate() method of PreparedStatement interface which returns an integer value that how many rows are affected after applying this query.

Example :

RemoveUniqueServlet.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 RemoveUniqueServlet 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 drop index 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>RemoveUniqueServlet</servlet-name>
<servlet-class>RemoveUniqueServlet</servlet-class>
</servlet>

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

</web-app>

Output :

A table 'vegetable' into which the column 'weight' is specified as an unique

In the description of table 'vegetable' the 'UNI' will be shown in the column key of the field 'weight'.

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

When an unique constraint will be removed from the above table then you can use the duplicate values as :

After executing the above example once if you are trying it again without making the column unique you will get the error as :

When an unique key is removed from the column weight then in the description of table 'vegetable' it will show blank in the column key of the field 'weight'.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics