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 */
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
Posted by vinay | 5:42 AM