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

Advanced Java Lab Code Answers

The document contains code solutions for four advanced Java lab exercises. It includes a servlet to check if a number is prime, a JDBC program to display employee data, a JSP page for user login, and a Java program demonstrating LinkedList operations. Each code snippet is designed to illustrate specific Java concepts and functionalities.

Uploaded by

anisamujawar6340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Advanced Java Lab Code Answers

The document contains code solutions for four advanced Java lab exercises. It includes a servlet to check if a number is prime, a JDBC program to display employee data, a JSP page for user login, and a Java program demonstrating LinkedList operations. Each code snippet is designed to illustrate specific Java concepts and functionalities.

Uploaded by

anisamujawar6340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Java Lab Code Answers

Q1: Servlet to Check if a Number is Prime

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PrimeCheckServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
int num = Integer.parseInt(request.getParameter("number"));
boolean isPrime = true;

if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Number " + num + " is " + (isPrime ? "Prime" : "Not Prime") + "</h2>");
}
}

Q2: JDBC Program to Display Emp Table Data

import java.sql.*;

public class DisplayEmpData {


public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/yourdbname";
String user = "root";
String password = "yourpassword";

try {
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT empid, empname, age FROM Emp");

while (rs.next()) {
System.out.println("ID: " + rs.getInt("empid") +
", Name: " + rs.getString("empname") +
", Age: " + rs.getInt("age"));
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Q3: JSP Page to Accept Login Name and Password

<%@ page import="java.io.*" %>


<%
String login = request.getParameter("login");
String pass = request.getParameter("password");

String correctLogin = "YourName"; // Replace with your actual name


String correctPass = "pw@123";

if (login != null && pass != null) {


if (login.equals(correctLogin) && pass.equals(correctPass)) {
out.println("<h2>Logged in Successfully</h2>");
} else {
out.println("<h2>Error: Invalid Login or Password</h2>");
}
}
%>

<form method="post">
Login: <input type="text" name="login"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>

Q4: Java Program Using LinkedList

import java.util.*;

public class LinkedListDemo {


public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();

// Insert 10 numbers
for (int i = 1; i <= 10; i++) {
list.add(i * 10); // example: 10, 20, 30...
}

System.out.println("Original List: " + list);

// Remove sixth element (index 5)


list.remove(5);

System.out.println("After Removing 6th Element: " + list);


}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy