Lab Manual
Lab Manual
double length;
double width;
this.length = length;
this.width = width;
@Override
double area() {
double radius;
Circle(double radius) {
this.radius = radius;
@Override
double area() {
Output:
class LibraryItem {
String title;
int id;
void issue() {
// Issue operation
void returnItem() {
// Return operation
String author;
int pages;
void search() {
// Search operation
}
void renew() {
// Renew operation
void fineCalculation() {
// Fine calculation
String category;
String publicationDate;
void issue() {
String journalType;
void issue() {
}
7. Java program for banking application with exception handling:
InsufficientBalanceException(String message) {
super(message);
TransactionCountExceededException(String message) {
super(message);
TransactionAmountExceededException(String message) {
super(message);
class BankingApplication {
int transactionCount = 0;
if (transactionCount >= 3) {
balance -= amount;
transactionCount++;
try {
bank.withdraw(50000);
bank.withdraw(30000);
bank.withdraw(20000);
Output:
8. Create a student database and store the details of the students in a table. Perform the SELECT,
INSERT, UPDATE and DELETE operations using JDBC connectivity
import java.sql.*;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
// Create table
System.out.println("Creating table...");
statement = connection.createStatement();
statement.executeUpdate(createTableSQL);
// Insert data
System.out.println("Inserting data...");
String insertSQL = "INSERT INTO students (name, age) VALUES (?, ?)";
preparedStatement.setString(1, "John");
preparedStatement.setInt(2, 20);
if (rowsInserted > 0) {
// Select data
System.out.println("Selecting data...");
while (resultSet.next()) {
int id = resultSet.getInt("id");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
// Update data
System.out.println("Updating data...");
preparedStatement = connection.prepareStatement(updateSQL);
preparedStatement.setInt(1, 21);
preparedStatement.setString(2, "John");
if (rowsUpdated > 0) {
}
// Delete data
System.out.println("Deleting data...");
preparedStatement = connection.prepareStatement(deleteSQL);
preparedStatement.setString(1, "John");
if (rowsDeleted > 0) {
e.printStackTrace();
} finally {
// Close resources
try {
if (statement != null) {
statement.close();
if (connection != null) {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
Make sure to replace "your_username" and "your_password" with your actual MySQL database
username and password respectively.
Output:
Connecting to database...
Creating table...
Inserting data...
Selecting data...
Updating data...
Deleting data...
9. Create a HTML login page (login.html) with username and password fields.
Create a servlet (LoginServlet.java) to handle the login request and validate the username and
password.
Connect to the database and validate the user credentials in the servlet.
html
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
</form>
</body>
</html>
java
Copy code
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
response.setContentType("text/html");
try {
Class.forName("com.mysql.jdbc.Driver");
PreparedStatement ps = con.prepareStatement(
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
} else {
} catch (Exception e) {
System.out.println(e);
out.close();
xml
Copy code
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>
Create a database table named users with columns username and password. Insert some sample
data into the table.
Output:
When a user enters invalid credentials, it will display "Invalid username or password".
Make sure to replace "your_database_name", "username", and "password" with your actual
database name, username, and password respectively.