Friday 4 July 2014

Java update data

Here is the program to update data in the database


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javafetch;

/**
 *
 * @author jawadmahmood
 */
import java.sql.*;
public class JavaFetch {

    /**
     * @param args the command line arguments
     */
   
    public static void main(String[] args) {
        // TODO code application logic here
        try{
           
            Connection con;
            Statement st;
            ResultSet rs;
           
           
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:8889/jrecords",
      "root","root");
           
            st = con.createStatement();
           
            String addVar = args[0];
            String nameVar = args[1];
           
            String sql = "UPDATE post " +   "set content = '" +addVar+"' " +
                    "where title = '"+nameVar+"'";
            int num = st.executeUpdate(sql);
            System.out.println(num + " records updated " + sql);
            con.close();
           
        }catch( Exception ex){
            System.out.println("The error is "+ ex);
       
        }
    }
   
}


////////

here is the image of the code 

































When run the file you will have to put the two parameters 
one for content and one for  title for where condition 

and after excution of the program the value will be change in the database 



No comments:

Post a Comment