Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
How to add a column in a table 
 

In this section, you will learn how to add column in a table.

 

How to add a column in a table

                         

Consider a situation where the requirement of the client gets changed and you have asked to modify the structure of the table. In reality it is the work of the database administrator but as a Java programmer you should know how you can modify the structure of the table. The problem is that we have to add a new column to our database by using the java program. There is no need to get panic. What we simply need is to use a query for adding a new column in the database table. 

To get the desired result firstly we need to make a connection with our database. After connection has been established pass the query in the prepareStatement() for adding new column in the database. This method will return the PreparedStatement object. By the object of the PreparedStatement we will call the executeUpdate() which will tell the status of  the table.

The code of the example is given below:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletAddingNewColumn extends HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse responsethrows 
                          
ServletException, IOException{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost/zulfiqar";
    Connection connection;
    try{
      Class.forName("org.gjt.mm.mysql.Driver");
      connection = DriverManager.getConnection(connectionURL, "root""admin");
      PreparedStatement pst = connection.prepareStatement
                              (
"alter table emp_details add column sal int(5)");
      int i = pst.executeUpdate();
      if (i==1){
        pw.println("Column has been added");
      }
      else{
        pw.println("No column has been added");
      }
    }
    catch(Exception e){
      pw.println("The exception is " + e);
    }
  }
}

web.xml file for this program:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
 <servlet>
 <servlet-name>Zulfiqar</servlet-name>
 <servlet-class>ServletAddingNewColumn</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Zulfiqar</servlet-name>
 <url-pattern>/ServletAddingNewColumn</url-pattern>
 </servlet-mapping>
</web-app>

The output of the program is given below:

Download this example

                         

» View all related tutorials
Related Tags: c ide orm table data form io make type column field name columns change this id nsis war tab row

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

how to add in databased using how to create table and how to insert the data in table pls give me examples

Posted by sudhakar on Tuesday, 09.16.08 @ 09:38am | #79780

i want to fetch a data from a data base written
in text area or in a menu or list using mysql and i am using servlets.

Posted by rajeshng on Thursday, 12.6.07 @ 02:58am | #41400

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.