Jayawant Shikshan Prasarak Mandal
Group of Institute
Jayawantrao Sawant College of Engineering, Hadapsar
MCA DEPARTMENT
Java Programming
(2020 Pattern)
Prepared By:
Prince Vivek Kejriwal
MCA I Sem I
Academic year 2022-2023
Roll No : 15 Exam No.20000
Name-Prince Vivek Kejriwal Roll No- 15
Jayawant Shikshan Prasarak Mandal
Group of Institute
Institute Name: -JSCOE MCA Dept
Certificate
This is to certify that, Prince Vivek Kejriwal has successfully
completed the term work in Subject: Java Programming (2020 Pattern).
As per the syllabus of Pune University during the Academic year.
2022-2023 MCA I SEM I
Date: 25/03/2023
Subject Teacher Head of Department
University Exam No : 20000
Name-Prince Vivek Kejriwal Roll No- 15
INDEX
SUBMISSION REPORT
1 29/11/22 Write a program to display transpose of matrix.
2 Write a program to demonstrate the use of
06/12/22 a) Package
b) Interface
c) abstract class
3 Write a program to demonstrate
12/12/22 a) Operations performed on String
b) Use of StringBuilder Class
c) Use of StringTokenizer Class
4 15/12/22 Write a program to demonstrate user defined
exception.
5 Write a program to create a thread using
22/12/22 a) Extending the Thread class
b) Implementing Runnable interface.
6 30/12/22 Write a program to copy the contents of one file into
another file in reverse direction.
7 09/01/23 Write a program to display the contents of a file.
8 13/01/23 Write a program to Create Calculator by using AWT.
9 16/01/23 Write a program to create a menu using AWT / Swing.
10 19/01/23 Write a program to Create Log in form by using
AWT/Swing and JDBC.
11 23/01/23 Write a program to demonstrate operations performed
on ArrayList.
12 27/01/23 Write a program to demonstrate operations performed
on LinkedList.
Name-Prince Vivek Kejriwal Roll No- 15
13 30/01/23 Write a JDBC program to Perform CRUD Operations
on Oracle Database.(4 Different Programs)
14 02/02/23 Write a program to Connect Java Application with
MySQL Database.
15 03/02/23 Write a servlet program to implement Get and Post
methods.
16 07/02/23 Write a JSP program.
Name-Prince Vivek Kejriwal Roll No- 15
Q 1.Write a program to display transpose of matrix.
Ans:
package Programs;
import java.util.Scanner;
class TransposeMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter 9 elements of array A: ");
int A[][]=new int [3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
A[i][j]=sc.nextInt();
}
}
Sc.close();
System.out.println("\n Matrix of array A is: ");
for(int k=0;k<3;k++)
{
for(int l=0;l<3;l++)
{
System.out.print(" ");
System.out.print(A[k][l]);
}
System.out.println("\n");
}
System.out.println("\n A'=\n");
int C[][]=new int [3][3];
for(int m=0;m<3;m++)
{
for(int n=0;n<3;n++)
{
C[m][n]=A[n][m];
System.out.print(" ");
System.out.print(C[m][n]);
}
System.out.println("\n");
}
}
}}
Name-Prince Vivek Kejriwal Roll No- 15
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Q 2. Write a program to demonstrate the use of
a) Package
Ans:
//save as Example.java
package Package1;
public class Example
{
public int addInt(int a,int b)
{
return(a+b);
}
public void msg()
{
System.out.println("This is Example of Package");
}
}
//save as Practical2.java
package Programs;
import Package1.Example;
class PackageExample
{
public static void main(String[] args)
{
Example p1=new Example();
System.out.println(p1.addInt(10,20));
p1.msg();
Output:
Name-Prince Vivek Kejriwal Roll No- 15
b) Interface
Ans:
package Programs;
interface Interface1
{
String str1 = "This ";
}
interface Interface2
{
String str2 = "is ";
}
interface Interface3
{
String str3 = "Java ";
}
interface Interface4
{
String str4 = "World !!!";
}
class SubClass4 implements Interface1, Interface2, Interface3, Interface4
{
String str;
SubClass4()
{
str = str1.concat(str2).concat(str3).concat(str4);
}
void display()
{
System.out.println(str);
}
}
public class MainClass
{
public static void main(String[] args)
{
SubClass4 obj = new SubClass4();
obj.display();
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
c) abstract class
Ans:
package Programs;
abstract class Logic
{
public void java()
{
System.out.println("Java Programming");
}
public void example()
{
System.out.println("This is Example of abstract class");
}
}
public class AbstractClass extends Logic
{
public void example()
{
System.out.println("This is Example of abstract class");
}
public static void main(String[] args)
{
Logic d=new AbstractClass();
d.java();
d.example();
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Q 3. Write a program to demonstrate
a) Operations performed on String
Ans:
package Programs;
class StringOperations
{
public static void main(String args[])
{
String str1 = "Java", str2 = "Programmimg ";
System.out.println("\n str1=\""+str1+"\"str2=\""+str2+"\"\n");
System.out.println(" 1) str1 charAt(2): "+str1.charAt(2));
System.out.println(" 2) str1 & str2 concatenation: "+str1.concat(str2));
System.out.println(" 3) str1 & str2 is Equals? :"+str1.equals(str2));
System.out.println(" 4) str2 length: "+str2.length());
System.out.println(" 5) str2 replace * : "+str2.replace('m','*'));
System.out.println(" 6) str2 Is empty : "+str2.isEmpty());
System.out.println(" 7) str2 index of: "+str2.indexOf('m'));
System.out.println(" 8) str2 toLowerCase:"+str2.toLowerCase());
System.out.println(" 9) str2 toUpperCase:"+str2.toUpperCase());
System.out.println(" 10) str2 length after trim: "+str2.trim().length());
String str3 = "Hello java programer";
System.out.println("\n str3=\""+str3+"\"\n\n 11) str3 split: ");
String arr[] = str3.split("\\s");
for(String w:arr)
{
System.out.println(" "+w);
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
b) Use of StringBuilder Class
Ans:
package Programs;
class StringBuilderDemo
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("Hello");
sb.append("Java");
System.out.println("\n append: "+sb);
sb = new StringBuilder("Hello");
sb.insert(1,"java");
System.out.println("\n insert: "+sb);
sb = new StringBuilder("Hello");
sb.replace(1,3,"java");
System.out.println("\n replace: "+sb);
sb = new StringBuilder("Hello");
sb.delete(1,3);
System.out.println("\n delete: "+sb);
sb = new StringBuilder("Hello");
sb.reverse();
System.out.println("\n reverse: "+sb);
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
c) Use of StringTokenizer Class
Ans:
package Programs;
import java.util.StringTokenizer;
public class Tokenizer
{
public static void main(String args[])
{
System.out.println("This is String Tokenizer Pragram…”);
StringTokenizer st = new StringTokenizer("My name is Rajesh"," ");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Q 4. Write a program to demonstrate user defined exception.
Ans:
package Programs;
public class ExceptionTest
{
public static void main(String[] args)
{
int a[] = new int[2];
try
{
System.out.println("Access element three :" + a[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown :" + e);
}
finally
{
a[0] = 6;
System.out.println("First element value: " +a[0]);
System.out.println("The finally statement is executed");
}
}
}
Output:
Q 5. Write a program to create a thread using
Name-Prince Vivek Kejriwal Roll No- 15
a) Extending the Thread class
Ans:
package Programs;
class ExtendThread extends Thread
{
public void run()
{
System.out.println("This is Example of Extending Thread Class");
System.out.println("thread is running...");
}
public static void main(String args [ ] )
{
ExtendThread t1=new ExtendThread();
t1.start();
}
}
Output:
b) Implementing Runnable interface.
Name-Prince Vivek Kejriwal Roll No- 15
Ans:
package Programs;
class RunInterface implements Runnable
{
public void run()
{
System.out.println("This is Example of Implementing Runnable Interface");
System.out.println("thread is running...");
}
public static void main(String args [ ] )
{
RunInterface m1=new RunInterface();
Thread t1 =new Thread(m1);
t1.start();
}
}
Output:
Q 6. Write a program to copy the contents of one file into another file in reverse
direction.
Name-Prince Vivek Kejriwal Roll No- 15
Ans:
package Programs;
import java.io.*;
public class MirrorFile
{
public static void main(String [] args) throws IOException
{
RandomAccessFile inFile=null;
RandomAccessFile outFile=null;
try
{
int buf;
long index, posn;
inFile=new RandomAccessFile("d://rj/text1.txt","r");
outFile=new RandomAccessFile("d://rj/text2.txt","rw");
posn=inFile.length()-1;
for(index=posn; index>=0; index--)
{
inFile.seek(index);
buf=inFile.read();
outFile.write(buf);
}
outFile.close();
inFile.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output:
Q 7. Write a program to display the contents of a file.
Ans:
Name-Prince Vivek Kejriwal Roll No- 15
package Programs;
import java.io.*;
public class FileReaderDemo
{
public static void main(String args[]) throws Exception
{
FileReader fr = new FileReader("D://file.txt");
BufferedReader br = new BufferedReader(fr);
String s;
while((s=br.readLine())!=null)
{
System.out.println(s);
}
fr.close();
}
}
Output:
Q 8. Write a program to Create Calculator by using AWT.
Ans:
Name-Prince Vivek Kejriwal Roll No- 15
package Programs;
import java.awt.*;
import java.awt.event.*;
class MyCalc extends WindowAdapter implements ActionListener
{
Frame f;
Label l1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button badd,bsub,bmult,bdiv,bmod,bcalc,bclr,bpts,bneg,bback;
double xd;
double num1,num2,check;
MyCalc()
{
f= new Frame("MY CALCULATOR");
l1=new Label();
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(50,50,260,60);
b1=new Button("1");
b1.setBounds(50,340,50,50);
b2=new Button("2");
b2.setBounds(120,340,50,50);
b3=new Button("3");
b3.setBounds(190,340,50,50);
b4=new Button("4");
b4.setBounds(50,270,50,50);
b5=new Button("5");
b5.setBounds(120,270,50,50);
b6=new Button("6");
b6.setBounds(190,270,50,50);
b7=new Button("7");
b7.setBounds(50,200,50,50);
b8=new Button("8");
b8.setBounds(120,200,50,50);
b9=new Button("9");
b9.setBounds(190,200,50,50);
b0=new Button("0");
b0.setBounds(120,410,50,50);
bneg=new Button("+/-");
bneg.setBounds(50,410,50,50);
bpts=new Button(".");
bpts.setBounds(190,410,50,50);
bback=new Button("back");
bback.setBounds(120,130,50,50);
badd=new Button("+");
badd.setBounds(260,340,50,50);
bsub=new Button("-");
bsub.setBounds(260,270,50,50);
bmult=new Button("*");
bmult.setBounds(260,200,50,50);
bdiv=new Button("/");
bdiv.setBounds(260,130,50,50);
bmod=new Button("%");
bmod.setBounds(190,130,50,50);
bcalc=new Button("=");
bcalc.setBounds(245,410,65,50);
bclr=new Button("CE");
bclr.setBounds(50,130,65,50);
b1.addActionListener(this);
Name-Prince Vivek Kejriwal Roll No- 15
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bpts.addActionListener(this);
bneg.addActionListener(this);
bback.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this);
bmod.addActionListener(this);
bcalc.addActionListener(this);
bclr.addActionListener(this);
f.addWindowListener(this);
f.add(l1);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b0);
f.add(badd);
f.add(bsub);
f.add(bmod);
f.add(bmult);
f.add(bdiv);
f.add(bmod);
f.add(bcalc);
f.add(bclr);
f.add(bpts);
f.add(bneg);
f.add(bback);
f.setSize(360,500);
f.setLayout(null);
f.setVisible(true);
}
public void windowClosing(WindowEvent e)
{
f.dispose();
}
public void actionPerformed(ActionEvent e)
{
String z,zt;
if(e.getSource()==b1)
{
zt=l1.getText();
z=zt+"1";
l1.setText(z);
Name-Prince Vivek Kejriwal Roll No- 15
}
if(e.getSource()==b2)
{
zt=l1.getText();
z=zt+"2";
l1.setText(z);
}
if(e.getSource()==b3)
{
zt=l1.getText();
z=zt+"3";
l1.setText(z);
}
if(e.getSource()==b4)
{
zt=l1.getText();
z=zt+"4";
l1.setText(z);
}
if(e.getSource()==b5)
{
zt=l1.getText();
z=zt+"5";
l1.setText(z);
}
if(e.getSource()==b6)
{
zt=l1.getText();
z=zt+"6";
l1.setText(z);
}
if(e.getSource()==b7)
{
zt=l1.getText();
z=zt+"7";
l1.setText(z);
}
if(e.getSource()==b8)
{
zt=l1.getText();
z=zt+"8";
l1.setText(z);
}
if(e.getSource()==b9)
{
zt=l1.getText();
z=zt+"9";
l1.setText(z);
}
if(e.getSource()==b0)
{
zt=l1.getText();
z=zt+"0";
l1.setText(z);
}
if(e.getSource()==bpts) //ADD DECIMAL PTS
{
zt=l1.getText();
z=zt+".";
l1.setText(z);
}
if(e.getSource()==bneg) //FOR NEGATIVE
{
zt=l1.getText();
Name-Prince Vivek Kejriwal Roll No- 15
z="-"+zt;
l1.setText(z);
}
if(e.getSource()==bback) // FOR BACKSPACE
{
zt=l1.getText();
try
{
z=zt.substring(0, zt.length()-1);
}
catch(StringIndexOutOfBoundsException f)
{
return;
}
l1.setText(z);
}
//AIRTHMETIC BUTTON
if(e.getSource()==badd) //FOR ADDITION
{
try
{
num1=Double.parseDouble(l1.getText());
}
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=1;
}
if(e.getSource()==bsub) //FOR SUBTRACTION
{
try
{
num1=Double.parseDouble(l1.getText());
}
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=2;
}
if(e.getSource()==bmult) //FOR MULTIPLICATION
{
try
{
num1=Double.parseDouble(l1.getText());
}
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=3;
}
if(e.getSource()==bdiv) //FOR DIVISION
{
Name-Prince Vivek Kejriwal Roll No- 15
try
{
num1=Double.parseDouble(l1.getText());
}
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=4;
}
if(e.getSource()==bmod) //FOR MOD/REMAINDER
{
try
{
num1=Double.parseDouble(l1.getText());
}
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=5;
}
//RESULT BUTTON
if(e.getSource()==bcalc)
{
try
{
num2=Double.parseDouble(l1.getText());
}
catch(Exception f)
{
l1.setText("ENTER NUMBER FIRST ");
return;
}
if(check==1)
xd =num1+num2;
if(check==2)
xd =num1-num2;
if(check==3)
xd =num1*num2;
if(check==4)
xd =num1/num2;
if(check==5)
xd =num1%num2;
l1.setText(String.valueOf(xd));
}
//FOR CLEARING THE LABEL and Memory
if(e.getSource()==bclr)
{
num1=0;
num2=0;
check=0;
xd=0;
z="";
l1.setText(z);
}
}
Name-Prince Vivek Kejriwal Roll No- 15
public static void main(String args[])
{
new MyCalc();
}
}
Output:
Q 9. Write a program to create a menu using AWT / Swing.
Name-Prince Vivek Kejriwal Roll No- 15
Ans:
package Programs;
import javax.swing.*;
import java.awt.event.*;
public class MenuExample implements ActionListener
{
JFrame f;
JMenuBar mb;
JMenu file,edit,help;
JMenuItem cut,copy,paste,selectAll;
JTextArea ta;
MenuExample()
{
f= new JFrame();
cut= new JMenuItem("cut");
copy= new JMenuItem("copy");
paste= new JMenuItem("paste");
selectAll= new JMenuItem("selectAll");
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
mb= new JMenuBar();
file= new JMenu("File");
edit= new JMenu("Edit");
help= new JMenu("Help");
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(selectAll);
mb.add(file);
mb.add(edit);
mb.add(help);
ta= new JTextArea();
ta.setBounds(5,5,360,320);
f.add(mb);
f.add(ta);
f.setJMenuBar(mb);
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Name-Prince Vivek Kejriwal Roll No- 15
if(e.getSource()==cut)
ta.cut();
if(e.getSource()==paste)
ta.paste();
if(e.getSource()==copy)
ta.copy();
if(e.getSource()==selectAll)
ta.selectAll();
}
public static void main(String args[])
{
new MenuExample();
}
Output:
Q 10. Write a program to Create Log in form by using AWT/Swing and JDBC.
Name-Prince Vivek Kejriwal Roll No- 15
Ans:
package Programs;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.EmptyBorder;
public class UserLogin extends JFrame
{
private static final long serialVersionUID = 1L;
private JTextField textField;
private JPasswordField passwordField;
private JButton btnNewButton;
private JLabel label;
private JPanel contentPane;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
UserLogin frame = new UserLogin();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
);
}
public UserLogin()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(450, 190, 1014, 597);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Login");
lblNewLabel.setForeground(Color.BLACK);
lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46));
lblNewLabel.setBounds(423, 13, 273, 93);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 32));
textField.setBounds(481, 170, 281, 68);
contentPane.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32));
Name-Prince Vivek Kejriwal Roll No- 15
passwordField.setBounds(481, 286, 281, 68);
contentPane.add(passwordField);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBackground(Color.BLACK);
lblUsername.setForeground(Color.BLACK);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 31));
lblUsername.setBounds(250, 166, 193, 52);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setForeground(Color.BLACK);
lblPassword.setBackground(Color.CYAN);
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 31));
lblPassword.setBounds(250, 286, 193, 52);
contentPane.add(lblPassword);
btnNewButton = new JButton("Login");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnNewButton.setBounds(545, 392, 162, 73);
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String userName = textField.getText();
String password = passwordField.getText();
try {
Connection connection = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/logintable","root", "root");
PreparedStatement st = (PreparedStatement) connection
.prepareStatement("Select name, password from student where name=?
and password=?");
st.setString(1, userName);
st.setString(2, password);
ResultSet rs = st.executeQuery();
if (rs.next())
{
dispose();
UserHome ah = new UserHome();
ah.setTitle("Welcome");
ah.setVisible(true);
JOptionPane.showMessageDialog(btnNewButton, "You have successfully
logged in");
}
else
{
JOptionPane.showMessageDialog(btnNewButton, "Wrong Username &
Password");
}
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}
} );
contentPane.add(btnNewButton);
label = new JLabel("");
label.setBounds(0, 0, 1008, 562);
Name-Prince Vivek Kejriwal Roll No- 15
contentPane.add(label);
}
}
// UserHome Screen
package Programs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserHome extends JFrame
{
UserHome()
{
JFrame f1=new JFrame("jscoe/Student-Registration-Form");
JLabel l = new JLabel("Welcome");
l.setForeground(Color.BLACK);
l.setFont(new Font("Times New Roman", Font.PLAIN, 46));
l.setBounds(120,50, 273, 93);
f1.add(l);
f1.setSize(600,450);
f1.setLayout(null);
f1.setVisible(true);
}
public static void main(String[] args)
{
new UserHome();
}
}
Name-Prince Vivek Kejriwal Roll No- 15
Output:
Home Screen
Name-Prince Vivek Kejriwal Roll No- 15
Q 11. Write a program to demonstrate operations performed on ArrayList.
Ans:
package practical;
import java.util.*;
public class ArrayListDemo
{
public static void main(String args[])
{
ArrayList<String> list=new ArrayList<String>();
list.add("Ravi");
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
Iterator itr=list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Q 12. Write a program to demonstrate operations performed on LinkedList.
Ans:
package practical;
import java.util.*;
public class LinkedListDemo
{
public static void main(String args[])
{
LinkedList<String> al=new LinkedList<String>();
al.add("Rajesh");
al.add("Amit");
al.add("Gagan");
al.add("Mayur");
al.add("Onkar");
System.out.println("Size of the Linkedlist is: "+al.size());
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Q 13. Write a JDBC program to Perform CRUD Operations on Oracle Database.
a) Create Operation:
Ans:
package Programs;
import java.sql.*;
public class CreateDemo
{
static final String URL = "jdbc:mysql://localhost/mca";
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args)
{
try(Connection conn = DriverManager.getConnection(URL, USER, PASS);
Statement stmt = conn.createStatement();)
{
String sql = "CREATE TABLE STUDENT " +
"(Roll INTEGER not NULL, " +
" Name VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( Roll ))";
stmt.executeUpdate(sql);
System.out.println("Created table STUDENT in given mca");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
b) Insert Records Operation:
Ans:
package Programs;
import java.sql.*;
public class InsertDemo
{
static final String URL = "jdbc:mysql://localhost/mca";
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args)
{
try(Connection conn = DriverManager.getConnection(URL, USER, PASS);
Statement stmt = conn.createStatement();)
{
System.out.println("Inserting records into the table...");
String sql = "INSERT INTO STUDENT VALUES (43, 'RAJESH',22)";
stmt.executeUpdate(sql);
sql = "INSERT INTO STUDENT VALUES (05, 'AMIT',22)";
stmt.executeUpdate(sql);
sql = "INSERT INTO STUDENT VALUES (46, 'ONKAR',21)";
stmt.executeUpdate(sql);
sql = "INSERT INTO STUDENT VALUES (21, 'MAYUR',23)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
c) Update Operation:
Ans:
package Programs;
import java.sql.*;
public class UpdateDemo
{
static final String URL = "jdbc:mysql://localhost/mca";
static final String USER = "root";
static final String PASS = "root";
static final String QUERY = "SELECT Roll, Name, age FROM STUDENT";
public static void main(String[] args)
{
try(Connection conn = DriverManager.getConnection(URL, USER, PASS);
Statement stmt = conn.createStatement();)
{
String sql = "UPDATE STUDENT " +
"SET age = 21 WHERE Roll = 05";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery(QUERY);
System.out.println("Record Updated");
while(rs.next())
{
System.out.println();
System.out.print("Roll: " + rs.getInt("Roll"));
System.out.print(", Name: " + rs.getString("Name"));
System.out.print(", Age: " + rs.getInt("age"));
}
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
d) Delete Operation:
Ans:
package Programs;
import java.sql.*;
public class DeleteDemo
{
static final String URL = "jdbc:mysql://localhost/mca";
static final String USER = "root";
static final String PASS = "root";
static final String QUERY = "SELECT Roll, Name, age FROM Student";
public static void main(String[] args)
{
try(Connection conn = DriverManager.getConnection(URL, USER, PASS);
Statement stmt = conn.createStatement();)
{
String sql = "DELETE FROM STUDENT " + "WHERE Roll = 21";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery(QUERY);
System.out.println("Record Deleted");
while(rs.next())
{
System.out.println();
System.out.print("Roll: " + rs.getInt("Roll"));
System.out.print(", Name: " + rs.getString("Name"));
System.out.print(", Age: " + rs.getInt("age"));
}
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Q 14. Write a program to Connect Java Application with MySQL Database.
Ans:
package Programs;
import java.sql.*;
public class JDBCconnect
{
static final String URL = "jdbc:mysql://localhost/mca";
static final String USER = "root";
static final String PASS = "root";
static final String QUERY = "SELECT Roll, Name, age FROM STUDENT";
public static void main(String[] args)
{
try(Connection conn = DriverManager.getConnection(URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);)
{
System.out.println("connected to Mysql Database");
while(rs.next())
{
System.out.println();
System.out.print("Roll: " + rs.getInt("Roll"));
System.out.print(", Name: " + rs.getString("Name"));
System.out.print(", Age: " + rs.getInt("age"));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Q 15. Write a servlet program to implement Get and Post methods.
Ans:
Html file (index.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div>
<form action='CheckVoterServlet' method="POST">
Person Name:: <input type="text" name="pname">
<br><br>
Person Age:: <input type="password" name="page">
<br><br>
<input type="submit" value="Check Voting Eligibility">
</div>
</body>
</html>
Web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">
<display-name>CheckVoterServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>CheckVoterServlet</display-name>
<servlet-name>CheckVoterServlet</servlet-name>
<servlet-class>servlet.CheckVoterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckVoterServlet</servlet-name>
<url-pattern>/CheckVoterServlet</url-pattern>
</servlet-mapping>
</web-app>
Name-Prince Vivek Kejriwal Roll No- 15
Java file (CheckVoterServlet.java)
package servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Servlet implementation class CheckVoterServlet
*/
public class CheckVoterServlet extends HttpServlet
{
// Method to handle POST method request
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// declare variables
PrintWriter pw = null;
String name = null;
String tage = null;
int age = 0;
// set content type
res.setContentType("text/html");
// get PrintWriter object
pw = res.getWriter();
// get form data (from req parameter)
name = req.getParameter("pname");
tage = req.getParameter("page");
// convert String value to int
age = Integer.parseInt(tage);
// check age
pw.println("<h1 style='text-align: center; color:blue'>"
+ "Hello "+ name + "</h1>");
if(age < 18)
{
pw.println("<h2 style='text-align: center; color:red'>"
+"You are not eligible for voting.</h2>"
+"<h3 style='text-align: center; color:green'>"
+"Please wait for more " + (18-age) + " years.<br>"
+" Thank You.<h3>"
);
}
else
{
pw.println("<h2 style='text-align: center; color:green'>"
+"You are eligible for voting.</h2>"
+"<h3 style='text-align: center'>"
+"Thank You.<h3>"
);
}
Name-Prince Vivek Kejriwal Roll No- 15
// close stream
pw.close();
}
// Method to handle GET method request
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// call doPost() method
doPost(req, res);
}
}
Output:
Q 16. Write a JSP program.
Name-Prince Vivek Kejriwal Roll No- 15
Ans:
Web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">
<display-name>JspDemoProgram</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
Jsp file (index.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Lucky_Draw</title>
</head>
<body bgcolor="cyan">
<form action="EnterluckyDraw" method="post">
<h2>
<b>Enter Your Details to win a Lucky Draw</b>
</h2>
<br>
<br>
<label for="name">Full Name:</label>
<input type="text" id="name" name="user_name">
<br>
<br>
<br>
<label for="mail">___E-mail:</label>
<input type="email" id="mail" name="user_email">
<br>
<br>
<br>
<label for="msg">Peace msg:</label>
<textarea id="msg" name="user_message"></textarea>
</form>
</body>
</html>
Output:
Name-Prince Vivek Kejriwal Roll No- 15
Name-Prince Vivek Kejriwal Roll No- 15