How to add a column into the table?
>> Wednesday, June 8, 2011
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.
ServletAddingNewColumn class
<web-app>
<servlet>
<servlet-name>bhaskar</servlet-name>
<servlet-class>ServletAddingNewColumn</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bhaskar</servlet-name>
<url-pattern>/ServletAddingNewColumn</url-pattern>
</servlet-mapping>
</web-app>
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.
ServletAddingNewColumn class
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletAddingNewColumn extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response)throws 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
<web-app>
<servlet>
<servlet-name>bhaskar</servlet-name>
<servlet-class>ServletAddingNewColumn</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bhaskar</servlet-name>
<url-pattern>/ServletAddingNewColumn</url-pattern>
</servlet-mapping>
</web-app>
0 comments:
Post a Comment