Advance Java

Create JDBC program which contains following functions: 
 a. Connect 
 b. Create Table 
 c. Insert Records into respective table 
 d. Update records of particular table of database 
 e. Display records 
 f. Delete Records from table


package databasecrud;
import java.sql.*;
import java.util.*;
 
public class DatabaseCRUD {
 
    public static void main(String[] args) {
       
        databaseConnection();
        createTable();
        insertRecord();
        updateRecord();
        displayRecord();
        deleteRecord();
    }
 
    // create connection with database
    static void databaseConnection() {
        try {
 
            String dbConn = "jdbc:mysql://localhost:3306/college";
            String username = "root";
            String pwd = "";
 
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
 
            if (conn != null) {
                System.out.println("Connected");
            }
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    // create studentDetail table in database
    static void createTable() {
        String dbConn = "jdbc:mysql://localhost:3306/college";
        String username = "root";
        String pwd = "";
        String query = "CREATE TABLE IF NOT EXISTS studentDetail ("
                + "id INTEGER AUTO_INCREMENT PRIMARY KEY, "
                + "rollno VARCHAR(12) NOT NULL, "
                + "name VARCHAR(50) NOT NULL,"
                + "branch VARCHAR(20) NOT NULL"
                + ")";
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
            if(conn != null) {
                Statement st = conn.createStatement();
                st.execute(query);
                st.close();
            }
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    // insert record into studentDetailDetail table
    static void insertRecord() {
        String dbConn = "jdbc:mysql://localhost:3306/college";
        String username = "root";
        String pwd = "";
        String query = "INSERT INTO studentDetail"
                + "(rollno, name, branch)"
                + "values(101, 'Ram', 'ME')";
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
            if(conn != null) {
                Statement st = conn.createStatement();
                int n = st.executeUpdate(query);
               
                System.out.println(n + " rows inserted.");
                st.close();
            }
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
   
    // upadate record in studentDetail table
    static void updateRecord() {
        String dbConn = "jdbc:mysql://localhost:3306/college";
        String username = "root";
        String pwd = "";
        String query = "UPDATE studentDetail SET branch = 'CE' WHERE rollno = 101";
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
            if(conn != null) {
                Statement st = conn.createStatement();
                int n = st.executeUpdate(query);
               
                System.out.println(n + " rows updated.");
                st.close();
            }
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
   
    // display studentDetail detail from studentDetail table
    static void displayRecord() {
        String dbConn = "jdbc:mysql://localhost:3306/college";
        String username = "root";
        String pwd = "";
        String query = "SELECT * FROM studentDetail";
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
           
            if(conn != null) {
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(query);
               
                while(rs.next()) {
                    System.out.print(rs.getString(2) + " ");
                    System.out.print(rs.getString(3) + " ");
                    System.out.println(rs.getString(4));
                }
                rs.close();
                st.close();
            }
            conn.close();
           
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
   
    // delete record from studentDetail table
    static void deleteRecord() {
        String dbConn = "jdbc:mysql://localhost:3306/college";
        String username = "root";
        String pwd = "";
        String query = "DELETE FROM studentDetail WHERE rollno = 101";
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
           
            if(conn != null) {
                Statement st = conn.createStatement();
                int n = st.executeUpdate(query);
               
                System.out.println(n + " rows deleted.");
                st.close();
            }
            conn.close();
           
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

Post a Comment

0 Comments