import java.sql.*;
import java.io.*;
class Database
{
Connection con;
ResultSet rs;
Statement stmt;
BufferedReader br;
String str,temp;
Database()
{
try
{
con=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ACCESS_DB");
stmt=con.createStatement();
str=new String();
temp=new String();
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Connection Ok... Tested...");
}
catch(Exception E)
{
System.out.println("Connection Error : " + E.getMessage());
}
}
public void search()
{
System.out.println();
try
{
System.out.print("Enter string to be search : ");
str=br.readLine();
temp="Select * from Mobile Where Name Like '%" + str + "%' OR Num Like '%" + str + "%'";
System.out.println(temp);
rs=stmt.executeQuery(temp);
while(rs.next())
{
for(int i=1;i<=2;i++)
{
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void add_new()
{
System.out.println();
String no,name;
try
{
System.out.print("Enter Mobile No : ");
no=br.readLine();
System.out.print("Enter Name : ");
name=br.readLine();
temp="Insert into Mobile Values('"+no+"','"+name+"')";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Record Successfully Inserted...");
}
else
{
System.out.println("Record Insertion failure...");
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void select_all()
{
System.out.println();
try
{
rs=stmt.executeQuery("Select * from Mobile");
while(rs.next())
{
for(int i=1;i<=2;i++)
{
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void con_close()
{
try
{
con.close();
}
catch(Exception E)
{
System.out.println("Connection Closing Error : " + E.getMessage());
}
}
public void update_record()
{
System.out.println("1.Search by Name");
System.out.println("1.Search by Number");
System.out.print("Enter your choice : ");
int ch1;
try
{
ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.print("Enter Name : ");
str=br.readLine();
System.out.print("Enter New Number : ");
temp="Update Mobile Set Num='"+br.readLine()+"' Where Name='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Updation Successful...");
}
else
{
System.out.println("Updation Unsuccessful...");
}
break;
case 2:
System.out.print("Enter Number : ");
str=br.readLine();
System.out.println("Enter New Name : ");
temp="Update Mobile Set Name='"+br.readLine()+"' Where Num='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Updation Successful...");
}
else
{
System.out.println("Updation Unsuccessful...");
}
break;
default:
System.out.println("Wrong Choice... Try Again...");
}
}
catch(Exception E)
{
System.out.println("Updation Error : "+E.getMessage());
}
}
public void delete_record()
{
System.out.println("1. Search by Name");
System.out.println("2. Search by Number");
System.out.print("Enter your choice : ");
int ch1;
try
{
ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.print("Enter Name to be deleted : ");
str=br.readLine();
temp="Delete From Mobile where Name='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Deletion Successful...");
}
else
{
System.out.println("Deletion failure...");
}
break;
case 2:
System.out.print("Enter Number to be deleted : ");
str=br.readLine();
temp="Delete From Mobile where Num='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Deletion Successful...");
}
else
{
System.out.println("Deletion failure...");
}
break;
default :
System.out.println("Wrong Choice...");
}
}
catch(Exception E)
{
System.out.println("Record Delection Error : " + E.getMessage());
}
}
}
public class DB_SAMPLE
{
public static void main(String args[])
{
Database db=new Database();
BufferedReader br;
int ch=0;
System.out.println("");
System.out.println("Mobile Contact Database for Personal Desktop Use");
System.out.println("");
System.out.println("");
System.out.println("1. See Entire Database");
System.out.println("2. Search");
System.out.println("3. Add new Record");
System.out.println("4. Delete");
System.out.println("5. Update");
System.out.print("Enter your Choice : ");
try
{
br=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception E)
{
System.out.println("Console Reading Error : " + E.getMessage());
}
switch (ch)
{
case 1:
db.select_all();
break;
case 2:
db.search();
break;
case 3:
db.add_new();
break;
case 4:
db.delete_record();
break;
case 5:
db.update_record();
break;
default:
System.out.println("Wrong Choice... Try Again...");
}
db.con_close();
}
}