Ad

DATABASE CONNECTIVITY(With all Queries)

                       
                               DATABASE CONNECTIVITY 

QUERIES:

1 ==> INSERT QUERY:  "INSERT INTO tableName(id,name)" + "VALUES(?,?)" 
2 ==> DELETE QUERY: "DELETE FROM tableName WHERE id=1"
3 ==> UPDATE QUERY: "UPDATE tableName SET id='2' , name= 'hashmi' WHERE id=1 "
4 ==> PRINT ALL DATA: "SELECT * FROM tableName"

USES OF QUERIES IN PROGRAM:


import java.sql.*;
import java.util.*;
/**
 *
 * @author usman
 */

public class SameAssignment {

    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
          Employee em = new Employee(026,"hashmi",true,"Permanenet");
         // Employee em1 = new Employee(146,"ali  ",true,"Permanenet");
         // Employee em2 = new Employee(040,"usamn",true,"Permanenet");

     
          Scanner input = new Scanner(System.in);
      
              mainmenu(); 
              
               //calling functions
         System.out.print("Enter your choice: ");

            switch (input.nextInt()) {
              
                   case 1:
                       addEmployee(em);
                       //addEmployee(em1);
                      // addEmployee(em2);
                       break;
                   case 2:
                       updateEmployee();
                     break;
                   case 3:
                       deleteEmployee();
                       break;
                   case 4:
                       serchEmplpoyee(em);
                       break;
                   case 5:
                       printEmployee();
                       break;
                   case 0:
                       System.exit(0);
                   default:
                       throw new AssertionError();
               }
   
    }
    //insert new data
      static void addEmployee(Employee employee) throws ClassNotFoundException{
            try{

       Class.forName("com.mysql.jdbc.Driver");
      Connection  con = DriverManager.getConnection("jdbc:mysql://localhost/employee","hashmi" ,"470350");}
            catch (Exception ex) {
                System.out.println("usman" +ex);
          }
            try{
                int id = employee.getId();
                String name = employee.getName();
                boolean gender = employee.isGender();
                String type = employee.getType();
             Statement st = con.createStatement();
           String SQL =  "INSERT INTO employee(id, name, gender,type) " +
             "VALUES( id , name,  gender , type )" ;

            int num = st.executeUpdate(SQL);
                System.out.println("Updated");
       }
            catch (SQLException ex) {
                System.out.println("w:" +ex.getMessage());
          }
      }
       //Search  data
       static void searchEmplpoyee(Employee employee) throws ClassNotFoundException{
             try{

       Class.forName("com.mysql.jdbc.Driver");
     Connection  con = DriverManager.getConnection("jdbc:mysql://localhost/employee","hashmi" ,"470350");
        Statement  st = con.createStatement();
          ResultSet  rs = st.executeQuery("SELECT * FROM employee WHERE id = 1");

       
                System.out.print(rs.getInt("id"));
                System.out.print(rs.getString("name"));
                System.out.print(rs.getString("gender"));
                System.out.print(rs.getBoolean("type"));
             
            
       }
             catch (SQLException ex) {
                 System.out.println("Exception:" +ex.getMessage());
           }
       }
       
       //print Employee
       static void printEmployee() throws ClassNotFoundException{  
             try{

       Class.forName("com.mysql.jdbc.Driver");
      Connection  con = DriverManager.getConnection("jdbc:mysql://localhost/employee","hashmi" ,"470350");
          Statement st = con.createStatement();
           ResultSet rs = st.executeQuery("SELECT * FROM employee");

            while (rs.next()) {
                System.out.print(rs.getInt("id"));
                System.out.print(rs.getString("name"));
                System.out.print(rs.getString("gender"));
                System.out.print(rs.getString("type"));
       }
             }
             catch (SQLException ex) {
                 System.out.println("Exception:" +ex.getMessage()); 
           }
       }
       //update employee
        static void updateEmployee() throws ClassNotFoundException {
          try{

       Class.forName("com.mysql.jdbc.Driver");
      Connection  con =  DriverManager.getConnection("jdbc:mysql://localhost/Employee","hashmi" ,"470350");
         Statement  st = con.createStatement();
            int num = st.executeUpdate("UPDATE employee SET id= '12', name='Usman', gender=true ,type= 'contract' WHERE id=1");
            System.out.println(" Rows updated. ");
       }
          catch (SQLException ex) {
               System.out.println("Exception:" +ex.getMessage()); 
            }
        }
       //delete Employee
          static void deleteEmployee() throws ClassNotFoundException{
             try{
  
       Class.forName("com.mysql.jdbc.Driver");
      Connection  con = DriverManager.getConnection("jdbc:mysql://localhost/Employee","hashmi" ,"470350");
           Statement st = con.createStatement();
            int num = st.executeUpdate("DELETE FROM Employee WHERE id=1");
            System.out.println( " Delete record. ");
       }
             catch (SQLException ex) {
                 System.out.println("Exception:" +ex.getMessage());  
              }
          }
          
          //Menu
           public static void mainmenu() {
  
        System.out.println("Enter 1:========= Add Employee=========");
        System.out.println("Enter 2:=========Update Employee======");
        System.out.println("Enter 3:=========Delete Employee========");
        System.out.println("Enter 4:=========Search Employee========");
        System.out.println("Enter 7:=========All Employee==========");
        System.out.println("Enter 0:==========Exist===========");
        
       
    }
     
    }


    

Post a Comment

0 Comments