This section contains the detail about the Deleting row and column of a database table in java.
Deleting row and column of a table
In this program ,we delete row and column of a table. First we create connection to a database using connection interface and java driver. After it we can delete row using "delete " keyword and also can delete column using "Alter" table commands SQL.
For this example, we are using a database named as "cellular" of mysql--
Table : cellular(Before deletion)

In this table, we will delete id having minimum value and we will also delete column "name".
importÃ? java.sql.*;Ã?Â
class� colerase� {
� � � � � � public� static� void� main(String[]� args)� throws� SQLException� {
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? //Ã? declareÃ? aÃ? connectionÃ? byÃ? usingÃ? ConnectionÃ? interfaceÃ?Â
� � � � � � � � � � � � Connection� connection� =� null;
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã?Â
� � � � � � � � � � � � String� connectionURL� =� "jdbc:mysql://192.168.10.13:3306/ankdb";
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? //declareÃ? aÃ? resultSetÃ?Â
� � � � � � � � � � � � ResultSet� rs� =� null;
� � � � � � � � � � � � //� Declare� statement.
� � � � � � � � � � � � Statement� statement� =� null;
� � � � � � � � � � � � try� {
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? //Ã? LoadÃ? JDBCÃ? driverÃ? "com.mysql.jdbc.Driver".Ã?Â
� � � � � � � � � � � � � � � � Class.forName("com.mysql.jdbc.Driver").newInstance();
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã?Â
� � � � � � � � � � � � � � � � connection� =� DriverManager.getConnection(connectionURL,
� � � � � � � � � � � � � � � � "root",� "root");
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã?Â
� � � � � � � � � � � � � � � � statement� =� connection.createStatement();
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã?Â
� � � � /*� sql� query� find� the� element� from� the� table� havinf� minimum� ID� */
� � � � rs� =� statement.executeQuery("select� min(id)� from� cellular");
� � � � rs.next();
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã?Â
� � � � //� This� sql� query� delete� the� row� having� minimum� ID.
Ã? statement.executeUpdate("deleteÃ? fromÃ? cellularÃ? whereÃ? id='"+rs.getInt(1)+"'");Ã? Ã?Â
� System.out.println("Row� is� deleted� successfully.");
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã?Â
� � � � � � � � � � � � � � � � //� This� sql� query� delete� the� column� name� of� specified� name.
� statement.executeUpdate("ALTER� TABLE� cellular� DROP� name");
� System.out.println("column� 'Address'� is� deleted� successfully.");
Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã? Ã?Â
� � � � � }
� � � � � //� 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();
� � � � � � � � � � }
� � � � � � }
}
Table : cellular(after deletion of minimum "id" value and column "name")


[ 0 ] Comments