Ad Code

Students Information Application Using JDBC & Java | StudentInfo Application.

 StudentInfo Application

Welcome to CodingFizz,

Java is a widely most popular programming language. It's a general and object-oriented programming language. Java is a simple and easy programming language.

JDBC stands for Java Database Connectivity. it is used for connecting java programs to the database (MySql, Oracle, SQL, etc.).


In this tutorial, We will make a small application using java and JDBC. The name of the application is the Student Infomation. In this program, we will store the student's details in the database and how to fetch data from the database.

Operations:

  • Insert: Insert student details into the database (name, address, id, phone).
  • Delete: delete student details from the database.
  • Select: Fetch all details from the database.
This code is for MySql Database. Even you can make it for other databases. It is a basic program.

Firstly You need to install MySql and mysql.connector.jar file.

Then, create a database and table into your database.



And connect the .jar file with your IDE.

Source Code:


 package App;

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.Scanner;

 //connect to data base
 class connect {
    static Connection con;

    public static Connection createC() {
        try {
            // load the driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // connet to the database
            String user = "root";
            String password = "bca12345";
            String url = "jdbc:mysql://localhost:3306/students";

            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
 }



 // data store into the database
 class db{
    public static boolean insertintodb(Input in) {
        boolean f = false;
        try {
            Connection con = connect.createC();
            String quary = "insert into studetail(sname, scity, sphone) values(?,?,?)";
            PreparedStatement pstmt = con.prepareStatement(quary);
            pstmt.setString(1, in.getstudentname());
            pstmt.setString(2, in.getstudentcity());
            pstmt.setString(3, in.getstudentphone());
            pstmt.executeUpdate();
            f = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }
    public static boolean deletefromdb(int i){
        boolean f = false;
        try {
            Connection con = connect.createC();
            String quary = "delete from studetail where sid=?";
            PreparedStatement pstmt = con.prepareStatement(quary);
            pstmt.setInt(1, i);
            pstmt.executeUpdate();
            f = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }
    public static void showall(){
        try {
            Connection con = connect.createC();
            String quary = "select * from studetail;";
            java.sql.Statement stmt=con.createStatement();
            ResultSet set= stmt.executeQuery(quary);
            while(set.next()){

                int id=((ResultSet) set).getInt("sid");
                String name=((ResultSet) set).getString("sname");
                String city=((ResultSet) set).getString("scity");
                String phone=((ResultSet) set).getString("sphone");

                System.out.println("Student ID: "+id);
                System.out.println("Student Name: "+name);
                System.out.println("Student City: "+city);
                System.out.println("Student Phone: "+phone);
                System.out.println("\n################################\n");
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 }



 // Input from the user 
 class Input {
    private int studentid;
    private String studentname;
    private String studentcity;
    private String studentphone;

    public void setstudentname(String studentname) {
        this.studentname = studentname;
    }

    public String getstudentname() {
        return studentname;
    }

    public void setstudentcity(String studentcity) {
        this.studentcity = studentcity;
    }

    public String getstudentcity() {
        return studentcity;
    }

    public void setstudentid(int studentid) {
        this.studentid = studentid;
    }

    public int getstudentid() {
        return studentid;
    }

    public void setstudentphone(String studentphone) {
        this.studentphone = studentphone;
    }

    public String getstudentphone() {
        return studentphone;
    }

    public Input(int studentid, String studentname, String studentcity, String studentphone) {
        super();
        this.studentid = studentid;
        this.studentname = studentname;
        this.studentcity = studentcity;
        this.studentphone = studentphone;
    }

    public Input(String studentname, String studentcity, String studentphone) {
        super();
        this.studentname = studentname;
        this.studentcity = studentcity;
        this.studentphone = studentphone;
    }

    public Input() {
        super();
    }

    public String toString() {
        return "Student Details = [Student Id = " + studentid + ", Student Name = " + studentname + ", Student Phone = "
                + studentphone + ", Student City = " + studentcity + "]";

    }
 }



 // Main Application
 public class stuapp {
    public static void main(String[] args) {
        System.out.println("........Welcome........");
        try (Scanner s = new Scanner(System.in)) {
            while (true) {
                System.out.println("Press 1 to Add student");
                System.out.println("Press 2 to Delete student");
                System.out.println("Press 3 to Show all students");
                System.out.println("Press 4 to Exit student");
                int c = s.nextInt();

                if (c == 1) {

                    System.out.print("Add a student!!");
                    String a = s.nextLine();
                    System.out.println("\nEnter your name: ");
                    String name = s.nextLine();
                    System.out.println("Enter your Phone No.: ");
                    String phone = s.nextLine();
                    System.out.println("Enter your city: ");
                    String city = s.nextLine();

                    Input in = new Input(name, city, phone);
                    boolean ans = db.insertintodb(in);
                    if (ans) {
                        System.out.println("Added");
                    } else {
                        System.out.println("Not Added");
                    }
                    System.out.println(in);

                } else if (c == 2) {
                    System.out.println("Enter The student ID: ");
                    int i =s.nextInt();
                    db.deletefromdb(i);
                    boolean ans = db.deletefromdb(i);
                    if (ans) {
                        System.out.println("Deleted");
                    } else {
                        System.out.println("Not Deleted");
                    }
            
                } else if (c == 3) {
                    db.showall();
                } else if (c == 4) {
                    break;
                } else {

                }
            }
            System.out.println("Thankyou!");
        }
    }
 }

Output:



Share with your friends and follow us on Instagram, facebook and twitter.

Post a Comment

0 Comments

Ad Code