Here i use eclipse software and Oracle Database
1. Create a class file named city and paste this.
2. Create a class file named main and past the content given below City class.
---------------------------------------------------------------------------------------------------------------
import java.sql.*;
import java.util.Scanner;
public class City 
{
 Scanner sc=new Scanner(System.in); 
 String b,f,d;
 Connection c;
 PreparedStatement P;
 public City() throws ClassNotFoundException, SQLException
 {
 final String driver="oracle.jdbc.driver.OracleDriver";
 final String c1="jdbc:oracle:thin:@localhost:1521:xe";
 final String user="system";
 final String pass="a"; 
 Class.forName(driver);
 c=DriverManager.getConnection(c1,user,pass);
 try{
 if(c!=null)
   {
  System.out.println("connection Established"); 
      }
  else 
   {
  System.out.println("Not Connected");
   }
 }catch(Exception e){System.out.println("exception is :"+e);}
 }
 void create() throws SQLException
 {
  String s1="create table MyCity(ID number(10),Name varchar2(15),CountryCode varchar2(20),District varchar2(10),Population number(10))";
  P=c.prepareStatement(s1);
  P.executeUpdate();
  System.out.println(" Table Created");
 }
void insert() throws SQLException
{
 int a,e;
 String s1="insert into MyCity values(?,?,?,?,?)";
 PreparedStatement q1=c.prepareStatement(s1);
 System.out.println("enter no of records");
 int  n=sc.nextInt();
 for(int i=1;i<=n;i++){
  System.out.println("enter the "+ i + " record");
  System.out.println("------------------------");
 System.out.print("Enter id : ");
  q1.setInt(1,a=sc.nextInt());
 sc.nextLine();
 System.out.print("Enter name: ");
 q1.setString(2, b=sc.nextLine());
 System.out.println("Enter CountryCode : ");
 q1.setString(3, d=sc.nextLine());
 System.out.println("Enter  District : ");
 q1.setString(4,f=sc.nextLine());
 System.out.println("Enter Population :  ");
 q1.setInt(5, e=sc.nextInt());
    q1.executeUpdate();
 }
System.out.println("Records isnerted ");
}
void delete() throws SQLException
{
 System.out.println("Enter the record id to delete");
 int a=sc.nextInt();
 String s1="delete  MyCity where ID="+a;
 PreparedStatement q1=c.prepareStatement(s1);
 q1.executeUpdate();
 System.out.println("Record Deleted ");
}
void print_city() throws SQLException
{
 String s1="select District from MyCity where District like'k%'";
 PreparedStatement q1=c.prepareStatement(s1);
 ResultSet rs=q1.executeQuery(s1);
 while(rs.next()) {System.out.println("City :" +rs.getString(1));}
}
void print_order()throws SQLException
{
String s1="select ID,Name,CountryCode,District,poprulation from MyCity order by District"; 
PreparedStatement q1=c.prepareStatement(s1);
ResultSet rs=q1.executeQuery(s1);
while(rs.next()) 
{
System.out.println(" ID :" +rs.getInt(1));
System.out.println("Name :"+ rs.getString(2));
System.out.println("COuntry Code : "+rs.getString(3));
System.out.println("District : "+rs.getString(4));
System.out.println("Population :  "+rs.getInt(5));
System.out.println("---------------------------------------");
}
}
}
-------------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.sql.SQLException;
public class main {
public static void main(String []args) throws Exception 
{ System.out.println("                   -----------welcome to my Database-------------    ");
 City c2=new City();
System.out.println("enter your choice 1. create");
System.out.println("2. insert elements ");
System.out.println("3. delete row");
System.out.println("4. print city namestart with k");
System.out.println("5. print all detail in ascending order");
Scanner sc=new Scanner(System.in);
int ch=sc.nextInt();
switch(ch)
{
case 1: c2.create();
break;
case 2: c2.insert();
break;
case 3: c2.delete();
break;
case 4: c2.print_city();
break;
case 5: c2.print_order();
break;
default:
System.out.println("wrong choice");
}
}
}
 

 

No comments:
Post a Comment