Tuesday, September 10, 2013

JDBC : 05 - JDBC PreparedStatement Update, Delete Example


JDBC : 05 - JDBC PreparedStatement Update, Delete Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class PreparedStatementUpdateDeleteExample {

       Connection connection = null;
       /**
       * @param args
       */
      public static void main(String[] args) {

             PreparedStatementUpdateDeleteExample example = new       PreparedStatementUpdateDeleteExample();
             try {
                   example.execute() ;
             } catch (SQLException e) {
                   e.printStackTrace();
             }
      }

      private void execute() throws SQLException {

             boolean execute = false;
             try{
                    connection = createConnection();
                    if(null != connection)
                    //Update a record
                    execute = doUpdate(false, connection); if(execute) System.out.println("Record Updated");
                    //Delete a record
                    execute = doDelete(false); if(execute) System.out.println("Record Deleted");
                    //Select records
                    doSelect();
             } catch (Exception e) {
                    e.printStackTrace();
             } finally {
                    if(null != connection)
                             connection.close();
             }
      }

      private Connection createConnection(){

              String username = "root";
              String password = "res13pg";
              try {
                    Class.forName("com.mysql.jdbc.Driver");
                    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306 /myworkspace",username,password);
              } catch (ClassNotFoundException e) {
                     e.printStackTrace();
              } catch (SQLException e) {
                     e.printStackTrace();
              }
              return connection;
       }

       private boolean doUpdate(boolean execute, Connection connection){

              String updateSQL = "UPDATE DBUSER SET USERNAME = ? WHERE USER_ID = ? ";
              PreparedStatement prstatement = null;
              try {
                     prstatement = connection.prepareStatement(updateSQL);
                     prstatement.setString(1, "Dhahamdi");
                     prstatement.setInt(2, 104); 

                     int i = prstatement.executeUpdate(); System.out.println("i -->" + i);
                     execute = true;
              } catch (SQLException e) {
                     e.printStackTrace();
              }
              return execute;
       }

       private boolean doDelete(boolean execute){

               String deleteSQL = "DELETE FROM DBUSER WHERE USER_ID = ? ";
               PreparedStatement prstatement = null;
               try {
                     prstatement = connection.prepareStatement(deleteSQL);
                     prstatement.setInt(1, 104); 

                     int i = prstatement.executeUpdate(); System.out.println("i -->" + i);
                     execute = true;
               } catch (SQLException e) {
                     e.printStackTrace();
               }
               return execute;
       }

       private void doSelect(){

               String selectSQL = "SELECT USER_ID, USERNAME from DBUSER";
               PreparedStatement prstatement = null;
               ResultSet rs = null;

               try {
                     prstatement = connection.prepareStatement(selectSQL);
                     rs = prstatement.executeQuery(selectSQL);
                     while (rs.next()) {
                             String usrid = rs.getString("USER_ID");
                             String usrname = rs.getString("USERNAME");

                             System.out.println("userid : " + usrid);
                             System.out.println("username : " + usrname);
                     }
               } catch (SQLException e) {
                      e.printStackTrace();
               }
        }
}


Output :

i -->1
Record Updated
i -->1
Record Deleted
userid : 101
username : Sanjeeva
userid : 102
username : Sandamali
userid : 103
username : Thinuli