Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 16 Answers

 


A) Write a JSP script to accept username and password from user, if they are same then

display “Login Successfully” message in Login.html file, otherwise display “Login

Failed” Message in Error.html file.

Answer :

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
    <% String username = request.getParameter("username");
       String password = request.getParameter("password");
       if (username != null && password != null && username.equals(password)) { %>
            <h1>Login Successfully</h1>
       <% } else { %>
            <h1>Login Failed</h1>
       <% } %>
</body>
</html>

HTML Code:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <form action="login.jsp" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username" required><br> <label for="password">Password:</label> <input type="password" id="password" name="password" required><br> <input type="submit" value="Login"> </form> </body> </html>

Error.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Error</title>
</head>
<body>
    <h1>Login Failed</h1>
    <p>Please check your username and password and try again.</p>
</body>
</html>

Output :

 

B) Write a Java program to accept the details of students (rno, sname, per) at least 5

Records, store it into database and display the details of student having highest

percentage. (Use PreparedStatement Interface)

Answer :

import java.sql.*;

public class StudentDetails {
    public static void main(String[] args) {
        try {
            // Connect to database
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "");

            // Prepare SQL statement to insert student details
            String insertSql = "INSERT INTO students (rno, sname, per) VALUES (?, ?, ?)";
            PreparedStatement insertStmt = conn.prepareStatement(insertSql);

            // Insert 5 student records
            insertStmt.setInt(1, 1);
            insertStmt.setString(2, "John");
            insertStmt.setFloat(3, 85.0f);
            insertStmt.executeUpdate();

            insertStmt.setInt(1, 2);
            insertStmt.setString(2, "Jane");
            insertStmt.setFloat(3, 90.0f);
            insertStmt.executeUpdate();

            insertStmt.setInt(1, 3);
            insertStmt.setString(2, "Bob");
            insertStmt.setFloat(3, 80.0f);
            insertStmt.executeUpdate();

            insertStmt.setInt(1, 4);
            insertStmt.setString(2, "Alice");
            insertStmt.setFloat(3, 95.0f);
            insertStmt.executeUpdate();

            insertStmt.setInt(1, 5);
            insertStmt.setString(2, "Tom");
            insertStmt.setFloat(3, 75.0f);
            insertStmt.executeUpdate();

            // Prepare SQL statement to retrieve student with highest percentage
            String selectSql = "SELECT * FROM students WHERE per = (SELECT MAX(per) FROM students)";
            PreparedStatement selectStmt = conn.prepareStatement(selectSql);

            // Execute SQL statement and display results
            ResultSet rs = selectStmt.executeQuery();
            while (rs.next()) {
                int rno = rs.getInt("rno");
                String sname = rs.getString("sname");
                float per = rs.getFloat("per");
                System.out.println("Roll No: " + rno + ", Name: " + sname + ", Percentage: " + per);
            }

            // Close resources
            rs.close();
            selectStmt.close();
            insertStmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output :


Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 16 Answers Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 16 Answers Reviewed by technical_saurabh on December 15, 2022 Rating: 5

No comments:

Powered by Blogger.