Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JDBC
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Delete a Specific Row from a Database Table

                         

Consider a case where we are creating a table and we have to add some data in it. It may happen that we might add a wrong data in a row, now we need to delete that wrong data. This can be done very easily, and in this section we are going to do the same that is, how to delete a specific row from a specific database table. Here is an example with code which provides the facility for deleting specific row in a database table. Sometimes, you want to delete a specific row then you use some java methods and APIs interfaces. Detail information given below:

Description of program:

Firstly, in this program we are going to establish the connection by using MySQL database. After establishing the connection we are going to delete a specific row from the table. If the row get deletes successfully then it will shows an appropriate message: "Row is deleted.". If the row doesn't gets deleted then displays a message: "Row is not deleted". If there arises any problem while establishing the connection or there doesn't exist the table by that name or there is no data in the table then the exception will be thrown that "SQL statement is not executed". If any problems occurs while making connection then system displays a message with the help of java.lang.ClassNotFoundException exception.

Description of code:

DELETE FROM employee6 WHERE Emp_code = '1':
Above code deletes the record in the employee6 table which has Emp_code = '1'.
        employee6: This is a table name of a specific database.
        Emp_code: It specifies the column name of a specific database table.
        '1': This is the value of specified column name.

Here is the code of program:

import java.sql.*;

public class DeleteSpecificRow{
  public static void main(String[] args) {
    System.out.println("An example for Deleting a Row from a Database!");
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String driver = "com.mysql.jdbc.Driver";
    String db = "jdbctutorial";
    try{
      Class.forName(driver);
      con = DriverManager.getConnection(url+db, "root""root");
      try{
        Statement st = con.createStatement();
        String sql = "DELETE FROM employee6 WHERE Emp_code = '1'";
        int delete = st.executeUpdate(sql);
        if(delete == 1){
          System.out.println("Row is deleted.");
        }
        else{
          System.out.println("Row is not deleted.");
        }
      }
      catch (SQLException s){
        System.out.println("SQL statement is not executed!");
      }
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

Download this example.

                         

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

Current Comments

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

above example didnt solve my prob either..
bcoz it already deleted a record whn i load da page.
right now, i want to delete a record whn i click a button or checkbox near a list of data i showed in the main page..
so tell me,how on earth i can get it done?

Posted by nurule aiman on Wednesday, 03.26.08 @ 23:26pm | #54429

nice example

Posted by siva on Tuesday, 03.25.08 @ 10:22am | #54228

hye, i wanna ask how i want to delete a specific record frm a database.
hear dis,
i have an order table.
how i want to delete a record in the table when i click a button or checkbox besides the table interface in main page.

Posted by nurul aiman on Tuesday, 03.25.08 @ 01:28am | #54204

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.