1. Write a menu driven program (Command line interface) to perform the followingoperations on student table. 1. Insert 2. Modify 3. Delete 4. Search 5. View All 6. Exit
import java.sql.*;
import java.io.*;
class Ass2setb1
{
public static void main(String args[])throws IOException,SQLException
{
Connection c=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement ps1,ps2,ps=null;
int ch;
try
{
Class.forName("org.postgresql.Driver");
c=DriverManager.getConnection("jdbc:postgresql://192.168.100.10/tybcs8",
"tybcs8","");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ps=c.prepareStatement("insert into jstud values(?,?,?)");
ps1=c.prepareStatement("delete from jstud where rno=?");
ps2=c.prepareStatement("update jstud set per=? where rno=?");
if(c==null)
{
System.out.println("Connection not established");
}
else
{
System.out.println("Connection established");
stmt=c.createStatement();
do
{
System.out.println("1.Insert");
System.out.println("2.Modify");
System.out.println("3.Delete");
System.out.println("4.Search");
System.out.println("5.View All");
System.out.println("6.Exit");
System.out.println("Enter choise :");
ch =Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter Roll Number :");
int rno=Integer.parseInt(br.readLine());
System.out.println("Enter Name :");
String name=br.readLine();
System.out.println("Enter percentage:");
float per=Float.parseFloat(br.readLine());
ps.setInt(1,rno);
ps.setString(2,name);
ps.setFloat(3,per);
ps.executeUpdate();
break;
case 2:
System.out.println("Enter Roll Number :");
rno=Integer.parseInt(br.readLine());
System.out.println("Enter percentage to modify:");
per=Float.parseFloat(br.readLine());
ps2.setInt(2,rno);
ps2.setFloat(1,per);
ps2.executeUpdate();
break;
case 3:
System.out.println("Enter Roll Number for delete record :");
rno=Integer.parseInt(br.readLine());
ps1.setInt(1,rno);
ps1.executeUpdate();
break;
case 4:
System.out.println("Enter Roll Number for search record :");
rno=Integer.parseInt(br.readLine());
rs=stmt.executeQuery("Select name,per from jstud where rno="+rno);
while(rs.next())
{
System.out.println("Name:"+rs.getString(1));
System.out.println("Percentage:"+rs.getFloat(2));
}
break;
case 5:
rs=stmt.executeQuery("Select * from jstud");
while(rs.next())
{
System.out.println("Roll no:"+rs.getInt(1));
System.out.println("Name:"+rs.getString(2));
System.out.println("Percentage:"+rs.getFloat(3));
}
break;
case 6:
System.exit(0);
break;
}
}while(ch<6);
c.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
0 Comments
If anyone has Doubts or suggestions please let me know