Java JDBC Queries

full-width Java JDBC
Java JDBC,technical hassan ali, technicalhassanali
Database Connection Query Code

When you Established the Connection to java jdbc you remember some thing.
1.First Build a path and Added the Driver mySql Connector mysql-connector-java-8.0.25.jar ,Otherwise Connection not Established.
2.Second Enter Correct Database Name, Database Root username, Database Password.
3.If Your Face Problem of SSL Connection then use the String DB_URL = "jdbc:mysql://localhost:3306/crud?"+"autoReconnect=true&useSSL=true"; folowing query rather then Using String DB_URL = "jdbc:mysql://localhost:3306/crud";.For Further Detail about SSL Click the Icon :

Connection Query

  • try{} The try statement lets you test a block of code for errors.
  • catch(Exception e) The catch statement lets you handle the error, and Exception is the Handle these error amd store the object name like e.
import java.sql.*;
public class ali{
public static void main(String args[]){
	try{
		Class.forName("com.mysql.cj.jdbc.Driver");
		String DB_URL = "jdbc:mysql://localhost:3306/crud";
		String DB_USER = "root";
		String DB_PASSWORD = "*******************";
		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);

		if (con!= null) { //check connection
            System.out.println("Connection is Established");
        }
	}catch(Exception e){
			System.out.println(e);
			}
  }
}

Show Data

Statement – Used to execute string-based SQL queries, and we can't pass the Parameter in Statement.
PreparedStatement – Used to execute parameterized SQL queries, like Insert or Update
ResultSet – The term "result set" refers to the row and column data contained in a ResultSet object.It is used to store the data which are returned from the database table after the execution of the SQL statements in the Java Program.


Commonly used methods of ResultSet interface:

  1. public boolean next():

    is used to move the cursor to the one row next from the current position, and we use the next() method in the while loop.if the cursor move to next rows if the rows find then it is true looop execute and then forward, At the End the rows not fatch then while condition false.In Blow Example you can See the next() method use.

  2. import java.sql.*;
    
    public class DB{
    public static void main(String args[]){
    	System.out.println("\t\t\t_________________");
    	System.out.println("");
    	System.out.println("\t\t\tWelcome to LMS");
    	System.out.println("\t\t\t_________________");
    
    	try {
    		Class.forName("com.mysql.cj.jdbc.Driver");
    		String DB_URL = "jdbc:mysql://localhost:3306/crud";
    		String DB_USER = "root";
    		String DB_PASSWORD = "technicalhassanali";
    		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    		Statement stmt=con.createStatement();
    
    		System.out.println("+------+-------------+----------------+--------+--------------+");
    		ResultSet rs=stmt.executeQuery("select * from student");
    
    		while(rs.next()) {
    			String sid = rs.getString(1);
    			String sname = rs.getString(2);
    			String fname = rs.getString(3);
    			String saddress = rs.getString(4);
    			String sphone = rs.getString(5);
    	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
    		}
    	    con.close();
    	    System.out.println("+------+-------------+----------------+--------+--------------+");
    
    	}catch(Exception e){
    		System.out.println(e);
    
    	}
      }
    }
  3. public boolean previous():

    is used to move the cursor to the one row previous from the current position.
    e.g If we have 8 rows in the table then we use the first rs.afterLast(); By using the afterLast() method cursor move the last position and then you write the rs.previous(); and you see the first position last record print that means that the cursor move backword 8 rows to 1st rows.

  4. package record;
    import java.sql.*;
    
    public class Test{
    public static void main(String args[]) throws Exception{
    	showData();
    
    }
    //Connection Method
    static Connection con;
    public static Connection dbconnect() {
    	try {
    		Class.forName("com.mysql.cj.jdbc.Driver");
    		String DB_URL = "jdbc:mysql://localhost:3306/crud";
    		String DB_USER = "root";
    		String DB_PASSWORD = "technicalhassanali";
    		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    		return con;
    	}catch(Exception e){
    		System.out.println(e);
    		return null;
    	}
    }
    
    public static void showData() {
    	try{
    		con = dbconnect(); //call connection Method
    		Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
    
    		System.out.println("+------+-------------+----------------+--------+--------------+");
    		ResultSet rs=stmt.executeQuery("select * from student");
    		rs.afterLast();
    		while(rs.previous()) {
    			String sid = rs.getString(1);
    			String sname = rs.getString(2);
    			String fname = rs.getString(3);
    			String saddress = rs.getString(4);
    			String sphone = rs.getString(5);
    	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
    		}
    	    con.close();
    	    System.out.println("+------+-------------+----------------+--------+--------------+");
    
    	}catch(Exception e){
    			System.out.println(e);
    			}
    }
    
    
    }//class close
  5. public boolean first():

    is used to move the cursor to the first row in result set object.

  6. package record;
    import java.sql.*;
    
    public class Test{
    public static void main(String args[]) throws Exception{
    	showData();
    
    }
    //Connection Method
    static Connection con;
    public static Connection dbconnect() {
    	try {
    		Class.forName("com.mysql.cj.jdbc.Driver");
    		String DB_URL = "jdbc:mysql://localhost:3306/crud";
    		String DB_USER = "root";
    		String DB_PASSWORD = "technicalhassanali";
    		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    		return con;
    	}catch(Exception e){
    		System.out.println(e);
    		return null;
    	}
    }
    
    public static void showData() {
    	try{
    		con = dbconnect(); //call connection Method
    		Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    
    		System.out.println("+------+-------------+----------------+--------+--------------+");
    		ResultSet rs=stmt.executeQuery("select * from student");
    		rs.first(); //cursor return only first rows
    			String sid = rs.getString(1);
    			String sname = rs.getString(2);
    			String fname = rs.getString(3);
    			String saddress = rs.getString(4);
    			String sphone = rs.getString(5);
    	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
    
    	    con.close();
    	    System.out.println("+------+-------------+----------------+--------+--------------+");
    
    	}catch(Exception e){
    			System.out.println(e);
    			}
    }
    
    }//class close
  7. public boolean last():

    is used to move the cursor to the last row in result set object.

  8. package record;
    import java.sql.*;
    
    public class Test{
    public static void main(String args[]) throws Exception{
    	showData();
    
    }
    //Connection Method
    static Connection con;
    public static Connection dbconnect() {
    	try {
    		Class.forName("com.mysql.cj.jdbc.Driver");
    		String DB_URL = "jdbc:mysql://localhost:3306/crud";
    		String DB_USER = "root";
    		String DB_PASSWORD = "technicalhassanali";
    		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    		return con;
    	}catch(Exception e){
    		System.out.println(e);
    		return null;
    	}
    }
    
    public static void showData() {
    	try{
    		con = dbconnect(); //call connection Method
    		Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    
    		System.out.println("+------+-------------+----------------+--------+--------------+");
    		ResultSet rs=stmt.executeQuery("select * from student");
    		rs.last(); //cursor return only last
    			String sid = rs.getString(1);
    			String sname = rs.getString(2);
    			String fname = rs.getString(3);
    			String saddress = rs.getString(4);
    			String sphone = rs.getString(5);
    	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
    
    	    con.close();
    	    System.out.println("+------+-------------+----------------+--------+--------------+");
    
    	}catch(Exception e){
    			System.out.println(e);
    			}
    }
    
    }//class close
  9. public boolean absolute(int row):

    is used to move the cursor to the specified row number in the ResultSet object.If you pass 3 value in rs.absolute(3); then only 3 value fatch.

  10. package record;
    import java.sql.*;
    
    public class Test{
    public static void main(String args[]) throws Exception{
    	showData();
    
    }
    
    //Connection Method
    static Connection con;
    public static Connection dbconnect() {
    	try {
    		Class.forName("com.mysql.cj.jdbc.Driver");
    		String DB_URL = "jdbc:mysql://localhost:3306/crud";
    		String DB_USER = "root";
    		String DB_PASSWORD = "technicalhassanali";
    		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    		return con;
    	}catch(Exception e){
    		System.out.println(e);
    		return null;
    	}
    }
    
    
    public static void showData() {
    	try{
    		con = dbconnect(); //call connection Method
    		Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
    
    		System.out.println("+------+-------------+----------------+--------+--------------+");
    		ResultSet rs=stmt.executeQuery("select * from student");
    		rs.absolute(3);  //return 3rd rows
    			String sid = rs.getString(1);
    			String sname = rs.getString(2);
    			String fname = rs.getString(3);
    			String saddress = rs.getString(4);
    			String sphone = rs.getString(5);
    	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
    
    	    con.close();
    	    System.out.println("+------+-------------+----------------+--------+--------------+");
    
    	}catch(Exception e){
    			System.out.println(e);
    			}
    }
    
    }//class close
  11. public boolean relative(int row):

    is used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative.

  12. package record;
    import java.sql.*;
    
    public class Test{
    public static void main(String args[]) throws Exception{
    	showData();
    
    }
    //Connection Method
    static Connection con;
    public static Connection dbconnect() {
    	try {
    		Class.forName("com.mysql.cj.jdbc.Driver");
    		String DB_URL = "jdbc:mysql://localhost:3306/crud";
    		String DB_USER = "root";
    		String DB_PASSWORD = "technicalhassanali";
    		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    		return con;
    	}catch(Exception e){
    		System.out.println(e);
    		return null;
    	}
    }
    
    public static void showData() {
    	try{
    		con = dbconnect(); //call connection Method
    		Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    
    		System.out.println("+------+-------------+----------------+--------+--------------+");
    		ResultSet rs=stmt.executeQuery("select * from student");
    		rs.relative(3); // return only relative rows
    			String sid = rs.getString(1);
    			String sname = rs.getString(2);
    			String fname = rs.getString(3);
    			String saddress = rs.getString(4);
    			String sphone = rs.getString(5);
    	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
    
    	    con.close();
    	    System.out.println("+------+-------------+----------------+--------+--------------+");
    
    	}catch(Exception e){
    			System.out.println(e);
    			}
    }
    
    }//class close
  13. public int getInt(int columnIndex):

    is used to return the data of specified column index of the current row as int.

  14. public int getInt(String columnName):

    is used to return the data of specified column index of the current row as int.

  15. public String getString(int columnIndex):

    is used to return the data of specified column index of the current row as int.

  16. public String getString(String columnName):

    is used to return the data of specified column index of the current row as int.


import java.sql.*;

public class DB{
public static void main(String args[]){
	System.out.println("\t\t\t_________________");
	System.out.println("");
	System.out.println("\t\t\tWelcome to LMS");
	System.out.println("\t\t\t_________________");

	try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String DB_URL = "jdbc:mysql://localhost:3306/crud";
		String DB_USER = "root";
		String DB_PASSWORD = "technicalhassanali";
		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
		Statement stmt=con.createStatement();

		System.out.println("+------+-------------+----------------+--------+--------------+");
		ResultSet rs=stmt.executeQuery("select * from student");
		while(rs.next()) {
			String sid = rs.getString(1);
			String sname = rs.getString(2);
			String fname = rs.getString(3);
			String saddress = rs.getString(4);
			String sphone = rs.getString(5);
	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
		}
	    con.close();
	    System.out.println("+------+-------------+----------------+--------+--------------+");
	}catch(Exception e){
		System.out.println(e);
	}
  }
}

Update Data

PreparedStatement pst = con.prepareStatement(query_name); is used to execute the query or update the Query instead using the Statement. PreparedStatement in Java allows you to write a parameterized query which gives better performance than Statement class in Java. PreparedStatement is very easy to use Parametrized constructor like pst.setString(1,variable_name) or pst.setInt(2,variable_name); and at last you can Enter the pst.executeUpdate();
The Java JDBC PreparedStatement primary features are:

  • Easy to insert parameters into the SQL statement.
  • Easy to reuse the PreparedStatement with new parameter values.
  • May increase performance of executed statements.
  • Enables easier batch updates

pst.executeUpdate(); – It will return 1, means that one rows Effected in the Database and the Data in the touple are added Sucessfully.If it will return 0 then means that the errror in the Data you Enter.int Add_Touble = pst.executeUpdate(); we print the Add Touple value the show the 1 or 0. 1 mean added data and 0 mean data not added.


import java.io.*;
import java.sql.*;

public class DB{
public static void main(String args[]) throws Exception{
	System.out.println("\t\t\t_________________");
	System.out.println("");
	System.out.println("\t\t\tWelcome to LMS");
	System.out.println("\t\t\t_________________");
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));


	try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String DB_URL = "jdbc:mysql://localhost:3306/crud";
		String DB_USER = "root";
		String DB_PASSWORD = "technicalhassanali";
		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);


		//Get input to Update the Record
		System.out.print("Student ID = ");
		int rid = Integer.parseInt(Bf.readLine());
		System.out.print("Student Name = ");
			String stu_Name = Bf.readLine();
		System.out.print("Father Name = ");
			String stu_Father = Bf.readLine();
		System.out.print("Address = ");
			String stu_Address = Bf.readLine();
		System.out.print("Phone Number =");
			String stu_phone = Bf.readLine();
			//Update Query
		String update_query = "UPDATE student set Name=?,Father_Name=?,Address=?,Phone_Number=? where id=?";
		PreparedStatement pst = con.prepareStatement(update_query);
		 pst.setString(1, stu_Name);
		 pst.setString(2, stu_Father);
		 pst.setString(3, stu_Address);
		 pst.setString(4, stu_phone);
		 pst.setInt(5, rid);
		 int result = pst.executeUpdate();
		 if(result>0) {
			 System.out.println("________________________");
			 System.out.println("Record Updated Sucessfully");
			 System.out.println("________________________");
		 }else {
			 System.out.println("________________________");
			 System.out.println("Record Not Updated");
			 System.out.println("________________________");
		 }
		 con.close(); //Connection Close
	}catch(Exception e){
			System.out.println(e);
			}
  }
}

Insert Data
import java.io.*;
import java.sql.*;
public class InsertData{
public static void main(String args[]) throws Exception{
	System.out.println("\t\t\t_________________");
	System.out.println("");
	System.out.println("\t\t\tWelcome to LMS");
	System.out.println("\t\t\t_________________");
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));

	try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String DB_URL = "jdbc:mysql://localhost:3306/crud";
		String DB_USER = "root";
		String DB_PASSWORD = "technicalhassanali";
		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);

		Statement stmt=con.createStatement();

		//Getting Input From User and Store in variables
		System.out.print("Student ID = ");
			int stu_id = Integer.parseInt(Bf.readLine());
		System.out.print("Student Name = ");
			String stu_Name = Bf.readLine();
		System.out.print("Father Name = ");
			String stu_Father = Bf.readLine();
		System.out.print("Address = ");
			String stu_Address = Bf.readLine();
		System.out.print("Phone Number =");
			String stu_phone = Bf.readLine();
			//Insert Query
		String insertQuery = "Insert into student(id,Name,Father_Name,Address,Phone_Number) values(?,?,?,?,?)";
		PreparedStatement pst = con.prepareStatement(insertQuery);
		pst.setInt(1,stu_id);
		pst.setString(2,stu_Name);
		pst.setString(3, stu_Father);
		pst.setString(4, stu_Address);
		pst.setString(5, stu_phone);
	    int check = pst.executeUpdate();
        if(check>0){
	    System.out.println("________________________");
	    System.out.println("Record Save Successfully");
	    System.out.println("________________________");
        }else{
        System.out.println("________________________");
	    System.out.println("Record Not Saved check Enter Value");
	    System.out.println("________________________");
        }
	    con.close();
	}catch(Exception e){
			System.out.println(e);
			}
  }
}

Delete Data
import java.io.*;
import java.sql.*;

public class InsertData{
public static void main(String args[]) throws Exception{
	System.out.println("\t\t\t_________________");
	System.out.println("");
	System.out.println("\t\t\tWelcome to LMS");
	System.out.println("\t\t\t_________________");
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));

	try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String DB_URL = "jdbc:mysql://localhost:3306/crud";
		String DB_USER = "root";
		String DB_PASSWORD = "technicalhassanali";
		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);

		System.out.print("Enter the ID of the Record = ");
		int sid = Integer.parseInt(Bf.readLine());

		String sql = "Delete from student where id="+sid;

		int deleteRows = stmt.executeUpdate(sql);
		if(deleteRows>0) {
			System.out.println("________________________");
			System.out.println("Record Deleted successfully");
			System.out.println("________________________");
		}else {
			System.out.println("________________________");
			System.out.println("Record Not Found");
			System.out.println("________________________");
		}
         con.close(); //Connection Close
		}catch(Exception e){
			System.out.println(e);
			}
  }
}

Search Record
import java.io.*;
import java.sql.*;

public class DB{
public static void main(String args[]) throws Exception{
	System.out.println("\t\t\t_________________");
	System.out.println("");
	System.out.println("\t\t\tWelcome to LMS");
	System.out.println("\t\t\t_________________");
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));

	try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String DB_URL = "jdbc:mysql://localhost:3306/crud";
		String DB_USER = "root";
		String DB_PASSWORD = "technicalhassanali";
		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);

		Statement stmt=con.createStatement();
		System.out.print("Enter Student ID= ");
		int searchRecord = Integer.parseInt(Bf.readLine());

		String Search_Record = "Select * from student where id="+searchRecord;

		Statement search = con.createStatement();
		ResultSet rs = search.executeQuery(Search_Record);
		if(rs.next()) {
			System.out.println("________________________");
			System.out.println("ID = "+rs.getString(1));
			System.out.println("Name = "+rs.getString(2));
			System.out.println("Father Name = "+rs.getString(3));
			System.out.println("Address = "+rs.getString(4));
			System.out.println("Phone Number = "+rs.getString(5));
	    System.out.println("________________________");
		}else {
			System.out.println("________________________");
			System.out.println("Record Not Found");
			System.out.println("________________________");
		}
		 con.close(); //Connection Close
	}catch(Exception e){
			System.out.println(e);
			}
  }
}

Download Java JDBC Complete Crud Operation code



Java JDBC Simple LMS Code
import java.io.*;
import java.sql.*;
import java.util.*;

public class Hassan{
public static void main(String args[]) throws Exception{
	System.out.println("\t\t\t_________________");
	System.out.println("");
	System.out.println("\t\t\tWelcome to LMS");
	System.out.println("\t\t\t_________________");
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));

	while(true) {
		System.out.println("Press 1 to Show Record");
		System.out.println("Press 2 to update Record");
		System.out.println("Press 3 to Insert Record");
		System.out.println("Press 4 to Delete Record");
		System.out.println("Press 5 to Search Record");
		System.out.println("Press 6 to Exit");
		int a = Integer.parseInt(Bf.readLine());

		if(a == 1) {
			showData();
		}else if(a == 2) {
			//delete student
			updateData();
		}else if(a == 3) {
			//display
			insertData();
		}
		else if(a == 4) {
			deleteData();
		}else if(a==5) {
			searchRecord();
		}
		else if(a == 6) {
			break;
		}
		else {

		}
	}

	System.out.println("________________________");
	System.out.println("Thanks for using LMS");
	System.out.println("________________________");

}


//Connection Method
static Connection con;
public static Connection dbconnect() {
	try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String DB_URL = "jdbc:mysql://localhost:3306/crud";
		String DB_USER = "root";
		String DB_PASSWORD = "technicalhassanali";
		Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
		return con;
	}catch(Exception e){
		System.out.println(e);
		return null;
	}
}


public static void showData() {
	try{
		con = dbconnect(); //call connection Method
		Statement stmt=con.createStatement();

		System.out.println("+------+-------------+----------------+--------+--------------+");
		ResultSet rs=stmt.executeQuery("select * from student");
		while(rs.next()) {
			String sid = rs.getString(1);
			String sname = rs.getString(2);
			String fname = rs.getString(3);
			String saddress = rs.getString(4);
			String sphone = rs.getString(5);
	    System.out.println("| "+sid+"    |  "+sname+" | "+fname+ " | "+saddress+ " | "+sphone+ " | "  );
		}
	    con.close();
	    System.out.println("+------+-------------+----------------+--------+--------------+");

	}catch(Exception e){
			System.out.println(e);
			}
}

								/*Update Data Method*/
public static void updateData() {
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));

	try{
		con = dbconnect(); //call connection Method
		Statement stmt=con.createStatement();

		//Get input to Update the Record
		System.out.print("Student ID = ");
		int rid = Integer.parseInt(Bf.readLine());
		System.out.print("Student Name = ");
			String stu_Name = Bf.readLine();
		System.out.print("Father Name = ");
			String stu_Father = Bf.readLine();
		System.out.print("Address = ");
			String stu_Address = Bf.readLine();
		System.out.print("Phone Number =");
			String stu_phone = Bf.readLine();
			//Update Query
		String update_query = "UPDATE student set Name=?,Father_Name=?,Address=?,Phone_Number=? where id=?";

		PreparedStatement pst = con.prepareStatement(update_query);
		 pst.setString(1, stu_Name);
		 pst.setString(2, stu_Father);
		 pst.setString(3, stu_Address);
		 pst.setString(4, stu_phone);
		 pst.setInt(5, rid);
		 int result = pst.executeUpdate();
		 if(result>0) {
			 System.out.println("________________________");
			 System.out.println("Record Updated Sucessfully");
			 System.out.println("________________________");
		 }else {
			 System.out.println("________________________");
			 System.out.print("Record Not Updated");
			 System.out.println("________________________");
		 }
		 con.close(); //Connection Close
	}catch(Exception e){
			System.out.println(e);
			}
}

								/*Insert Data Method*/
public static void insertData() {
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
	try{
		con = dbconnect(); //call connection Method
		Statement stmt=con.createStatement();

		//Insert Date
		System.out.print("Student ID = ");
			int stu_id = Integer.parseInt(Bf.readLine());
		System.out.print("Student Name = ");
			String stu_Name = Bf.readLine();
		System.out.print("Father Name = ");
			String stu_Father = Bf.readLine();
		System.out.print("Address = ");
			String stu_Address = Bf.readLine();
		System.out.print("Phone Number =");
			String stu_phone = Bf.readLine();
			//Insert Query
		String insertQuery = "Insert into student(id,Name,Father_Name,Address,Phone_Number) values(?,?,?,?,?)";
		PreparedStatement pst = con.prepareStatement(insertQuery);
		pst.setInt(1,stu_id);
		pst.setString(2,stu_Name);
		pst.setString(3, stu_Father);
		pst.setString(4, stu_Address);
		pst.setString(5, stu_phone);
	    pst.executeUpdate();
	    con.close();   //Connection Close
	    System.out.println("________________________");
	    System.out.println("Record Save Successfully");
	    System.out.println("________________________");
}catch(Exception e){
	System.out.println(e);
	}
}

						/*Delete Data Method*/
public static void deleteData() {
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
	Scanner input = new Scanner(System.in);

	try{
		con = dbconnect(); //call connection Method
		Statement stmt=con.createStatement();
		System.out.print("Enter the ID of the Record = ");
		int sid = Integer.parseInt(Bf.readLine());

		String sql = "Delete from student where id="+sid;
		int deleteRows = stmt.executeUpdate(sql);
		if(deleteRows>0) {
			System.out.println("________________________");
			System.out.println("Record Deleted successfully");
			System.out.println("________________________");
		}else {
			System.out.println("________________________");
			System.out.println("Record Not Found");
			System.out.println("________________________");
		}
}catch(Exception e){
	System.out.println(e);
	}
}
							/*Search Record Method*/
public static void searchRecord() {
	BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
	try {
		con = dbconnect(); //call connection Method
		Statement stmt=con.createStatement();
		System.out.print("Enter Student ID= ");
		int searchRecord = Integer.parseInt(Bf.readLine());

		String Search_Record = "Select * from student where id="+searchRecord;

		Statement search = con.createStatement();
		ResultSet rs = search.executeQuery(Search_Record);
		if(rs.next()) {
			System.out.println("________________________");
			System.out.println("ID = "+rs.getString(1));
			System.out.println("Name = "+rs.getString(2));
			System.out.println("Father Name = "+rs.getString(3));
			System.out.println("Address = "+rs.getString(4));
			System.out.println("Phone Number = "+rs.getString(5));
	    System.out.println("________________________");
		}else {
			System.out.println("________________________");
			System.out.println("Record Not Found");
			System.out.println("________________________");
		}
	}catch(Exception e){
		System.out.println(e);
		}

   }//close while loop

}//class close

Use of String.Format();

string.format() is used to the better performance in the output of the String.By the use of string.format() the output string should be made Stylish.We have Pass the arguments in this format() method.
There are many format specifiers we can use. Here are some common ones:

  • %a- floating point (except BigDecimal)
  • %b- Any type
  • %c- Character
  • %d- integer (incl. byte, short, int, long, bigint)
  • %e - Exponential floating-point number
  • %f - Floating-point number
  • %g - floating point
  • %h- any type
  • %i - Integer (base 10)
  • %o - Octal number (base 8)
  • %s - String
  • %u - Unsigned decimal (integer) number
  • %x - Hexadecimal number (base 16)
  • %t - Date/time
  • %n - Newline

In Blow Example of JDBC Course Section.I have Design in the format by using string.format().


package lms;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.*;
public class Course {

	static Connection con = null;
	static Statement stmt = null;
	public static void show() throws Exception {
		BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));

		dbConnect database = new dbConnect();
		con = database.db();
		Statement statement = con.createStatement();
		stmt = statement;

		while(true) {
			 System.out.println("Press 1 : Show Course");
			 System.out.println("Press 2 : Insert New Course");
			 System.out.println("Press 3 : Course Selection");
			 System.out.println("Press 4 : Show Select Course");
			 System.out.println("Press 5 : Delete Course");
			 System.out.println("Press 6 : Back");
			 int n = Integer.parseInt(Bf.readLine());
			 if(n==1) {
				 selectCourse();   //call student profile Method
			 }else if(n==2) {
				 InsertCourse();
			 }else if(n==3) {
				 course_selection();
			 }else if(n==4) {
				 showSelectCourse();
			 }else if(n==5) {
				 DeleteCourse();
			 }
			 else if(n==6) {
				 break;
			 }else {
				System.out.println("\t\t\t--------------------");
				System.out.println("\t\t\tEnter Correct Number");
				System.out.println("\t\t\t--------------------");
			 }
		 }//close while loop
	}

	public static void selectCourse() {
		BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
		try {
			String Query = "select * from lms.course";
			 ResultSet rs = stmt.executeQuery(Query);
			 System.out.format("+--------------+------------------------+--------------------------------------------+----------------+%n");
			 System.out.format("|  Course Code |           Name         |                Description                 |    Credit Hours|%n");
			 System.out.format("+--------------+------------------------+--------------------------------------------+----------------+%n");

			 while(rs.next()) {
				String courseId=String.format("| %-13s",rs.getString("Course_Code"));
				String Name = String.format("| %-23s",rs.getString("Name"));
				String Description =String.format("| %-43s", rs.getString("Description"));
				String Credit =String.format("| %-14s |",rs.getString("Credit_Hours"));
				System.out.println(courseId+""+Name+""+Description+""+Credit);

			 }
			 System.out.format("+--------------+------------------------+--------------------------------------------+----------------+%n");

		}catch(Exception e) {
			System.out.println(e);
		}

	}


	public static void InsertCourse() {
		BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
		try {
			String Query = "insert into Course(Course_Code,Name,Description,Credit_Hours)values(?,?,?,?)";
			PreparedStatement pst = con.prepareStatement(Query);
			System.out.println("Course Code = ");
			String course_id=Bf.readLine();
			System.out.println("Name = ");
			String name=Bf.readLine();
			System.out.println("Description = ");
			String desc=Bf.readLine();
			System.out.println("Credit Hours = ");
			String hours=Bf.readLine();

			pst.setString(1, course_id);
			pst.setString(2, name);
			pst.setString(3, desc);
			pst.setString(4, hours);
			int check = pst.executeUpdate();
			if(check>0) {
				System.out.println("Course Added Successfully");
			}else {
				System.out.println("Error Check Data");
			}
		}catch(Exception e) {
			System.out.println(e);
		}
	}



	public static void course_selection() {
		BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
		try {


			String Query = "insert into course_selection(Student_ID,Course_Code,Course_Name,Teacher_ID)values(?,?,?,?)";

			System.out.println("Student ID");
				String std_id = Bf.readLine();
			System.out.println("Course Code = ");
				String course_id=Bf.readLine();
				System.out.println("Name = ");
				String name=Bf.readLine();
			System.out.println("Teacher ID = ");
				String tname=Bf.readLine();

		    PreparedStatement pst = con.prepareStatement(Query);
			pst.setString(1, std_id);
			pst.setString(2, course_id);
			pst.setString(3, name);
			pst.setString(4, tname);
			int check = pst.executeUpdate();
			if(check>0) {
				System.out.println("Course Select Successfully");
			}else {
				System.out.println("Error Check Data");
			}

		}catch(Exception e) {
			System.out.println(e);
		}
	}




	public static void showSelectCourse() {
		try {
			BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
			try {

				String Query = "Select * from lms.course_selection";


				 System.out.format("+------------+-----------------+------------------------+----------------+--------------------------+--------------------+%n");
				 System.out.format("| Student ID |    Course Code  |       Course Name      |    Teacher ID  | Obtained_Number          |   Total_Number     |%n");
				 System.out.format("+------------+-----------------+------------------------+----------------+--------------------------+--------------------+%n");
				 ResultSet rs = stmt.executeQuery(Query);
					while(rs.next()) {
						String std_id=        String.format("| %-11s",rs.getString("Student_ID"));
						String ccode =        String.format("| %-16s",rs.getString("Course_Code"));
						String cname =        String.format("| %-23s", rs.getString("Course_Name"));
						String teacherid =    String.format("| %-15s",rs.getString("Teacher_ID"));
						String obtn_number =  String.format("| %-25s",rs.getString("Obtained_Number"));
						String total_number = String.format("| %-19s|", rs.getString("Total_Number"));
						System.out.println(std_id+""+ccode+""+cname+""+teacherid+""+obtn_number+""+total_number);
						}
					System.out.format("+------------+-----------------+------------------------+----------------+--------------------------+--------------------+%n");
				}
			catch(Exception e) {
				System.out.println(e);
			}
		}catch(Exception e) {
			System.out.println(e);
		}
	}



	public static void DeleteCourse() {
		BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
		try {
			System.out.println("Student ID");
			String std_id = Bf.readLine();

			System.out.println("Course Code = ");
			String course_code=Bf.readLine();

			String Query = "Delete from course_selection where Course_Code=? and Student_ID=?";
			PreparedStatement pst = con.prepareStatement(Query);
			pst.setString(1, course_code);
			pst.setString(2, std_id);
			int check = pst.executeUpdate();
			if(check>0) {
				System.out.println("Course Delete Successfully");
			}else {
				System.out.println("Error Check Course Code");
			}
		}catch(Exception e) {
			System.out.println(e);
		}
	}

}

LMS

Click the Blow Link and Download the complete LMS Project.

Download LMS Project

Post a Comment

Post a Comment (0)

Previous Post Next Post