[go: up one dir, main page]

0% found this document useful (0 votes)
15 views2 pages

Program 7

Uploaded by

omchudasama3688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Program 7

Uploaded by

omchudasama3688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

*;
import java.sql.*;
public class Prog7 {
public static void main(String[] args)throws Exception
{
while(true)
{
System.out.println("\n 1.Insert a Record\n
2.Update Record \n
3.Delete a Record \n
4 Search Record \n
5 Exit");
System.out.println("Enter your choice : ");
DataInputStream br=new DataInputStream(System.in);
int x=Integer.parseInt(br.readLine());
switch(x)
{
case 1:
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection
("jdbc:mysql://localhost:3306/abc","root","");
System.out.println("Inserting Records...");
PreparedStatement ps=conn.prepareStatement("insert into books
values(?,?,?,?)");
System.out.println("Enter Book ID --->");
int bid=Integer.parseInt(br.readLine());
System.out.println("Enter title of book --->");
String title=br.readLine();
System.out.println("Enter Author Name --->");
String auth=br.readLine();
System.out.println("Enter price --->");
float pr=Float.valueOf(br.readLine()).floatValue();
ps.setInt(1,bid);
ps.setString(2,title);
ps.setString(3,auth);
ps.setFloat(4,pr);
ps.executeUpdate();
System.out.println("Record Inserted");
conn.close();
break;
case 2:
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/abc","root","");
System.out.println("Enter Book ID of record you wish to update :");
bid=Integer.parseInt(br.readLine());
System.out.println (“Enter New Title :”);
title=Integer.parseInt(br.readLine());
String str=”update book set title=? Where Book_id=?”;
PreparedStatement ps=con.prepareStatement(str);
ps.setString(1,title);
ps.setInt(2,bid);
ps.executeUpdate();
System.out.println("Record Updated");
break;
case 3:
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/abc","root","");
System.out.println("Enter Book ID of record you want to delete :");
bid=Integer.parseInt(br.readLine());
String sql=”delete from book where Book_id=?”;
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1,bid);
ps.executeUpdate();
System.out.println("Record Deleted");
break;
case 4:
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/abc","root","");
System.out.println("Enter Book ID to search :");
String t1=br.readLine();
Statement st=con.createStatement();
String str="select * from books where Book_id="+t1;
ResultSet rs=st.executeQuery(str);
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+"
"+rs.getFloat(4));
}
break;
case 5:
System.exit(0);
break;
}
}
}
}

You might also like