jspmwakadcodes
Collections
SET A
-
Write a java program to accept names of ‘n’ cities, insert same into array list collection and display the contents of same array list, also remove all these elements.
import java.util.*;
class ass1seta1
{
public static void main(String[] args)
{
ArrayList A1=new ArrayList();
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter n:");
n=sc.nextInt();
System.out.println("\nEnter Name of Cities:");
for(int i=0;i<n;i++)
{
A1.add(sc.next());
}
System.out.println("\nCities are:");
System.out.println(A1);
A1.removeAll(A1);
System.out.println("\nAfter Removing:");
System.out.println(A1);
}
} -
Write a java program to read ‘n’ names of your friends, store it into linked list, also display contents of the same.
import java.util.*;
public class ass1seta2
{
public static void main(String[] args)
{
LinkedList<String>a1=new LinkedList<String>();
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter n:");
n=sc.nextInt();
System.out.println("\nEnter Names:");
for(int i=0;i<n;i++)
{
a1.add(sc.next());
}
System.out.println("\nName of Friends are:");
Iterator<String>itr=a1.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
} -
Write a program to create a new tree set, add some colors (string) and print out the tree set.
import java.util.Set;
import java.util.TreeSet;
public class ass1seta3
{
public static void main(String[] args)
{
Set ts=new TreeSet();
ts.add("Red");
ts.add("Pink");
ts.add("Blue");
ts.add("Red");
System.out.println("Members from TreeSet="+ts);
}
} -
Create the hash table that will maintain the mobile number and student name. Display the contact list.
import java.util.*;
class ass1seta4
{
public static void main(String[] args)
{
Hashtable<String,String>hashtable=new Hashtable<String,String>();
hashtable.put("1234567891","Ram");
hashtable.put("9955557110","Gayatri");
hashtable.put("9423094234","Soham");
System.out.println(hashtable);
}
}
SET B
-
Accept ‘n’ integers from the user. Store and display integers in sorted order having proper collection class. The collection should not accept duplicate elements.
import java.util.HashSet;
public class ass1setb1
{
public static void main(String[] args)
{
HashSet hs = new HashSet(5, 0.5f);
System.out.println(hs.add("1"));
System.out.println(hs.add("2"));
System.out.println(hs.add("3"));
System.out.println(hs.add("4"));
System.out.println(hs.add("5"));
System.out.println(hs);
Boolean b = hs.add("1");
System.out.println("Duplicate item allowed = " + b);
System.out.println(hs);
}
} -
Write a program to sort HashMap by keys and display the details before sorting and after sorting.
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Iterator;
public class ass1setb2
{
public static void main(String args[])
{
//implementation of HashMap
HashMap<Integer, String> hm=new HashMap<Integer, String>();
//addding keys and values to HashMap
hm.put(23, "Yash");
hm.put(17, "Arun");
hm.put(15, "Swarit");
hm.put(9, "Neelesh");
Iterator <Integer> it = hm.keySet().iterator();
System.out.println("Before Sorting");
while(it.hasNext())
{
int key=(int)it.next();
System.out.println("key:"+key+"\tvalue:"+hm.get(key));
}
System.out.println("\n");
Map<Integer, String> map=new HashMap<Integer, String>();
System.out.println("After Sorting");
//using TreeMap constructor to sort the HashMap
TreeMap<Integer,String> tm=new TreeMap<Integer,String> (hm);
Iterator itr=tm.keySet().iterator();
while(itr.hasNext())
{
int key=(int)itr.next();
System.out.println("key:"+key+"\tvalue:"+hm.get(key));
}
}
} -
Write a program that loads names and phone numbers from a text file where the data is organized as one line per record and each field in a record are separated by a tab (\t).it takes a name or phone number as input and prints the corresponding other value from the hash table (hint: use hash tables)
import java.util.;
import java.io.;
class ass1setb3
{
public static void main(String args[])throws IOException
{
File f=new File("text.txt");
Scanner sc=new Scanner(f);
Hashtable<String,String>ht=new Hashtable<String,String>();
for(int i=0;i<3;i++)
{
ht.put(sc.next(),sc.next());
}
System.out.println(ht);
}
}
Multithreading
SET A
-
Program to define a thread for printing text on output screen for ‘n’ number of times. Create 3 threads and run them. Pass the text ‘n’ parameters to the thread constructor. Example:
i. First thread prints “COVID19” 10 times.
ii. Second thread prints “LOCKDOWN2020” 20 times
iii. Third thread prints “VACCINATED2021” 30 times
import java.io.*;
class MyThread extends Thread
{
String message;
int n;
MyThread (String message,int n)
{
this.message = message;
this.n=n;
}
public void run()
{
try
{
for(int i=1; i<=n; i++)
{
System.out.println(message + "-" + i);
Thread.sleep(500);
}} catch(InterruptedException ie) { }
}
}
public class ass2seta1
{
public static void main( String[] args)
{
MyThread t1 = new MyThread("COVID19",10);
MyThread t2 = new MyThread("LOCKDOWN2020",20);
MyThread t3 = new MyThread("VACCINATION2021",30);
System.out.println(t1);
t1.start();
System.out.println(t2);
t2.start();
System.out.println(t3);
t3.start();
}
} -
Write a program in which thread sleep for 6 sec in the loop in reverse order from 100 to 1 and change the name of thread.
class MyThread extends Thread
{
String name;
MyThread(String name)
{
this.name=name;
}
public void run()
{
try
{
System.out.println("Reverse Sequence");
for(int i=100;i>+1;i--)
{
System.out.println(i);
Thread.sleep(6000);
}
}
catch(InterruptedException ie)
{}
}
}
public class ass2seta2
{
public static void main(String[] args)
{
MyThread t1=new MyThread("Reverse");
System.out.println(t1);
t1.start();
}
} -
Write a program to solve producer consumer problem in which a producer produces a value and consumer consume the value before producer generate the next value. (Hint: use thread synchronization)
import java.util.LinkedList;
import java.io.;
import java.lang.String.;
public class ass2seta3
{
public static void main(String args[])throws InterruptedException
{
final PC pc=new PC();
Thread t1=new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
Thread t2=new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.consumer();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
public static class PC
{
LinkedList<Integer> list=new LinkedList<>();
int capacity=2;
public void produce()throws InterruptedException
{
int value=0;
while(true)
{
synchronized(this)
{
while(list.size()==capacity)
wait();
System.out.println("Producer produced="+value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
public void consumer()throws InterruptedException
{
while(true)
{
synchronized(this)
{
while(list.size()==0)
wait();
int val=list.removeFirst();
System.out.println("Consumer consumed="+val);
notify();
Thread.sleep(1000);
}
}
}
}
}
SET B
-
Write a program to calculate the sum and average of an array of 1000 integers (generated randomly) using 10 threads. Each thread calculates the sum of 100 integers. Use these values to calculate average. [Use join method ].
import java.util.*;
public class ass2setb1 extends Thread
{
int array[];
int sum=0;
public int getSum()
{
return sum;}
public ass2setb1()
{
Random r=new Random();
array=new int[100];
for(int i=0;i<array.length;i++)array[i]=r.nextInt(1000);}
public static void main(String[] args) throws InterruptedException
{
ass2setb1 t[]=new ass2setb1[10];
int sum=0;
for(int i=0;i<t.length;i++)
{
t[i]=new ass2setb1();
t[i].start();
t[i].join();
t[i].sleep(500);
System.out.println("Thread Started:"+i);} for(int i=0;i<t.length;i++) { sum+=t[i].getSum(); } System.out.println("Sum of 1000 integer is:\t"+sum); float f=sum/100; System.out.println("Avarage of 1000 integer is:\t"+f);
}
@Override
public void run()
{
int i=0;
while(i<array.length)
{
sum+=array[i];
i++;}
}
} -
Write a program for a simple search engine. Accept a string to be searched. Search for the string in all text files in the current folder. Use a separate thread for each file. The result should display the filename, line number where the string is found.
import java.io.*;
public class ass2setb2 extends Thread
{
File f1;
String fname;
static String str;
String line;
LineNumberReader reader = null;
ass2setb2(String fname)
{
this.fname=fname;
f1=new File(fname);
}
public void run()
{
try
{
FileReader fr=new FileReader(f1);
reader=new LineNumberReader(fr);
while((line=reader.readLine())!=null)
{
if(line.indexOf(str)!=-1)
{
System.out.println("string found in "+fname+"at "+reader.getLineNumber()+"line");
stop();
}
}
}
catch(Exception e)
{
}
}
public static void main(String[] args) throws IOException
{
Thread t[]=new Thread[20];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String to search");
str=br.readLine();
FilenameFilter filter = new FilenameFilter()
{
public boolean accept(File file, String name)
{
if (name.endsWith(".txt"))
{
return true;
}
else
{
return false;
}
}
};
File dir1 = new File(".");
File[] files = dir1.listFiles(filter);
if (files.length == 0)
{
System.out.println("no files available with this extension");
}
else
{
for(int i=0;i<files.length;i++)
{
for (File aFile : files)
{
t[i]=new ass2setb2(aFile.getName());
t[i].start();
}
}
}
}
}
-
Write a program that implements a multi-thread application that has three threads. First thread generates random integer every 1 second and if the value is even, second thread computes the square of the number and prints. If the value is odd, the third thread will print the value of cube of the number.
import java.util.;
class square extends Thread
{
int n;
square(int n)
{
this.n=n;
}
public void run()
{
int sqr=nn;
System.out.println("square of"+n+"="+sqr);
}
}
class cube extends Thread
{
int n;
cube(int n)
{
this.n=n;
}
public void run()
{
int cube=nnn;
System.out.println("cube of"+n+"="+cube);
}
}
class randomnumber extends Thread
{
public void run()
{Random rd=new Random();
for(int i=0;i<10;i++)
{
int randomInteger=rd.nextInt(10);
System.out.println("random integer generated:"+randomInteger);
if(randomInteger % 2 ==0)
{
square s=new square(randomInteger);
s.start();
}
else
{
cube c=new cube(randomInteger);
c.start();
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("error"+e);
}
}
}
}
public class ass2setb3
{
public static void main(String args[])
{
randomnumber n=new randomnumber();
n.start();
}
}
DB Programming
SET A
- Create a PROJECT table with fields project_id, Project_name, Project_description, Project_Status. etc. Insert values in the table. Display all the details of the PROJECT table in a tabular format on the screen.(using swing).
import javax.swing.table.*;
import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Project extends JFrame implements ActionListener
{
JTextField t1,t2,t3,t4;
JLabel l1,l2,l3,l4;
JButton b1,b2;
int row,column;
Project()
{
setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("ID");
add(l1);
t1=new JTextField(10);
add(t1);
l2=new JLabel("Name");
add(l2);
t2=new JTextField(10);
add(t2);
l3=new JLabel("Description");
add(l3);
t3=new JTextField(10);
add(t3);
l4=new JLabel("Status");
add(l4);
t4=new JTextField(10);
add(t4);
b1=new JButton("Insert");
add(b1);
b1.addActionListener(this);
b2=new JButton("Display");
add(b2);
b2.addActionListener(this);
try
{
Class.forName("org.postgresql.Driver");
}
catch(Exception e)
{
System.out.println("Error"+e.getMessage());
}
}
public void actionPerformed(ActionEvent e2)
{
if(e2.getSource()==b1)
{
try
{
int eno=Integer.parseInt(t1.getText());
String ename=t2.getText();
String description=t3.getText();
String status=t4.getText();
Connection conn = DriverManager.getConnection("jdbc:postgresql:project","postgres","postgres");
PreparedStatement st=conn.prepareStatement("insert into fields values(?,?,?,?)");
st.setInt(1, eno);
st.setString(2,ename);
st.setString(3,description);
st.setString(4,status);
st.executeUpdate();
st.close();
JOptionPane.showMessageDialog(this,"Inserted");
}catch(Exception e)
{
System.out.println("Error"+e.getMessage());
}
}
if(e2.getSource()==b2)
{
try
{
Object[] data=new Object[4];
DefaultTableModel dtm=new DefaultTableModel();
JTable jt=new JTable(dtm);
ResultSet rs;
System.out.println("hello");
jt.setBounds(20,20,50,50);
String[] darr={"ID","Name","Description","Status"};
for(int column=0;column<4;column++)
dtm.addColumn(darr[column]);
Connection conn =DriverManager.getConnection("jdbc:postgresql://localhost/project","postgres","postgres");
Statement st=conn.createStatement();
rs=st.executeQuery("select * from fields");
for(row=0;rs.next();row++)
for(int column=0;column<4;column++)
{
data[column]=rs.getObject(column+1);
}
dtm.addRow(data);
rs.close();
getContentPane().add(new JScrollPane(jt));
}catch(Exception e)
{
}
}
}
public static void main(String args[])
{
new Project();
}
}
-
Write a program to display information about the database and list all the tables in the database. (Use DatabaseMetaData).
import java.sql.;
import java.io.;
class ass3seta2
{
public static void main(String[] args)throws SQLException
{
Connection con= null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName("org.postgresql.Driver");
con= DriverManager.getConnection("jdbc:postgresql:student","postgres","");
DatabaseMetaData dbmd = con.getMetaData();
rs = dbmd.getTables(null, null, null,new String[] {"TABLE"});
while (rs.next())
System.out.println( rs.getString("TABLE_NAME"));
con.close();
}
catch(Exception e)
{
System.out.println("ERROR"+e);
}
}
} -
Write a program to display information about all columns in the DONAR table using ResultSetMetaData.
import java.sql.;
import java.io.;
class ass3seta3
{
public static void main(String[] args) throws SQLException
{
Connection con= null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName("org.postgresql.Driver");
con= DriverManager.getConnection("jdbc:postgresql:student","postgres","");
stmt=con.createStatement();
rs = stmt.executeQuery("select * from stud");
ResultSetMetaData r1 = rs.getMetaData();
int noOfColumns = r1.getColumnCount();
System.out.println("Number of columns = " + noOfColumns);
for(int i=1; i<=noOfColumns; i++)
{
System.out.println("Column No : " + i);
System.out.println("Column Name : " + r1.getColumnName(i));
System.out.println("Column Type : " + r1.getColumnTypeName(i));
System.out.println("Column display size : " + r1.getColumnDisplaySize(i));
}
//con.close();
}
catch(Exception e)
{
System.out.println("ERROR"+e);
}
}
}
SET B
- Create a MOBILE table with fields Model_Number, Model_Name, Model_Color, Sim_Type, NetworkType, BatteryCapacity, InternalStorage, RAM and ProcessorType. Insert values in the table. Write a menu driven program to pass the input using Command line argument to perform the following operations on MOBILE table.
- Insert 2. Modify 3. Delete 4. Search 5. View All 6. Exit
import java.sql.;
import java.io.;
public class mobile
{
public static void main(String args[])throws SQLException
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement ps1=null,ps2=null,ps3=null;
int mno;
String mname,mcolor,stype,ntype,bcapacity,istorage,ram,ptype;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql:mobile","postgres","postgres");
stmt=con.createStatement();
while(true)
{
System.out.println("\nMENU");
System.out.println("\n1. Insert");
System.out.println("\n2. Modify ");
System.out.println("\n3. Delete");
System.out.println("\n4. Search");
System.out.println("\n5. View All");
System.out.println("\n6. Exit");
System.out.println("\nEnter choice:");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("\nInsert values->");
ps1=con.prepareStatement("Insert into mobile values(?,?,?,?,?,?,?,?,?)");
if(con!=null)
{
System.out.println("Connection Successful..");
System.out.println("Enter the mobile model number");
mno=Integer.parseInt(br.readLine());
System.out.println("Enter the mobile model name");
mname=br.readLine();
System.out.println("Enter the mobile model color");
mcolor=br.readLine();
System.out.println("Enter the mobile sim type");
stype=br.readLine();
System.out.println("Enter the mobile network type");
ntype=br.readLine();
System.out.println("Enter the mobile battery capacity");
bcapacity=br.readLine();
System.out.println("Enter the mobile internal storage");
istorage=br.readLine();
System.out.println("Enter the mobile RAM");
ram=br.readLine();
System.out.println("Enter the processor type");
ptype=br.readLine();
ps1.setInt(1,mno);
ps1.setString(2,mname);
ps1.setString(3,mcolor);
ps1.setString(4,stype);
ps1.setString(5,ntype);
ps1.setString(6,bcapacity);
ps1.setString(7,istorage);
ps1.setString(8,ram);
ps1.setString(9,ptype);
ps1.executeUpdate();
}
break;
case 2:
System.out.println("Enter Mobile Model ID to modify:");
mno=Integer.parseInt(br.readLine());
System.out.println("1.Model Name:");
System.out.println("2.Model Color:");
System.out.println("3.Sim type:");
System.out.println("4.Network type:");
System.out.println("5.Battery Capacity:");
System.out.println("6.Internal Storage:");
System.out.println("7.RAM:");
System.out.println("8.Processor Type:");
System.out.println("\nEnter choice:");
int ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.println("Enter Model Name to modify:");
mname=br.readLine();
ps2 = con.prepareStatement("Update mobile set mname= '"+mname+"'where mno = "+mno);
ps2.executeUpdate();
break;
case 2:
System.out.println("Enter Model Color to modify:");
mcolor=br.readLine();
ps2 = con.prepareStatement("Update mobile set mcolor= '"+mcolor+"'where mno = "+mno);
ps2.executeUpdate();
break;
case 3:
System.out.println("Enter Sim Type to modify:");
stype=br.readLine();
ps2 = con.prepareStatement("Update mobile set stype= '"+stype+"'where mno = "+mno);
ps2.executeUpdate();
break;
case 4:
System.out.println("Enter Network Type to modify:");
ntype=br.readLine();
ps2 = con.prepareStatement("Update mobile set ntype= '"+ntype+"'where mno = "+mno);
ps2.executeUpdate();
break;
case 5:
System.out.println("Enter Battery Capacity to modify:");
bcapacity=br.readLine();
ps2 = con.prepareStatement("Update mobile set bcapacity= '"+bcapacity+"'where mno = "+mno);
ps2.executeUpdate();
break;
case 6:
System.out.println("Enter Internal Storage to modify:");
istorage=br.readLine();
ps2 = con.prepareStatement("Update mobile set istorage= '"+istorage+"'where mno = "+mno);
ps2.executeUpdate();
break;
case 7:
System.out.println("Enter RAM to modify:");
ram=br.readLine();
ps2 = con.prepareStatement("Update mobile set ram= '"+ram+"'where mno = "+mno);
ps2.executeUpdate();
break;
case 8:
System.out.println("Enter Processor Type to modify:");
ptype=br.readLine();
ps2 = con.prepareStatement("Update mobile set ptype= '"+ptype+"'where mno = "+mno);
ps2.executeUpdate();
break;
}
System.out.println("record modified successfully");
break;
case 3:
System.out.println("Enter Mobile Model ID to Delete:");
mno=Integer.parseInt(br.readLine());
stmt.executeUpdate("Delete from mobile where mno = " + mno);
System.out.println("record deleted successfully");
break;
case 4:
System.out.println("Enter Mobile Model ID to Search:");
mno=Integer.parseInt(br.readLine());
rs = stmt.executeQuery("Select * from mobile where mno = " +mno);
if(rs.next())
{
System.out.println("Model ID:"+rs.getInt(1));
System.out.println("Model Name:"+rs.getString(2));
System.out.println("Model Color:"+rs.getString(3));
System.out.println("Sim type:"+rs.getString(4));
System.out.println("Network type:"+rs.getString(5));
System.out.println("Battery Capacity:"+rs.getString(6));
System.out.println("Internal Storage:"+rs.getString(7));
System.out.println("RAM:"+rs.getString(8));
System.out.println("Processor Type:"+rs.getString(9));
}
else
System.out.println("Student not found");
break;
case 5:
rs=stmt.executeQuery("Select * from mobile");
while(rs.next())
{
System.out.println("Model ID:"+rs.getInt(1));
System.out.println("Model Name:"+rs.getString(2));
System.out.println("Model Color:"+rs.getString(3));
System.out.println("Sim type:"+rs.getString(4));
System.out.println("Network type:"+rs.getString(5));
System.out.println("Battery Capacity:"+rs.getString(6));
System.out.println("Internal Storage:"+rs.getString(7));
System.out.println("RAM:"+rs.getString(8));
System.out.println("Processor Type:"+rs.getString(9));
}
break;
case 6:
System.exit(0);
}
}
}
catch(Exception e)
{
System.out.println("ERROR"+e);
}
con.close();
}
}
Servlets and JSP
SET A
- Design a servlet that provides information about a HTTP request from a client, such as IP address and browser type. The servlet also provides information about the server on which the servlet is running, such as the operating system type, and the names of currently loaded servlets.
(.java)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
-
Servlet implementation class Ass4SetA1
*/
@WebServlet("/ass4seta1")
public class ass4seta1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
java.util.Properties p = System.getProperties();
out.println("Server Name : "+request.getServerName()+"<br>");
out.println("Ip Address : "+request.getRemoteAddr()+"<br>");
out.println("Remote User : "+request.getRemoteUser()+"<br>");
out.println("Remote Host : "+request.getRemoteHost()+"<br>");
out.println("Local Name : "+request.getLocalName()+"<br>");
out.println("Server Port : "+request.getServerPort()+"<br>");
out.println("Protocol Name : "+request.getProtocol()+"<br>");
out.println("Os & Browser Details : "+request.getHeader("User-Agent")+"<br>");}
}
- Write a Program to make use of following JSP implicit objects:
i. out: To display current Date and Time.
ii. request: To get header information.
iii. response: To Add Cookie
iv. config: get the parameters value defined in
v. application: get the parameter value defined in
vi. session: Display Current Session ID
vii. pageContext: To set and get the attributes.
viii. page: get the name of Generated Servlet
(.html)
(.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
SET B
- Design an HTML page which passes customer number to a search servlet. The servlet searches for the customer number in a database (customer table) and returns customer details if found the number otherwise display error message.
(.html)
(.java)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
-
Servlet implementation class Ass4Setb1
*/
@WebServlet("/ass4setb1")
public class ass4setb1 extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();try { Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection("jdbc:postgresql:gayatri","postgres","postgres"); if(con != null) out.println("COnnection Successfull"); else out.println("Connection Failed"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("Select * from customer where cno = "+request.getParameter("cno")); if(rs.next()) { out.println("<h3>"+rs.getInt(1)+" - "+rs.getString(2)+"</h3>"); } else out.println("Record not found!"); } catch(Exception e) { out.println(e); }
}
}
- Design an HTML page containing option buttons (Maths, Physics, Chemistry and Biology) and buttons submit and reset. When the user clicks submit, the server responds by adding a cookie containing the selected subject and sends a message back to the client. Program should not allow duplicate cookies to be written.
(.html)
<input type = "submit"><input type = "reset" value = "reset">
</form> </body> </html>(.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
}
}
}
else
{
for(int i =0;i<s.length;i++)
{
Cookie c1 = new Cookie(s[i],"1");
response.addCookie(c1);
out.println(s[i]+" Added Successfully <br>");
}
}
%>
</body> </html>- Write a JSP program to display the details of PATIENT (PatientNo, PatientName, PatientAddress, Patientage,PatientDiease) in tabular form on browser.
(.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" import = "java.util.* ,java.sql.,java.io."
pageEncoding="UTF-8"%>
Spring
package com.example.demo;
import java.util.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class demoapplication {
public static void main(String[] args) {
SpringApplication.run(demoapplication.class, args);
System.out.println("If you can't explain it simply, you don't understand it well enough.");
Date d =new Date();
System.out.println("Current Date:"+d);
}
}