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.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Delete row and column from table through java code

Posted on: July 29, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Delete row and column from table through java code   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Shakeel
August 15, 2011
Nice

Effort is great.
sharom
March 15, 2013
lol

maklongko la...den minx cam ne nk delete je pon...yg ko bagi den coding panjang ni ni buat pe dol....

Ads

 

Ads