« Home | Insert values into table Using Java » | Program to Retrive values from Mysql Using Java » | Java Mysql Connectivity » | Program to Delete Files in a Given Directory » | Program to Write to a file » | Program to Rename Files in a given Directory » 

Thursday, March 09, 2006 

Delete and Update values in MySql Using Java

/* A Sample program to Delete and Update values from and to a table Using Java*/

import java.sql.*;
import java.util.*;

public class DeleteUpdate
{
public static void main(String args[]) {
try {
/* Test loading driver */

Hashtable members = new Hashtable();

// Load the JDBC driver
Class.forName("com.mysql.jdbc.Driver");

// Establish a connection
String url = "jdbc:mysql://localhost/test"; //here "test" is the name of the database in use
Connection connection = DriverManager.getConnection( url, "root", "kris" ); //root and password is name


/* Create a statement*/
Statement statement = connection.createStatement();


/*process to insert values into database test where phani is the tablename*/


String Delete = "delete from phani where name like '" + "Vemuri" + "'";
int j = statement.executeUpdate(Delete);

System.out.println("--------Values Deleted from table Successfully----------"+j);

/*process to Update values in your database test where phani is the tablename */

String Update = "update phani set name = '" + "aaaaaaa" +"' where ID = '" + "1000" +"'";
int updateCount = statement.executeUpdate(Update);

System.out.println("--------Values Updated Successfully----------"+ updateCount);



}
catch( Exception x ) {
x.printStackTrace();
}
}
}

/* Prerequisites you need to have Mysql installed in your system with a data base named "test"
and with username "root" and password "name"*/
/* Javac DeleteUpdate.java */
/* Java DeleteUpdate */

hindtyres block m 84 connaught circus new delhi 110001 01123415661 01123418731 9868823494 9868074517

Post a Comment

About me

  • I'm phanindra
  • From Hyderabad, Andhra Pradesh, India
  • An Alumini of JNTU Hyd into happening IT industry...
My profile

Visitors
Bloggeries Blog Directory BlogRankings.com singapore blog directory blog search directory
Google
 
Web conceptoftheday.blospot.com