0% found this document useful (0 votes)
66 views

Java

The document discusses a Java program that implements CRUD (create, read, update, delete) operations on student details stored in an SQL database. It establishes a database connection, then provides methods to add, update, delete, and search student records by prompting the user to enter details. The main method displays a menu and calls the appropriate method based on the user's choice. This allows basic management and querying of student data stored in the database.

Uploaded by

Jemes Bond
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Java

The document discusses a Java program that implements CRUD (create, read, update, delete) operations on student details stored in an SQL database. It establishes a database connection, then provides methods to add, update, delete, and search student records by prompting the user to enter details. The main method displays a menu and calls the appropriate method based on the user's choice. This allows basic management and querying of student data stored in the database.

Uploaded by

Jemes Bond
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

/********** Java Program To Print Reverse Of A Number ************/

class Reverse
{
public static void main(String args[])
{
int i,s=0,a = 120;
int no[] = new int[10];
System.out.print("Original Number : "+a);
for(i=0;i< 10;i++)
{
if(a!= '\0')
{
no[i] = a%10;
a = a / 10;
s++;
}
else break;
}
System.out.print("\n\nReversed Number : ");
for(i=s;i>0;i--)
{
System.out.print(no[i]);
}
}
}
/************ OUTPUT ************
* Original Number : 120
* Reversed Number : 021 */

/******* Java Implementation of Matrix Operation Using Arrays *******/


class Matrix
{
public static void main(String args[])
{
int i,j,k;
int mat1 [][]={ {10,11,12}, {13,14,15}, {16,17,18} };
int mat2 [][]={ {1,2,3}, {4,5,6}, {7,8,9} };
System.out.println("Operation ON Matrices \n1.Addition \n");
int sum [][] = new int [3][3];
for(i=0;i <3;i++)
{
for(j=0;j< 3;j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
System.out.print("\t" + sum[i][j]);
}
System.out.println("\t");
}
System.out.println("2.Subtraction\n");
int diff[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
diff [i][j] = mat1[i][j] - mat2[i][j];
System.out.print("\t"+ diff[i][j]);
}
System.out.println("\t");
}
System.out.println("3.Multiplication\n");
int prod[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
prod[i][j] = 0;
{
for(k=0;k< 3;k++)
prod[i][j] = prod[i][j]+mat1[i][k]*mat2[k][j];
}
System.out.print("\t"+ prod[i][j]);
}
System.out.println("\t");
}
}
}
/************* OUTPUT ***************
Operation ON Matrices
1.Addition
11 13 15
17 19 21
23 25 27
2.Subtraction
9 9 9
9 9 9
9 9 9
3.Multiplication
138 171 204
174 216 258
210 261 312 */

/* Java program on Arrays - Bubble sort on numbers


*/
import java.io.*;
class Bubble
{
public static void main(String args[]) throws IOException
{
int num;
System.out.println("Program to demonstrate bubble sort");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of elements to be entered <10");
num=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers to be sorted");
int arr[]=new int[10];
for(int i=0;i< num;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("The number you have entered:");
for(int i=0;i< num;i++)
{
System.out.println(arr[i]);
}
for(int i=0;i< num;i++)
{
for(int j=i+1;j< num;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("The sorted numbers are");
for(int i=0;i< num;i++)
{
System.out.println(arr[i]);
}
}
}

/********* Output **********


Program to demonstrate bubble sort
Enter the number of elements to be entered <10
5
Enter the numbers to be sorted
8
65
14
23
2
The number you have entered:
8
65
14
23
2
The sorted numbers are
2
8
14
23
65
*/
***** Java Program to read a file and display it on the screen */
import java.io.*;
public class LioFile
{
public static void main(String args[])
{
try
{
FileReader fr=new FileReader("LioFile.java");
BufferedReader br=new BufferedReader(fr);
String str=" ";
String s1 =" ";
System.out.println(" s1 is "+s1.equals("ass"));
while((str=br.readLine())!=null)
{
System.out.println(str);
}
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
/**** Output ***
s1 is false
import java.io.*;
public class J7
{
public static void main(String args[])
{
try
{
FileReader fr=new FileReader("J7.java");
BufferedReader br=new BufferedReader(fr);
String str=" ";
String s1 =" ";
System.out.println(" s1 is "+s1.equals("ass"));
while((str=br.readLine())!=null)
{
System.out.println(str);
}
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
*/

/* Function Overloading program in java. The program overloads the area() functi
on and calculates the area for square, rectangle and triangle */
class overloading
{
double area,root;
//Area Of Square
public void Area(double a)
{
area = a*a;
System.out.println("The Area Of Square : " +area);
}
// Area of rectangle
public void Area(double a,double b)
{
area= a*b;
System.out.println("The Area Of Rectangle : " +area);
}
// Aera Of Triangle
public void Area(double a,double b,double c)
{
double s= (a+b+c);
root = s*(s-a)*(s-b)*(s-c);
area = Math.sqrt(root);
System.out.println("The Area Of Triangle : " +area);
}
public static void main(String args[])
{
overloading ar = new overloading();
ar.Area(5.65);
ar.Area(5.32,43.2);
ar.Area(4,7,7);
}
}
/* Output
The Area Of Square : 31.922500000000003
The Area Of Rectangle : 229.82400000000004
The Area Of Triangle : 174.61958653026298 */

/* Simple Type casting program */


class cast
{
public static void main(String args[])
{
double a=5.35, b=55.6;
int rem;
rem = (int)a% (int)b;
System.out.println("Remainder : "+rem);
}
}
/* Output
Remainder : 5 */

Program in java for inserting, recording, deleting, editing and searching studen
t details stored in the SQL database.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

class studentdetails
{
Statement stmt;
String strSql;
Connection con;
BufferedReader bufferObj;
studentdetails (){
bufferObj=new BufferedReader(new InputStreamReader(System.in));
}
void establishConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}
try
{
String url="jdbc:odbc:new_dsn";
con=DriverManager.getConnection(url,"sa","");
stmt=con.createStatement();
}
catch(SQLException ce)
{
System.out.println("Error... "+ce);
}
}

void add()
{
try
{
System.out.println("\nEnter Roll Number: ");
String rollno=bufferObj.readLine();
System.out.println("\nEnter Students Name: ");
String name=bufferObj.readLine();
System.out.println("\nEnter Course Name");
String course=bufferObj.readLine();
strSql="Insert into Student values(" +rollno+",'"+name+"',' "+course+" ')";
stmt.executeUpdate(strSql);
System.out.println("\nRecords Successfully Added!");
System.out.println();
}
catch(SQLException ce)
{
System.out.println("Error... "+ce);
}
catch(Exception e)
{
System.out.println(e);
}
}
void update()
{
try
{
System.out.println("\nEnter Roll Number whose "+"record should be updated: ");
String roll=bufferObj.readLine();
System.out.println("Enter Students Name to be Modified: ");
String name=bufferObj.readLine();
System.out.println("Enter course name to be modified: ");
String course=bufferObj.readLine();
strSql="update Student set name=' "+name+" ',course='"+course+"' where rollno='"
+roll+"'";
stmt.executeUpdate(strSql);
System.out.println("\nRecord Successfully Modified !");
System.out.println();
}
catch(SQLException ioe)
{
System.out.println(ioe);
}
catch(Exception er)
{
System.out.println(er);
}
}
void delete()
{
try
{
System.out.println("Enter Students Name to be Deleted :") ;
String name=bufferObj.readLine();
strSql="Delete from Student where rtrim(name) like '"+name+ "'";
stmt.executeUpdate(strSql);
System.out.println("Record Successfully Deleted !");
System.out.println();
}
catch(SQLException et)
{
System.out.println("Error in Deletion.... "+et);
}
catch(Exception ty)
{
System.out.println("Error.... "+ty);
}
}
void search()
{
try
{
System.out.println("Enter Roll Number "+"whose record should be searched: ");
int roll=Integer.parseInt(bufferObj.readLine());
System.out.println("Enter Students Name to be Searched");
String name=bufferObj.readLine();
strSql="select * from student where name like'"+name;
strSql=strSql+"' and Rollno="+roll;
ResultSet rs=stmt.executeQuery(strSql);
if(!rs.next())
{
System.out.println("Name");
System.out.println(rs.getString(1)+"\t");
System.out.println("Roll Number");
System.out.println(rs.getInt(2)+"\t");
System.out.println("Course");
System.out.println(rs.getString(3)+"\n");
}
}
catch(SQLException er)
{
System.out.println(er);
}
catch(Exception t)
{
System.out.println(t);
}
}
public void menudisplay() throws IOException
{
char choice;
while(true)
{
System.out.println();
System.out.println("1. Add a Record");
System.out.println("2. Modify Record");
System.out.println("3. Delete a Record");
System.out.println("4. Search a Record");
System.out.println("5. Exit");
System.out.println("Enter your choice....:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
choice=(char) br.read();
switch(choice)
{
case '1' :
System.out.println("Adding a Record........");
add();
break;
case '2' :
System.out.println("Updating a Record........");
update();
break;
case '3' :
System.out.println("Deleting a Record........");
delete();
break;
case '4' :
System.out.println("Searching a Record........");
search();
break;
case '5' :
System.exit(0);
break;
default :
System.out.println("Adding a Record........");
add();
break;
}
}
}
}
class student
{
public static void main(String[] args) throws IOException
{
studentdetails stud = new studentdetails();
stud.establishConnection();
stud.menudisplay();
}
}
/*Write a program to find SUM AND PRODUCT of a given Digit. */
class Sum_Product_ofDigit{
public static void main(String args[]){
int num = Integer.parseInt(args[0]); //taking value as comma
nd line argument.
int temp = num,result=0;
//Logic for sum of digit
while(temp>0){
result = result + temp;
temp--;
}
System.out.println("Sum of Digit for "+num+" is : "+result);
//Logic for product of digit
temp = num;
result = 1;
while(temp > 0){
result = result * temp;
temp--;
}
System.out.println("Product of Digit for "+num+" is : "+result);
}
}

/*Write a program to Reverse a given no. */


class Reverse{
public static void main(String args[]){
int num = Integer.parseInt(args[0]); //take argument as
command line
int remainder, result=0;
while(num>0){
remainder = num%10;
result = result * 10 + remainder;
num = num/10;
}
System.out.println("Reverse number is : "+result);
}
}

import java.io.*;
public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
System.out.println("Number: ");
System.out.println(" "+ num);
for (int i=0; i<=num; i++){
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
System.out.println("After reversing the number: "+ " ");
System.out.println(" "+ rev);
if(n == rev){
System.out.print("Number is palindrome!");
}
else{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e){
System.out.println("Out of range!");
}
}
}

/* Write a program to Swap the values */


class Swap{
public static void main(String args[]){
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
System.out.println("\n***Before Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
//Swap logic
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("\n***After Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
}
}
Program 14
/* Write a program to convert given no. of days into months and days.
(Assume that each month is of 30 days)
Example :
Input - 69
Output - 69 days = 2 Month and 9 days */
class DayMonthDemo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int days = num%30;
int month = num/30;
System.out.println(num+" days = "+month+" Month and "+days+" days");
}
}
Program 15
/*Write a program to generate a Triangle.
eg:
1
2 2
3 3 3
4 4 4 4 and so on as per user given number */
class Triangle{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
for(int i=1;i<=num;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+i+" ");
}
System.out.print("\n");
}
}
}
Program 16
/* Write a program to Display Invert Triangle.
Example:
Input - 5
Output :
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
*/
class InvertTriangle{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
while(num > 0){
for(int j=1;j<=num;j++){
System.out.print(" "+num+" ");
}
System.out.print("\n");
num--;
}
}
}
Program 17
/*Write a program to find whether given no. is Armstrong or not.
Example :
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //use to check at last time
int check=0,remainder;
while(num > 0){
remainder = num % 10;
check = check + (int)Math.pow(remainder,3);
num = num / 10;
}
if(check == n)
System.out.println(n+" is an Armstrong Number");
else
System.out.println(n+" is not a Armstrong Number");
}
}
Program 18
/* Write a program to Find whether number is Prime or Not. */
class PrimeNo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
{
System.out.println(num+" is not a Prime Number");
flag = 1;
break;
}
}
if(flag==0)
System.out.println(num+" is a Prime Number");
}
}
Program 19
/* Write a program to find whether no. is palindrome or not.
Example :
Input - 12521 is a palindrome no.
Input - 12345 is not a palindrome no. */
class Palindrome{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //used at last time check
int reverse=0,remainder;
while(num > 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
if(reverse == n)
System.out.println(n+" is a Palindrome Number");
else
System.out.println(n+" is not a Palindrome Number");
}
}

You might also like