Student management Menu-Driven Java Program with JDBC, MYSQL.
Ranjit Panda

Student management Menu-Driven Java Program with JDBC, MYSQL.

It is a Menu based java program.

In this program, we will be operating CRUD in Our DB.

I will be using MYSQL

JDBC to connect with DB


Let's Know Little Bit About JDBC.

Java Database Connectivity is an API for the language java. We got different drivers for different database and those drivers /jar file contains methods and classes and interfaces implemented by respective Database vendors. So We have to use the specific jar file for a specific database. I will be using the MYSQL database so I will be using the MYSQL connector jar file, which will help the java program to connect to the MYSQL database.

Assuming we all know the steps to connect to DB, let's discuss here,

  • Load Driver
  • The creation of connection
  • The defining statements accordingly
  • Nowadays we don't require to close the connection JAVA(Garbage Collector) will do that for us, How Beautiful java is.

Without further due let's jump right into code.

I will be sharing the code snippets here describing the purpose of the java file.

First.java(starting file):

In this file, we will have our main(), and it will have all the code to perform operations(function call, getting inputs from the user, etc)


import java.io.*


import com.ranjit.panda.Student;
import com.ranjit.panda.StudentDao;


public class Start {


	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Hello User Please Enter Your Name: \n");
		String user = br.readLine();
		System.out.println("Welcome To Student Management App." + user);
		
		
		
		while(true) {
			System.out.println("PRESS 1 to ADD student");
			System.out.println("PRESS 2 to DELETE student");
			System.out.println("PRESS 3 to DISPLAY student");
			System.out.println("PRESS 4 to UPDATE student");
			System.out.println("PRESS 5 to EXIT App");
			
			int c = Integer.parseInt(br.readLine());
			
			if(c == 1) {
				//Add student
				
				System.out.println("Enter User Name: ");
				String name = br.readLine();
				System.out.println("Enter User Phone: ");
				String phone = br.readLine();
				System.out.println("Enter User City: ");
				String city = br.readLine();
				
				//create student object
				Student st = new Student(name,phone,city);
				boolean ans = StudentDao.insertStudentRecordToDB(st);
				if(ans) {
					System.out.println("Student record Inserted Successfully...");
					System.out.println("Student Record:" + st);
				}else {
					System.out.println("Some error Occured While Inserting...Please try Again!");
				}
				
			}
			else if(c == 2) {
				//Delete student
				System.out.println("Enter Student ID To Delete: ");
				int userID = Integer.parseInt(br.readLine());
				boolean f = StudentDao.deleteStudentRecordFromDB(userID);
				if(f) {
					System.out.println("Student Of ID " + userID + " Record Deleted... ");
				}else {
					System.out.println("Something Went Wrong.. Please try Again!");
				}
			}
			else if(c == 3) {
				//Display student
				StudentDao.showAllStudentRecords();
			}
			else if(c == 4) {
				//Update student
				System.out.println("PRESS 1 to UPDATE name");
				System.out.println("PRESS 2 to UPDATE phone");
				System.out.println("PRESS 3 to UPDATE city");
				int val = Integer.parseInt(br.readLine());
				if(val == 1) {
					//Update Name
					System.out.println("Enter name to UPDATE...");
					String name = br.readLine();
					System.out.println("Enter ID to identify student!");
					int id = Integer.parseInt(br.readLine());
					Student st = new Student();
					st.setStudentName(name);
					boolean f = StudentDao.updateStudentRecord(val,name,id,st);
					if(f) {
						System.out.println("Student Name Updated Successfully...");
					}else {
						System.out.println("Something Went Wrong Please try Again!");
					}
				}
				else if(val == 2) {
					//Update Phone
					System.out.println("Enter phone to UPDATE...");
					String phone = br.readLine();
					System.out.println("Enter ID to identify student!");
					int id = Integer.parseInt(br.readLine());
					Student st = new Student();
					st.setStudentPhone(phone);
					boolean f = StudentDao.updateStudentRecord(val,phone,id,st);
					if(f) {
						System.out.println("Student Phone Updated Successfully...");
					}else {
						System.out.println("Something Went Wrong Please try Again!");
					}
				}
				else if(val == 3) {
					//Update city
					System.out.println("Enter city to UPDATE...");
					String city = br.readLine();
					System.out.println("Enter ID to identify student!");
					int id = Integer.parseInt(br.readLine());
					Student st = new Student();
					st.setStudentCity(city);
					boolean f = StudentDao.updateStudentRecord(val,city,id,st);
					if(f) {
						System.out.println("Student City Updated Successfully...");
					}else {
						System.out.println("Something Went Wrong Please try Again!");
					}
				}
				else {
					System.out.println("Hey You have not updated Anything... Please choose option Correctly!");
				}
				
			}
			else if(c == 5) {
				//Exit
				
				System.out.println("Thank You For Using Application...If You Enjoyed, Please Experience It Again!" + user);
				break;
			}
			else {
				
			}
			
		}
	}
	


}

;        

Student.java:

This file will have data members like student properties and constructors respectively. getters and setters for the private properties(encapsulation).


package com.ranjit.panda


public class Student {
	
	private int studentID;
	private String studentName;
	private String studentPhone;
	private String studentCity;
	
	
	
	
	public int getStudentID() {
		return studentID;
	}




	public void setStudentID(int studentID) {
		this.studentID = studentID;
	}




	public String getStudentName() {
		return studentName;
	}




	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}




	public String getStudentPhone() {
		return studentPhone;
	}




	public void setStudentPhone(String studentPhone) {
		this.studentPhone = studentPhone;
	}




	public String getStudentCity() {
		return studentCity;
	}




	public void setStudentCity(String studentCity) {
		this.studentCity = studentCity;
	}




	public Student(int studentID, String studentName, String studentPhone, String studentCity) {
		super();
		this.studentID = studentID;
		this.studentName = studentName;
		this.studentPhone = studentPhone;
		this.studentCity = studentCity;
	}




	public Student(String studentName, String studentPhone, String studentCity) {
		super();
		this.studentName = studentName;
		this.studentPhone = studentPhone;
		this.studentCity = studentCity;
	}




	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}




	@Override
	public String toString() {
		return "Student [studentID=" + studentID + ", studentName=" + studentName + ", studentPhone=" + studentPhone
				+ ", studentCity=" + studentCity + "]";
	}
	
	
	
	
}

;        

CP.java:

This file will have all the code to connect to DB. And we will return that connection object at last.


package com.ranjit.panda
import java.sql.*;
public class CP {
	static Connection con;
	public static Connection createc() {
		
		try {
			
			//load the driver
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			//create the Connection
			String user = "*****"; //user of mysql
			String password = "*****"; //password of mysql
			String url = "jdbc:mysql://localhost:3306/student_manage";
			con = DriverManager.getConnection(url,user,password);
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		return con;
		
	}
}

;        

StudentDao.java:

This file will define all the functions will implement all the stuff i.e. the codes to operate on DB.ALL the DB-specific operate functions will be available here. And to perform operation i.e. all the operation stuff implemented inside Data Access Object class.


package com.ranjit.panda
import java.sql.*;


public class StudentDao {
	public static boolean insertStudentRecordToDB(Student st) {
		boolean f = false;
		try {
			Connection con = CP.createc();
			String query = "insert into students(sname,sphone,scity) values(?,?,?)";
			PreparedStatement pstmt = con.prepareStatement(query);
			//set the value of parameters
			pstmt.setString(1, st.getStudentName());
			pstmt.setString(2, st.getStudentPhone());
			pstmt.setString(3, st.getStudentCity());
			
			//execute..
			pstmt.executeUpdate();
			
			f = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return f;
	}


	public static boolean deleteStudentRecordFromDB(int userID) {
		// TODO Auto-generated method stub
		boolean f = false;
		try {
			Connection con = CP.createc();
			String query = "delete from students where sid=?";
			PreparedStatement pstmt = con.prepareStatement(query);
			//set the value of parameters
			pstmt.setInt(1, userID);
			
			//execute..
			pstmt.executeUpdate();
			
			f = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return f;
	}


	public static void showAllStudentRecords() {
		// TODO Auto-generated method stub
		boolean f = false;
		try {
			Connection con = CP.createc();
			String query = "select * from students";
			Statement st = con.createStatement();
			ResultSet set = st.executeQuery(query);
			
			while(set.next()) {
				int id = set.getInt(1);
				String name = set.getString(2);
				String phone = set.getString(3);
				String city = set.getString("scity");
				
				System.out.println("Student ID: " + id + " \n Student Name: " + name + " \nStudent Phone: " + phone + " \nStudent city: " + city);
				System.out.println("----------------------------------------------");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}


	public static boolean updateStudentRecord(int val,String toUpdate,int id,Student st) {
		// TODO Auto-generated method stub
		boolean f = false;
		
		try {
			? Connection con = CP.createc();
			 if(val == 1) {
					//Update Name
					
						String query = "update students set sname=? where sid=?";
						PreparedStatement pstmt = con.prepareStatement(query);
						pstmt.setString(1, toUpdate);
						pstmt.setInt(2, id);
						
						//execute..
						pstmt.executeUpdate();
						f = true;
				}
				else if(val == 2) {
					//Update Phone
					String query = "update students set sphone=? where sid=?";
					PreparedStatement pstmt = con.prepareStatement(query);
					pstmt.setString(1, toUpdate);
					pstmt.setInt(2, id);
					
					//execute..
					pstmt.executeUpdate();
					f = true;
				}
				else if(val == 3) {
					//Update City
					String query = "update students set scity=? where sid=?";
					PreparedStatement pstmt = con.prepareStatement(query);
					pstmt.setString(1, toUpdate);
					pstmt.setInt(2, id);
					
					//execute..
					pstmt.executeUpdate();
					f = true;
				}
				else {
					
				}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return f;
	}
}

;        

Database Stuff:(creating a database, table)

No alt text provided for this image

Eclipse IDE env stuff:

No alt text provided for this image

let's check out the video:(Result)

Thank You

MERN APPLICATION

Portfolio




Chidrup Vamaraju

Associate Software Engineer at WeSquare IT Solutions

1 年

please share the git link

回复
Atul Suradkar

Java Developer||Automation Testing ||UI Testing||

2 年

bro can u plz provide that zip file?

回复
Tejas Bhadane

Admin Executive at Udyogwardhini

2 年

Can you provide project zip file?

回复

要查看或添加评论,请登录

RANJIT PANDA的更多文章

社区洞察

其他会员也浏览了