JDBC: Prepared Statement


Prepared Statement Demo using Java/MySQL
------------------------------------------------
package com.vicwalks.web.util;


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


import com.mysql.jdbc.Connection;


public class PreparedStatementDemo {
 public static void main(String[] args) {
 System.out.println("===========Prepared statement demo===========\n");
 Connection con = null;
 PreparedStatement preparedStatement;
 try{
   Class.forName("com.mysql.jdbc.Driver");
   con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASENAME","userName","password");
   try{
      String sql = "SELECT * FROM employee WHERE salary >= ?";
      preparedStatement = con.prepareStatement(sql);
      preparedStatement.setInt(1,10000);
      ResultSet rs1 = preparedStatement.executeQuery();
      System.out.println("List of employee where salary >= 10000");
      System.out.println("empId" + "\t- " + "empName"+ "\t- " +"salary");
      System.out.println("=============================================\n");
      while (rs1.next()){
        String empId = rs1.getString(1);
        String empName = rs1.getString(2);
        int salary = rs1.getInt(3);
        System.out.println(empId + "\t- " + empName+ "\t- " +salary);
      }
 
      System.out.println("=============================================\n");
      
      preparedStatement.setInt(1,15000);
      ResultSet result2 = preparedStatement.executeQuery();
      System.out.println("List of employee where salary >= 15000");
      System.out.println("empId" + "\t- " + "empName"+ "\t- " +"salary");
      System.out.println("=============================================\n");
      while (result2.next()){
        String empId = result2.getString(1);
        String empName = result2.getString(2);
        int salary = result2.getInt(3);
        System.out.println(empId + "\t- " + empName+ "\t- " +salary);
      }
 
      System.out.println("=============================================\n");
 
 
     }
     catch (SQLException s){
          System.out.println("SQL statement is not executed!");
     }
  }
  catch (Exception e){
       e.printStackTrace();
  }
}
 }


where userName: MySql user name, for example : root
          Password: MySql password


Output

 ===========Prepared statement demo===========


List of employee where salary >= 10000
empId - empName - salary
=============================================


001 - Anuj Kumar - 12000
003 - Amit Kumar - 11000
005 - Ajay Bisht - 18000
=============================================


List of employee where salary >= 15000
empId - empName - salary
=============================================


005 - Ajay Bisht - 18000
=============================================

Comments

Popular posts from this blog

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

Read Images from a xlsx file using Apache POI

Upload csv file at Google Cloud Storage using java