JDBC Program insert record and display record


//JDBC Program insert record and display record
import java.sql.*;
public class student
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver
            Connection con = DriverManager.getConnection("jdbc:odbc:TYBCA"); //Create Connection with Data Source Name : BCAII
            Statement stmt = con.createStatement(); // Create Statement
            stmt.executeUpdate("insert into student values(1,'sachin','bmt')");
            // stmt.executeQuery("select * from student"); // Execute Query
            ResultSet rs = stmt.executeQuery("select * from student"); //return the data from Statement into ResultSet
            while(rs.next()) // Retrieve data from ResultSet
            {
                System.out.println("Roll no : "+rs.getInt(1));//1st column of Table
                System.out.println("Name : "+rs.getString(2)); //2st column of Table from database
                System.out.println("Address : "+rs.getString(3)); //3rd column of Table
            }
            stmt.close();
            con.close();
        }
        catch (Exception e)
        {
            System.out.println("Exception : "+e);
        }
    }
}
/*Output
D:\sachin>javac student.java

D:\sachin>java student
Roll no : 1
Name : sachin
Address : bmt
*/

WAP read charator form file and Display if charator if is alphabet then reverse it Case wheter it digit or Space

/*WAP read charator form file and Display
if charator if is alphabet then reverse it Case
wheter it digit or Space
*/
import java.io.*;
class Slip1
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("a.txt");
while((c=fr.read())!=-1)
{
if(c>=65&&c<=90)
{
c=c+32;
System.out.print((char)c);
}
else if(c>=97&&c<=122)
{
c=c-32;
System.out.print((char)c);
}
else if((char)c==' ')
{
System.out.print("Space"+' ');
}
else if(c>=48&&c<=57)
{
System.out.print("Digit"+(char)c+' ');
}
else
{
System.out.print((char)c);
}

}
fr.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
a.txt
Shinde Sachinde 9767@gmail.com
RS1234
D:\javapro\file>javac Slip1.java
D:\javapro\file>java Slip1
sHINDESpace sACHINSpace Digit9 Digit7 Digit6 Digit7 @GMAIL.COM
rsDigit1 Digit2 Digit3 Digit4
*/

To copy the content one file into the another file while copying change the case of alphabet and replase all Digit by '*'.

/*To copy the content one file into the another file
while copying change the case of alphabet
and replase all Digit by '*'.
*/
import java.io.*;
class Slip2
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("a.txt");
FileWriter fw=new FileWriter("b.txt");
while((c=fr.read())!=-1)
{
if(c>=65&&c<=90)
{
c=c+32;
fw.write(c);
}
else if(c>=97&&c<=122)
{
c=c-32;
fw.write(c);
}
else if(c>=48&&c<=57)
{
fw.write('*');
}
else
{
fw.write(c);
}

}
System.out.println("Copy Successfully");
fr.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
a.txtfile
Shinde Sachin 9767@gmail.com
1234R

D:\javapro\file>javac Slip2.java

D:\javapro\file>java Slip2
Copy Successfully

D:\javapro\file>
b.txt
sHINDE sACHIN ****@GMAIL.COM
****r
*/

Count the Spaces in file

//Count the Spaces in file
import java.io.*;
class Space
{
public static void main(String args[])throws IOException
{
int c,lcnt=1;
try
{
FileReader fr=new FileReader("Space.java");
while((c=fr.read())!=-1)
{
System.out.print((char)c);
if((char)c==' ')
{
lcnt ++;
}
}fr.close();
System.out.println("no of  Space in file"+lcnt);
}catch(Exception e)
{
System.out.println(e);
}
}
}


Count the lines in file

//Count the lines in file
import java.io.*;
class Line
{
public static void main(String args[])throws IOException
{
int c,lcnt=1;
try
{
FileReader fr=new FileReader("Line.java");
while((c=fr.read())!=-1)
{
System.out.print((char)c);
if((char)c=='\n')
{
lcnt ++;
}
}fr.close();
System.out.println("no of lines io file"+lcnt);
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*
D:\javapro\file>javac Line.java

D:\javapro\file>java Line
//Count the lines in file
import java.io.*;
class Line
{
        public static void main(String args[])throws IOException
        {
                int c,lcnt=1;
                try
                {
                        FileReader fr=new FileReader("Line.java");
                        while((c=fr.read())!=-1)
                        {
                                System.out.print((char)c);
                                if((char)c=='\n')
                                {
                                        lcnt ++;
                                }
                        }fr.close();
                System.out.println("no of lines io file"+lcnt);
                }catch(Exception e)
                {
                        System.out.println(e);
                }
        }
}
no of lines io file26
*/

Count the Spaces,word,lines,space in file

//Count the Spaces,word,lines,space in file
import java.io.*;
class Count
{
public static void main(String args[])//throws IOException
{
int c,scnt=0,ccnt=0,lcnt=0,wcnt=0;
try
{
FileReader fr=new FileReader("Count.java");
while((c=fr.read())!=-1)
{
ccnt++;
if((char)c==' ')
{
scnt ++;
}
if((char)c=='\n')
{
lcnt++;
}
if((char)c==' '||(char)c=='\n')
{
++wcnt;
}
}
fr.close();
System.out.println("no of  Space in file"+scnt);
System.out.println("no of  Line in file"+lcnt);
System.out.println("no of  Word in file"+wcnt);
System.out.println("no of  Character in file"+ccnt);
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
*/

Simple copy one file to another file and Display on the cmd prompt

//Simple copy one file to another file and Display on the cmd prompt
import java.io.*;
class CopyDis
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("CopyDis.java");
FileWriter fw=new FileWriter("SimpleCopy.txt");
while((c=fr.read())!=-1)
{
fw.write(c);
System.out.print((char)c);
}
System.out.println("Copy Successfully");
fr.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
*/

Simple copy one file to another file

//Simple copy one file to another file
import java.io.*;
class Copy
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("Copy.java");
FileWriter fw=new FileWriter("SimpleCopy.txt");
while((c=fr.read())!=-1)
{
fw.write(c);
}
System.out.println("Copy Successfully");
fr.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
*/

WAP to accept n numbers from command line and stores all prime & perfect numbers in different array & display it.

class PrimePerfect
{
            public static void main(String arg[])
            {
                        int num[]=new int[10];
                        int pr[]=new int[10];
                        int per[]=new int[10];
                        int flag=0,i,j,c1=0,c2=0,sum=0;
                        for(i=0;i<arg.length;i++)
                        {
                                    num[i]=Integer.parseInt(arg[i]);
                        }
                        System.out.println("Prime Number are-");
                        for(i=0;i<arg.length;i++)
                        {
                                    flag=0;
                                    for(j=2;j<num[i];j++)
                                    {
                                                if(num[i]%j==0)
                                                {
                                                            flag=1;
                                                            break;
                                                }
                                    }
                                    if(flag==0)
                                    {
                                                pr[c1]=num[i];
                                                System.out.println(pr[c1++]);
                                    }
                        }
                        System.out.println("Perfect Number are-");
                        for(i=0;i<arg.length;i++)
                        {
                                    sum=0;
                                    for(j=1;j<num[i]-1;j++)
                                    {
                                                if(num[i]%j==0)
                                                {
                                                            sum=sum+j;
                                                }
                                    }
                                    if(num[i]==sum)
                                    {
                                                per[c2]=num[i];
                                                System.out.println(per[c2++]);
                                    }
                        }         
                       
            }
}
/*OUTPUT:
D:\javapro>javac PrimePerfect.java

D:\javapro>java PrimePerfect 1 2 3 496 7 6 9 28 10 11
Prime Number are-
1
2
3
7
11
Perfect Number are-
496
6

*/

WAP to accept n numbers from command line and stores all prime & perfect numbers in different array & display it.

class PrimePerfect
{
            public static void main(String arg[])
            {
                        int num[]=new int[10];
                        int pr[]=new int[10];
                        int per[]=new int[10];
                        int flag=0,i,j,c1=0,c2=0,sum=0;
                        for(i=0;i<arg.length;i++)
                        {
                                    num[i]=Integer.parseInt(arg[i]);
                        }
                        System.out.println("Prime Number are-");
                        for(i=0;i<arg.length;i++)
                        {
                                    flag=0;
                                    for(j=2;j<num[i];j++)
                                    {
                                                if(num[i]%j==0)
                                                {
                                                            flag=1;
                                                            break;
                                                }
                                    }
                                    if(flag==0)
                                    {
                                                pr[c1]=num[i];
                                                System.out.println(pr[c1++]);
                                    }
                        }
                        System.out.println("Perfect Number are-");
                        for(i=0;i<arg.length;i++)
                        {
                                    sum=0;
                                    for(j=1;j<num[i]-1;j++)
                                    {
                                                if(num[i]%j==0)
                                                {
                                                            sum=sum+j;
                                                }
                                    }
                                    if(num[i]==sum)
                                    {
                                                per[c2]=num[i];
                                                System.out.println(per[c2++]);
                                    }
                        }         
                       
            }
}
/*OUTPUT:
D:\javapro>javac PrimePerfect.java

D:\javapro>java PrimePerfect 1 2 3 496 7 6 9 28 10 11
Prime Number are-
1
2
3
7
11
Perfect Number are-
496
6

*/

Write a java program to accept n name of cities & Sort them descending order.

class SortCity
{
            public static void main(String arg[])
            {
                        String name[]=new String[10];
                        int l=arg.length;
                        String temp;
                                    for(int i=0;i<l;i++)
                                    {
                                                name[i]=arg[i];
                                    }
                                    for(int j=0;j<l;j++)
                                    {
                                                for(int k=j+1;k<l;k++)
                                                {
                                                            if((name[j].compareTo(name[k]))<0)
                                                            {
                                                                        temp=name[j];
                                                                        name[j]=name[k];
                                                                        name[k]=temp;
                                                            }
                                                }
                                    }
                                    System.out.println("Sorted City Are-");
                                    for(int i=0;i<l;i++)
                                    {         
                                                System.out.println(name[i]);
                                    }
            }
}

/*OUTPUT
D:\javapro>javac SortCity.java

D:\javapro>java SortCity  pune nagar solapur thane satara baramati
Sorted City Are-
thane
solapur
satara
pune
nagar
baramati

*/

Define abstract class Shape with abstract method area() & Volume().Calculate area & Volume of Cone & Cylinder.

import java.io.*;
import java.lang.*;
abstract class Shape
{
float pi=3.14f;
abstract void area();
abstract void volume();
}
class Cone extends Shape
{
int red,side,height;
Cone(int r,int s,int h)
{
red=r;
side=s;
height=h;
}
void area()
{
float a=pi*red*side;
System.out.println("Area of Cone-"+a);
}
void volume()
{
float v=pi*red*red*(height/3);
System.out.println("Volume of Cone-"+v);
}
}
class Cylinder extends Shape
{
int red ,height;
Cylinder(int r,int h)
{
red=r;
height=h;
}
void area()
{
float a1=2*pi*red*height+2*pi*red*red;
System.out.println("Area of Cylinder-"+a1);
}
void volume()
{
float v1=pi*red*red*height;
System.out.println("Volume of Cylinder-"+v1);
}
}
class ShapeDemo1
{
public static void main(String arg[])
{
int r,s,h;
DataInputStream din=new DataInputStream(System.in);
try
{
System.out.println("Enter the values of Redius, Side , height-");
r=Integer.parseInt(din.readLine());
s=Integer.parseInt(din.readLine());
h=Integer.parseInt(din.readLine());
Cone c1=new Cone(r,s,h);
c1.area();
c1.volume();
Cylinder c2=new Cylinder(r,h);
c2.area();
c2.volume();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
/*Output
D:\javapro\Ass2>javac ShapeDemo1.java
Note: ShapeDemo1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

D:\javapro\Ass2>java ShapeDemo1
Enter the values of Redius, Side , height-
4
6
8
Area of Cone-75.36
Volume of Cone-100.48
Area of Cylinder-301.44
Volume of Cylinder-401.92
*/

Create an interface Shape with method area() Calculate area of Circle and Sphere. (Use final keyword)

import java.io.*;
interface Shape
{
final float pi=3.14f;
float area(int r);
}
class Circle implements Shape
{
public float area(int r)
{
return(pi*r*r);
}
}
class Sphere implements Shape
{
public float area(int r)
{
return(4*pi*r*r);
}
}
class ShapeDemo
{
public static void main(String arg[])
{
int r;
BufferedReader din=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("Enter radius-");
r=Integer.parseInt(din.readLine());
Circle c1=new Circle();
Sphere s1=new Sphere();
System.out.println("Area of Circle:"+c1.area(r));
System.out.println("Area of Sphere:"+s1.area(r));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
/*Output
D:\javapro\Ass2>javac ShapeDemo.java

D:\javapro\Ass2>java ShapeDemo
Enter radius-5
Area of Circle:78.5
Area of Sphere:314.0
*/

WAP to accept n cricket player, display average run of single player & all players. (Use array of object, method overloading & static keyword).

import java.io.*;
class Player
{
int p_code,run;
String p_name;
int i_played;
int nooftime_notout;
float avg;
void accept()
{
DataInputStream din=new DataInputStream(System.in);
try
{
System.out.print("Enter the player code-");
p_code=Integer.parseInt(din.readLine());
System.out.print("Enter the player name-");
p_name=din.readLine();
System.out.print("Enter the player runs-");
run=Integer.parseInt(din.readLine());
System.out.print("Enter the player inning played-");
i_played=Integer.parseInt(din.readLine());
System.out.print("Enter the player no of time not out-");
nooftime_notout=Integer.parseInt(din.readLine());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void display()
{

avg=run/(i_played-nooftime_notout);
System.out.println(p_name+"         "+avg);
}
void display(String nm)
{
if((nm.compareTo(p_name))==0)
{
avg=run/(i_played-nooftime_notout);
System.out.println(p_name+"         "+avg);
}
}
}
class PlayerDemo
{
public static void main(String arg[])
{
Player p1[]=new Player[10];
//Player p=new Player();
int i,n,ch;
boolean flag=true;
String nm;
DataInputStream din=new DataInputStream(System.in);
try
{
System.out.print("How many record accept u want-");
n=Integer.parseInt(din.readLine());
for(i=0;i<n;i++)
{
p1[i]=new Player();
p1[i].accept();
}
while(true)
{
System.out.println("\n1.Display average of all player\n2.Display average run of single player \n3.Exit ");
System.out.print("Enter ur Choice-");
ch=Integer.parseInt(din.readLine());

switch(ch)
{
case 1:
System.out.println("Player name is "+"   "+"Average is");
for(i=0;i<n;i++)
{
p1[i].display();
}
break;
case 2:
System.out.print("Enter player name whose display Average-");
nm=din.readLine();
System.out.println("\nPlayer name is "+"   "+"Average is");
for(i=0;i<n;i++)
{
p1[i].display(nm);
}
break;
case 3:
System.exit(0);
break;
default:

System.out.println("invalid Choice");

}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
/*Output
D:\javapro\Ass2>javac PlayerDemo.java
Note: PlayerDemo.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

D:\javapro\Ass2>java PlayerDemo
How many record accept u want-2
Enter the player code-101
Enter the player name-sachin
Enter the player runs-5700
Enter the player inning played-45
Enter the player no of time not out-85
Enter the player code-102
Enter the player name-dhoni
Enter the player runs-4500
Enter the player inning played-86
Enter the player no of time not out-45

1.Display average of all player
2.Display average run of single player
3.Exit
Enter ur Choice-1
Player name is    Average is
sachin               -142.0
dhoni                109.0

1.Display average of all player
2.Display average run of single player
3.Exit
Enter ur Choice-2
Enter player name whose display Average-sachin

Player name is    Average is
sachin               -142.0

1.Display average of all player
2.Display average run of single player
3.Exit
Enter ur Choice-3
*/