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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Delete row and column from table through java code 
 

In this section we will see how to delete row and column from given table through java code.

 

Delete row and column from table through java code

                         

In this section we will see how to delete row and column from given table through java code. Java code create connection between program and database by using connection interface and jdbc driver.

In this example given below we have a database named 'student' and a table 'stu_info' in same database. Structure of table 'stu_info' is as following.....

 

 

create table stu_info (
         ID int not null auto_increment,
         Name  varchar(20),
         Address varchar(20),
         Phone int(15),
         primary key(ID)
);
Table: 
+----+----------+------------+---------+
| ID | Name     | Phone      | Address |
+----+----------+------------+---------+
| 11 | Mahendra | 9990254913 | Delhi   |
| 12 | Mahendra | 9990254913 | Delhi   |
| 13 | Mahendra | 9990254913 | Delhi   |
+----+----------+------------+---------+

In this table we will delete the row having minimum value of ID then delete the column named 'Address'. Before running this java code you need to paste a .jar file named mysql connector.jar in the jdk1.6.0_01\lib

import java.sql.*; 
import java.io.*;
class ConnectToDatabase {
      public static void main(String[] args) throws SQLException {
            // declare a connection by using Connection interface 
            Connection connection = null;
            /* Create string of connection url within specified 
            format with machine name, port number and database 
            name. Here machine name id localhost and database 
            name is student. */
            String connectionURL = 
            "jdbc:mysql://localhost:3306/student";
            /*declare a resultSet that works as a table resulted by 
            execute a specified 
		sql query. */
            ResultSet rs = null;
            // Declare statement.
            Statement statement = null;
            try {
                // Load JDBC driver "com.mysql.jdbc.Driver". 
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                /* Create a connection by using getConnection() 
                method that takes parameters of string type 
                connection url, user name and password to connect
                to database. */
                connection = DriverManager.getConnection(connectionURL,
                "root", "root");
                /* createStatement() is used for create statement 
                object that is used for sending sql statements to
                the specified database. */
                statement = connection.createStatement();
                /* executeQuery() method execute specified sql query.
                Here this query checks number of rows present in table */ 
                rs = statement.executeQuery("select* from stu_info");
		int count = 0;
		while(rs.next()) {
			count++;
                }
		System.out.println("Number of Rows present 
                in table : "+count);
		/* Here sql query find the element from the table 
                havinf minimum ID */
		rs = statement.executeQuery("select min(ID) from 
                stu_info");
		rs.next();
				
		// This sql query delete the row having minimum ID.
                statement.executeUpdate("delete from stu_info where 
                ID='"+rs.getInt(1)+"'");	
		System.out.println("Row is deleted successfully.");
                /* Here this query checks number of rows present in 
                table after deletion */ 
		int count1 = 0;
                rs = statement.executeQuery("select* from stu_info");
		while(rs.next()) {
			count1++;
                }
		System.out.println("after deletion number of Rows 
                present in table : "+count1);
		// Here this query shows column names present in table. 
                rs = statement.executeQuery("show COLUMNS from stu_info");
                System.out.println("Columns in table 'stu_info' of 
                database 'student' : ");
                while(rs.next()) {
		System.out.println(rs.getString("Field"));
                }
                // This sql query delete the column name of specified name.
		statement.executeUpdate("ALTER TABLE stu_info DROP 
                Address");
		System.out.println("column 'Address' is deleted 
                successfully.");
                // Again this query shows column names present in table. 
		rs = statement.executeQuery("show COLUMNS from stu_info");
                System.out.println("after deletion columns in table '
                stu_info' of database 'student' : ");
                while(rs.next()) {
			System.out.println(rs.getString("Field"));
                }
	   }
	   // catch exceptions if found any exception at run time.
	   catch(Exception ex){
              System.out.println("Sorry ! found some problems with database.");
	      System.out.println("Error is : "+ ex);
          }
           finally {
                // close all the connections.
                rs.close();
                statement.close();
                connection.close();
          }
      }
}

Save this java code with class name and .java extension then compile from command prompt with javac command.

Download Source Code

                         

» View all related tutorials
Related Tags: sql mysql c ant jdbc help tutorial ria this for to drive e des in sta m nt wan j

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 
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.