๐Ÿ’ป
B.TECH โ€ข SKILL DEVELOPMENT COURSE

Node JS / React JS / Django Lab

Web Development Laboratory โ€ข Experiment 4

๐Ÿ’ป Node JS โš›๏ธ React JS ๐ŸŒ Web Development ๐Ÿงช Lab Program
Experiment 04

Explore the features of ES6 like arrow functions, callbacks, promises, async/await. Implement an application for reading the weather information from openweathermap.org and display the information in the form of a graph on the web page.

โ† All Lab Programs
EXP-04

I will provide you with the MySQL code for creating the student table and make some changes to your Java code to improve it:

☛  MySQL Code:


sql> CREATE TABLE IF NOT EXISTS student (
s_id INT PRIMARY KEY,
s_nameVARCHAR(255),
s_addressVARCHAR(255)
);

This SQL code creates a table with three columns: s_id for student ID (primary key), s_name for student name, and s_address for student address.

Java Code

InsertData.java


import java.sql.*;

import java.util.Scanner;

public class InsertData {
    public static void main(String[] args) {
        try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
             Statements = con.createStatement()) {

            Scanner sc = new Scanner(System.in);
            System.out.println("Inserting Data into student table:");
            System.out.println("________________________________________");

            System.out.print("Enter student id: ");
            int sid = sc.nextInt();
            System.out.print("Enter student name: ");
            String sname = sc.next();
            System.out.print("Enter student address: ");
            String saddr = sc.next();

            String insertQuery = "INSERT INTO student VALUES(" + sid + ",'" + sname + "','" + saddr + "')";
            s.executeUpdate(insertQuery);

            System.out.println("Data inserted successfully into student table");

        } catch (SQLException err) {
            System.out.println("ERROR: " + err);
        }
    }
}

Output :

	Inserting Data into student table:
	________________________________________
	Enter student id: 101
	Enter student name: John Doe
	Enter student address: 123 Main Street
	Data inserted successfully into student table

UpdateData.java


import java.sql.*;

import java.util.Scanner;

public class UpdateData {
    public static void main(String[] args) {
        try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
             Statements = con.createStatement()) {

            Scanner sc = new Scanner(System.in);
            System.out.println("Update Data in student table:");
            System.out.println("________________________________________");

            System.out.print("Enter student id: ");
            int sid = sc.nextInt();
            System.out.print("Enter student name: ");
            String sname = sc.next();
            System.out.print("Enter student address: ");
            String saddr = sc.next();

            String updateQuery = "UPDATE student SET s_name='" + sname + "', s_address='" + saddr + "' WHERE s_id=" + sid;
            s.executeUpdate(updateQuery);

            System.out.println("Data updated successfully");

        } catch (SQLException err) {
            System.out.println("ERROR: " + err);
        }
    }
}

Output :

	Update Data in student table:
	________________________________________
	Enter student id: 101
	Enter student name: Jane Doe
	Enter student address: 456 Broad Street
	Data updated successfully

DeleteData.java


import java.sql.*;

import java.util.Scanner;

public class DeleteData {
    public static void main(String[] args) {
        try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
             Statements = con.createStatement()) {

            Scanner sc = new Scanner(System.in);
            System.out.println("Delete Data from student table:");
            System.out.println("________________________________________");

            System.out.print("Enter student id: ");
            int sid = sc.nextInt();

            String deleteQuery = "DELETE FROM student WHERE s_id=" + sid;
            s.executeUpdate(deleteQuery);

            System.out.println("Data deleted successfully");

        } catch (SQLException err) {
            System.out.println("ERROR: " + err);
        }
    }
}

Output :

	Delete Data from student table:
	______________________________________
	Enter student id: 101
	Data deleted successfully

DisplayData.java


import java.sql.*;


public class DisplayData {
    public static void main(String[] args) {
        try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
             Statement s = con.createStatement()) {

            ResultSet rs = s.executeQuery("SELECT * FROM student");
            if (rs != null) {
                System.out.println("SID \t STU_NAME \t ADDRESS");
                System.out.println("________________________________________");

                while (rs.next()) {
                    System.out.println(rs.getString("s_id") + " \t " + rs.getString("s_name") + " \t " + rs.getString("s_address"));
                    System.out.println("________________________________________");
                }
            }

        } catch (SQLException err) {
            System.out.println("ERROR: " + err);
        }
    }
}

Output :

	SID 	 STU_NAME 	 ADDRESS
	________________________________________
	102 	 Alice Smith 	 789 Oak Avenue
	________________________________________
	103 	 Bob Johnson 	 567 Pine Road
	________________________________________
โ† Back to Lab Programs ๐Ÿ“š All Lab Manuals
```