Java JDBC program to implement CRUD Operation without Updatable Resultset

JDBC program to perform all CRUD operation on data stored in student table, containing the columns for roll_number number, name varchar2(50), and result in percentage without using Updatable resultset

Code:

package aj.pkg11;

import java.sql.*;

import java.util.*;

public class AJ11 {

public static void main(String args[])throws Exception{

int i=0 , op=0;

Float r;

String s = null,str = null;

String ch = null;

Class.forName("com.mysql.jdbc.Driver");

Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");

Scanner sc= new Scanner(System.in);

PreparedStatement pstm = cn.prepareStatement("Insert into student_table values (?,?,?)");

PreparedStatement dstm = cn.prepareStatement("Delete from student_table where Roll_Number = ?");

Statement ustm = cn.createStatement();

Statement stm = cn.createStatement();

do{

System.out.println("Which operation do you want to perform ?\n1.Create Records\n2.Read Records\nUpdate Records\n4.Delete Records");

op = sc.nextInt();

switch(op){

case 1:

System.out.println("Enter RollNumber : ");

i = sc.nextInt();

sc.nextLine();

pstm.setInt(1,i);

System.out.println("Enter Name : ");

str = sc.nextLine();

pstm.setString(2,str);

System.out.println("Enter Result : ");

r = Float.parseFloat(sc.nextLine());

pstm.setFloat(3,r);

boolean b = pstm.execute();

if(b==true){

System.out.println("Record for " +str+ " inserted");

}

break;

case 2:

ResultSet rs = stm.executeQuery("SELECT * FROM student_table");

int n = 1;

while(rs.next()){

System.out.println("Record " + n);

System.out.println("Roll_Number : "+rs.getInt(1));

System.out.println("Name : "+ rs.getString(2));

System.out.println("Result : "+rs.getInt(3));

n++;

}

break;

case 3:

System.out.println("Enter roll_number of record where you want to update");

int j = sc.nextInt();

System.out.println("Enter details for updation: ");

System.out.println("Enter RollNumber : ");

i = sc.nextInt();

sc.nextLine();

System.out.println("Enter Name : ");

s = sc.nextLine();

System.out.println("Enter Result : ");

r = Float.parseFloat(sc.nextLine());

int no = ustm.executeUpdate("Update student set Roll_Number ="+ i+",Name ="+ s+",Result ="+r+"where Roll_Number="+j);

System.out.println(no + " record(s) have been updated . ");

break;

case 4:

System.out.println("Enter roll_number of record for deletion.");

j = sc.nextInt();

dstm.setInt(1,j);

b = dstm.execute();

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

break;

default:

System.out.println("Enter correct choice");

}

System.out.println("Do you want to perform more operations ? (Y/N)");

ch = sc.next();

}while(ch.equals("Y") || ch.equals("y"));

pstm.close();

stm.close();

cn.close();

}

}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Java Program to Implement sorting algorithm using TCP on Server application

C++ program to perform data transformation Min-max and Z score Normalization