J2EE Programming

Hands On No. 4 : CRUD Operations using JDBC

For making connection with MySQL Database, download the Database driver by Clicking here. On this Page Select Latest Product Version and from Operating System, Select Platform Independent option. After making above selection, It will show two options. Among them select, Platform Independent (Architecture Independent), ZIP Archive option. Now open Netbeans IDE and in your project Window, Right Click on Libraries and Select Add JAR/Folder option. Specify the path of above mentioned downloaded file.
Source Code of Database
CREATE TABLE `emp` (
  `eno` bigint(20) UNSIGNED NOT NULL,
  `ename` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `emp` (`eno`, `ename`) VALUES (1, 'Dev'), (2, 'Darshee'), (3, 'Krishna'), (4, 'Bhavya');
Source Code for Displaying Data
package class1;

import java.sql.*;

/**
 *
 * @author Dhaval Mehta
 */
public class mysample1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try 
        {
            
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        } 
        catch (Exception ex) 
        {
            System.out.print("Driver Load Problem : "+ ex.getMessage());
        
        }
        try 
        {
        conn =
         DriverManager.getConnection("jdbc:mysql://localhost/dm?" +
                                   "user=dm&password=dm12");

    

   
        } 
        catch (SQLException ex) 
        {
        // handle any errors
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
        }
        
        try
        {
             stmt = conn.createStatement();
             rs = stmt.executeQuery("SELECT * FROM emp");
             while(rs.next())
             {
                 System.out.print(""+rs.getInt("eno"));
                 System.out.print("-"+rs.getString("ename"));
                 
             }
             
        }
        catch(Exception e)
        {
            System.out.print("Problem  : "+ e.getMessage());
        }
    }
    
}

                                
Source Code for Reading Data
package class1;

import java.sql.*;
import java.util.*;

/**
 *
 * @author Dhaval Mehta
 */
public class mysampleinsert {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try 
        {
            
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        } 
        catch (Exception ex) 
        {
            System.out.print("Driver Load Problem : "+ ex.getMessage());
        
        }
        try 
        {
        conn =
         DriverManager.getConnection("jdbc:mysql://localhost/dm?" +
                                   "user=dm&password=dm12");

    

   
        } 
        catch (SQLException ex) 
        {
        // handle any errors
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
        }
        
       try
       {
           Scanner sc= new Scanner(System.in); 
           System.out.print("Enter Your name ");  
           String ename= sc.next();
           stmt = conn.createStatement();
           stmt.executeUpdate("INSERT INTO `emp` (`eno`, `ename`) VALUES (NULL, '"+ ename +"');");
           System.out.print(ename + " is added successfully");
            
       }
       catch(Exception e)
       {
           System.out.println("Error : " + e.getMessage());
       }
    }
    
}