java_solutions
java_solutions
Slip 1A:
slip1B:
import java.sql.*;
import java.io.*;
class slip1B(here create database and insert record in it)
{
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:ab","postgres","");
if(con==null)
System.out.println("Connection Failed");
else
{
System.out.println("Connection sucessfull");
stmt=con.createStatement();
rs=stmt.executeQuery("Select * from emp");
while(rs.next())
{
System.out.println("EMP id="+rs.getInt(1));
System.out.println("EMP Name="+rs.getString(2));
System.out.println("EMP
Designation="+rs.getString(3));
System.out.println("EMP salary="+rs.getInt(4));
}
con.close();
}
}
catch(Exception e)
{
System.out.println("ERROR"+e);
}
}
}
slip2A:
import java.util.*;
scanner.close();
}
}
slip2B
slip2B(not runed)
mport java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class slip2 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
res.setContentType("text/html"
PrintWriter out=res.getWriter();
ServletConfig config=getServletConfig();
out.println("Server Name:"+req.getServerName());
out.println("Server Port:"+req.getServerPort());
out.println("Client IP Address:"+req.getRemoteHost())
out.println("Header info:"+req.getHeader("user-
out.println("context getServer Info:"+config.getServletName()
}
}
slip3A
Connection c=DriverManager.getConnection(
out.println("Connection created");
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from patient");
out.println("<html><body>");
out.println("<h1>PATIENT DETAILS</h1>");
out.println("<table border='1'>");
out.println("<tr><th>patient no</th><th>pname</th><th>
while(rs.next())
{
out.println("<tr>");
out.println("<td>"+ rs.getInt(1) +"</td>");
out.println("<td>"+ rs.getString(2) +"</td>");
out.println ("<td>"+ rs.getString(3) +"</td>");
out.println("<td>"+ rs.getInt(4) +"</td>");
out.println("<td>"+ rs.getString(5) +"</td>");
out.println("</tr>");
}
%>
</body>
</html>
slip3B:
import java.util.*;
class slip28B {
public static void main(String args[]) {
int n;
String element;
Scanner sc = new Scanner(System.in);
if (!a1.isEmpty()) {
a1.removeLast();
System.out.println("After deleting the last element: " + a1);
} else {
System.out.println("The list is empty, no element to delete.");
}
System.out.println("Displaying the list in reverse order:");
ListIterator<String> iterator = a1.listIterator(a1.size()); // Start at the end
of the list
while (iterator.hasPrevious()) {
System.out.println(iterator.previous());
}
}
}
slip4A
slip4B
import java.util.*;
while (true) {
System.out.println("\n*** City STD Code Manager ***");
System.out.println("1. Add a new city and its code");
System.out.println("2. Remove a city");
System.out.println("3. Search for a city");
System.out.println("4. Display");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");
int choice = sc.nextInt();
sc.nextLine(); // To consume the leftover newline character from
nextInt()
switch (choice) {
case 1:
System.out.print("Enter city name: ");
String city = sc.nextLine();
System.out.print("Enter STD code: ");
int code = sc.nextInt();
if (cityTable.containsKey(city)) {
System.out.println("City exists");
} else {
cityTable.put(city, code);
System.out.println("City successfully added");
}
break;
case 2:
System.out.print("Enter the city name to remove: ");
String cityToRemove = sc.nextLine();
if (cityTable.containsKey(cityToRemove)) {
cityTable.remove(cityToRemove);
System.out.println("City removed successfully.");
} else {
System.out.println("City not found!");
}
break;
case 3:
System.out.print("Enter the city name to search: ");
String cityToSearch = sc.nextLine();
if (cityTable.containsKey(cityToSearch)) {
System.out.println("STD Code for " + cityToSearch + " is: " +
cityTable.get(cityToSearch));
} else {
System.out.println("City not found!");
}
break;
case 4:
System.out.println(cityTable);
break;
case 5:
System.out.println("Exit");
sc.close();
return;
default:
System.out.println("Invalid choice");
}
}
}
}
slip5A
import java.util.*;
import java.util.Hashtable;
class slip5A
{
public static void main(String[] args)
{
int i;
Hashtable<Long,String> hashtable=new
Hashtable<Long,String>();
System.out.println("ENTER THE NO OF ENTRIES");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(i=0;i<n;i++)
{
System.out.println("ENTER THE NUMBER:");
long number=sc.nextLong();
System.out.println("ENTER THE NAME:");
String name=sc.next();
hashtable.put(number,name);
}
Enumeration<Long> keys = hashtable.keys();
while (keys.hasMoreElements())
{
long key = keys.nextElement();
System.out.println(key +": " + hashtable.get(key));
}
}
}
slip6A
import java.util.*;
class slip6A
{
public static void main(String args[])
{
int n,j;
Scanner sc=new Scanner(System.in);
System.out.println("enter Number");
n=sc.nextInt();
Set <Integer>ts=new TreeSet<Integer>();
for( int i=0;i<n;i++)
{
System.out.println("enter interger value");
j=sc.nextInt();
ts.add(j);
}
System.out.println("members from Treeset="+ts);
}
}
slip6B:
@Override
public void run() {
while (true) {
try {
// Green Light (3 seconds)
System.out.println(signalName + " Signal is GREEN");
Thread.sleep(3000); // Green for 3 seconds
} catch (InterruptedException e) {
System.out.println(signalName + " Signal interrupted");
}
}
}
}
slip7A:
import java.util.*;
class Square extends Thread
{
int x;
Square(int n)
{
x=n;
}
public void run()
{
System.out.println("Square="+x*x);
}
Cube(int n)
{
x=n;
}
public void run()
{
System.out.println("Cube="+x*x*x);
}
class slip7A
{
public static void main(String args[])
{
Number obj=new Number();
obj.start();
}}
slip7B:
import java.util.*;
import java.io.*;
import java.sql.*;
class slip7B {
public static void main(String args[]) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql:ab", "postgres",
"");
if (con == null)
System.out.println("Connection failed");
else {
System.out.println("Connection successful");
stmt = con.createStatement();
slip8A:
jsp:
%@ page language="java" import="java.util.*"%>
<%
int num=Integer.parseInt(request.getParameter("num"));
int flag=0;
out.println("<html><body>");
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
out.println("<h1 style='color:red'>Prime</h1>");
else
out.println("<h1 style='color:red'>Not a
Prime</h1>");
out.println("</html></body>");
%>
html file:
<html>
<head><title>Prime number or not</title></head>
<body>
<form action="slip8jsp.jsp" method="post">
Enter a number<input type="text" name="num"><br>
<input type="Submit" value="Submit">
</form>
</body>
</html>
slip11A:(not runed)
html file
<!DOCTYPE html>
<html>
<head>
<title>Customer Search</title>
</head>
<body>
<h2>Search Customer Details</h2>
<form action="slip11A" method="get">
<label for="customerNumber">Enter Customer
Number:</label>
<input type="text" id="customerNumber"
name="customerNumber" required>
<br><br>
<input type="submit" value="Search">
</form>
<div id="result"></div>
</body>
</html>
servlet file:
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class slip11A extends HttpServlet
{
public void service(HttpServletRequest
req,HttpServletResponse res)throws
IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
try
{
out.println("<html>");
out.println("<body>");
Class.forName("org.postgresql.Driver");
out.println("<h1>Driver loaded</h1>");
Connection
c=DriverManager.getConnection("jdbc:postgresql:ab","pos
tgres","");
out.println("<h1>Connection created</h1>");
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from
customer");
while(rs.next())
{
out.print("<h3>"+rs.getInt(1)+"-"+rs.getString(2)+"</h3
>");
out.println("<br>");
}
}
catch(Exception e)
{
out.println("ERROR"+e);
}
out.println("<h1>HI!Manisha</h1>");
out.println("</body>");
out.println("</html>");
}
}
slip11B:
import java.sql.*;
public class slip11B{
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
Connection conn =
DriverManager.getConnection("jdbc:postgresql:ab",
"postgres", "");
rs.close();
stmt.close();
conn.close();
} // try
catch (Exception e) {
System.out.println(e);
} // catch
}
}
slip 12A:
<!DOCTYPE html>
<html>
<head>
<title>PERFECT NUMBER</title>
</head>
<body>
<form action="perfect.jsp" method="post">
Enter Number :<input type="text" name="num">
<input type="submit" value="Submit" name="s1">
</form>
</body>
</html>
Perfect.jsp file:
<%@ page import="java.util.*" %>
<%
if(request.getParameter("s1")!=null)
{
Integer num,a,i,sum = 0;
num = Integer.parseInt(request.getParameter("num"));
a = num;
for(i=1;i<a;i++)
{
if(a%i==0)
{
sum=sum + i;
}
}
if(sum==a)
{
out.println(+num+ "is a perfect number");
}
else
{
out.println(+num+ "is not a perfect number");
}
}
%>
slip12B :
import java.sql.*;
import java.io.*;
class slip12B
{
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:ab","p
ostgres","");
if(con==null)
System.out.println("Connection is failed");
else
System.out.println("Connection is Successful");
stmt=con.createStatement();
String query4="Insert into Project
values(105,'OnlineBookshope','MarketProduct','Incomple'
)";
stmt.executeUpdate(query4);
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
}
slip13A:
import java.util.*;
import java.sql.*;
class slip13A
{
public static void main(String args[])
{
Connection con;
Statement st;
try
{
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql:ab","p
ostgres","");
st=con.createStatement();
DatabaseMetaData db=con.getMetaData();
String table[]={"TABLE"};
ResultSet rs=db.getTables(null,null,null,table);
while(rs.next())
{
System.out.println(rs.getString(3));
}
System.out.println("JDBC Driver
Name:"+db.getDriverName());
System.out.println("JDBC Driver Version
Name:"+db.getDriverVersion());
System.out.println("DB Product
Version:"+db.getDatabaseProductVersion());
System.out.println("Product
Name:"+db.getDatabaseProductName());
System.out.println("User Name:"+db.getUserName());
}
catch(Exception e)
{
System.out.println("Error!"+e);
}
}
}
slip13B:
class slip13B{
public static void main(String args[]) {
MyThread t1 = new MyThread("Shradha"), t2 = new
MyThread("Pooja");
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println("Main thread
interrupted.");
}
slip15A:
m1.setPriority(Thread.MIN_PRIORITY); // Set m1
to minimum priority
m2.setPriority(Thread.MAX_PRIORITY); // Set m2
to maximum priority
m1.start(); // Start m1
m2.start(); // Start m2
}
}
slip15B :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class slip15B extends HttpServlet
{
public void service(HttpServletRequest
req,HttpServletResponse res)throws
ServletException,IOException
{
PrintWriter out=null;
try{
res.setContentType("text/html");
out=res.getWriter();
Cookie c[]=req.getCookies();
if(c==null)
{
out.println("Welcome for cookie demo");
Cookie c1=new Cookie("count",Integer.toString(1));
res.addCookie(c1);
}
else{
for(int i=0;i<c.length;i++)
{
String name=c[i].getName();
String val=c[i].getValue();
out.println("Name="+name+"val="+val);
if(name.equals("count"))
{
int n=Integer.parseInt(val)+1;
c[i].setValue(Integer.toString(n));
out.println("page access for"+n+"times");
res.addCookie(c[i]);
}
}
}
out.close();
}catch(Exception e)
{
out.println("Error"+e);
}
}
}
slip16A:
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
import java.sql.*;
class slip16B {
public static void main(String args[]) {
int tno;
String tname, subject;
Scanner sc = new Scanner(System.in);
PreparedStatement ps1 = null, ps2 = null;
Connection con = null;
ResultSet rs = null;
try {
// Load the PostgreSQL driver
Class.forName("org.postgresql.Driver");
// Connect to the database (replace with
your actual database details)
con =
DriverManager.getConnection("jdbc:postgresql:ab",
"postgres", "");
if (con == null)
System.out.println("Connection
Failed");
else
System.out.println("Connection
Successful");
System.out.println("-------------------------");
} while (rs.next());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close all resources
if (ps1 != null) ps1.close();
if (ps2 != null) ps2.close();
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
slip17A:
import java.util.*;
class slip17A
{
public static void main(String args[])
{
int n,j;
Scanner sc=new Scanner(System.in);
System.out.println("enter Number");
n=sc.nextInt();
Set <Integer>ts=new TreeSet<Integer>();
for( int i=0;i<n;i++)
{
System.out.println("enter interger value");
j=sc.nextInt();
ts.add(j);
}
System.out.println("members from Treeset="+ts);
}
}
slip17B:
class slip17B implements Runnable {
@Override
public void run() {
int i = 1;
while (true) {
System.out.println(i);
i++;
if (i > 100) {
i = 1;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
thread.start();
}
}
slip18A:
import java.util.*;
slip18B:
import java.util.*;
slip19B:
slip20A:
<html>
<body>
<form method=get action="NumberWord.jsp">
Enter Any Number : <input type=text name=num><br><br>
<input type=submit value="Display">
</form>
<body>
</html>
NumberWord.jsp
<html>
<body>
<font color=red>
<%! int i,n;
String s1;
%>
<% s1=request.getParameter("num");
n=s1.length();
i=0;
do
{
char ch=s1.charAt(i);
switch(ch)
{
case '0': out.println("Zero ");break;
case '1': out.println("One ");break;
case '2': out.println("Two ");break;
case '3': out.println("Three ");break;
case '4': out.println("Four ");break;
case '5': out.println("Five ");break;
case '6': out.println("Six ");break;
case '7': out.println("Seven ");break;
case '8': out.println("Eight ");break;
case '9': out.println("Nine ");break;
}
i++;
}while(i<n);
%>
</font>
</body>
</html>
slip21A:
import java.util.*;
public class slip21A {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of subjects:
");
int n = scanner.nextInt();
scanner.nextLine();
Iterator<String> iterator =
subjectList.iterator();
System.out.println("\nSubjects list:");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
scanner.close();
}
}
slip23A:
import java.util.*;
slip23B :
import java.util.*;
System.out.println("\nUsing ListIterator:");
studentNames.listIterator().forEachRemaining(System.out
::println);
scanner.close();
}
}
slip27B:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.close();
}
}
slip28A :
import java.util.*;
class slip28B extends Thread
{
public void run()
{
System.out.println("running thread name
is :"+Thread.currentThread().getName());
}
public static void main(String args[])
{
slip28B m1= new slip28B();
// m1.setName("Thread1");
m1.start();
}
}
slip29A:(repeated)
slip29B:
import java.util.*;
class slip28B{
public static void main(String args[]) {
int n, element;
Scanner sc = new Scanner(System.in);
if (!a1.isEmpty()) {
a1.removeLast();
System.out.println("After deleting the last
element: " + a1);
} else {
System.out.println("The list is empty, no
element to delete.");
}