Q.1.
Advanced Java:
A)
Write a Java Program to create a Emp (ENo, EName, Sal) table and insert record
into
it. (Use PreparedStatement Interface)
Answer :
import java.sql.*;
import java.io.*;
class Slip9A {
public static void main(String args[]) throws Exception {
Connection con;
PreparedStatement pstmt;
int e1, s;
String enm;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bcadb", "root", "");
pstmt = con.prepareStatement("create table
employee1(eid int(8),ename varchar(20),sal int(8))");
pstmt.executeUpdate();
System.out.println("Table Created Successfully!!!!!!");
System.out.println("=====================================");
DataInputStream din = new DataInputStream(System.in);
System.out.println("Enter Employee Id, Name and Salary");
e1 = Integer.parseInt(din.readLine());
enm = din.readLine();
s = Integer.parseInt(din.readLine());
pstmt = con.prepareStatement("insert into
employee1 values(?,?,?)");
pstmt.setInt(1, e1);
pstmt.setString(2, enm);
pstmt.setInt(3, s);
pstmt.executeUpdate();
System.out.println("Record Inserted Successfully!!!!!!");
con.close();
}
}
B)
Write a JSP program to create an online shopping mall. User must be allowed to
do
purchase
from two pages. Each page should have a page total. The third page should
display
a bill, which consists of a page total of whatever the purchase has been done
and
print the total. (Use Session)
Answer :
<%@ page import="java.util.*" %>
<%
// Create a session object if one doesn't exist
HttpSession session = request.getSession(true);
// Get the cart object from the session
Map<String, Integer> cart = (Map<String, Integer>)session.getAttribute("cart");
if (cart == null) {
cart = new HashMap<String, Integer>();
session.setAttribute("cart", cart);
}
// Add items to the cart
if (request.getParameter("item") != null) {
String item = request.getParameter("item");
int quantity = Integer.parseInt(request.getParameter("quantity"));
if (cart.containsKey(item)) {
quantity += cart.get(item);
}
cart.put(item,
quantity);
}
// Calculate the page total
int pageTotal = 0;
for (Map.Entry<String, Integer> entry : cart.entrySet()) {
String item = entry.getKey();
int quantity = entry.getValue();
int price = getPrice(item);
pageTotal += price *
quantity;
}
// Display the shopping cart
out.println("<h1>Shopping
Cart</h1>");
out.println("<table>");
out.println("<tr><th>Item</th><th>Quantity</th><th>Price</th></tr>");
for (Map.Entry<String, Integer> entry : cart.entrySet()) {
String item = entry.getKey();
int quantity = entry.getValue();
int price = getPrice(item);
int total = price *
quantity;
out.println("<tr><td>" + item + "</td><td>" +
quantity + "</td><td>" + total + "</td></tr>");
}
out.println("</table>");
// Display the page total
out.println("<h2>Page Total: " +
pageTotal + "</h2>");
// Display the checkout button
out.println("<form action=\"checkout.jsp\"><input
type=\"submit\" value=\"Checkout\"></form>");
%>
No comments:
Post a Comment