In the first example we have seen how to execute the statement in JDBC. We will now discuss the CRUD(Create, Retrieve, Update and Delete) functionality with JDBC.


The below example with tell how to implement CRUD operations using Statement interface.


 


package com.visualbuilder;


import java.sql.Connection;


import java.sql.DriverManager;


import java.sql.Statement;


 


public class StatementExample {


 


    public static void main(String[] args) {


        try {


 


            /** Loading the ODBC- Bridge driver*/


                Class.forName("com.mysql.jdbc.Driver");


 


            /** Getting Connection*/


                                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");


 


            /** Creating Statement*/


                                Statement stmt = con.createStatement();


 


            /** Creating simple table*/


                                stmt.executeUpdate("Create Table visualbuilder(id int(4) NOT NULL,name varchar(45))");


                                System.out.println("Table Created");


 


            /** inserting Rows */


                                int i=stmt.executeUpdate(" insert into visualbuilder(id,name)values(1,'Sun java')");


                                    System.out.println(i + " Recored(s) inserted");


 


            /** Updating */


                                i= stmt.executeUpdate(" update visualbuilder set name='visual builder'");


                                System.out.println(i + " Recored(s) Updated");


 


            /** Deleting */


                                i= stmt.executeUpdate(" delete from visualbuilder");


                                System.out.println(i + " Recored(s) Deleted");


 


            /** Closing the Connection*/


                                con.close();


            } catch (Exception e) {


                             e.printStackTrace();


            }


        }


}


 


Output:-


Table Created


1 Recored(s) inserted


1 Recored(s) Updated


1 Recored(s) Deleted

                    

Copyright © 2010 VisualBuilder. All rights reserved