Q.1.
Advanced Java:
A)
Write a java Program in Hibernate to display “Hello world” message.
Answer :
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HelloWorld {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
System.out.println("Hello world");
session.close();
sessionFactory.close();
}
}
Output :
B)
Write a SERVLET program to display the details of Product (ProdCode, PName,
Price)
on the browser in tabular format. (Use database)
Answer :
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ProductDetailsServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Products");
out.println("<html><head><title>Product Details</title></head><body><table border='1'><tr><th>ProdCode</th><th>PName</th><th>Price</th></tr>");
while (rs.next()) {
String prodCode = rs.getString("ProdCode");
String pName = rs.getString("PName");
String price = rs.getString("Price");
out.println("<tr><td>" + prodCode + "</td><td>" + pName + "</td><td>" + price + "</td></tr>");
}
out.println("</table></body></html>");
con.close();
} catch (Exception e) {
out.println(e);
}
}
}
Output :
Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 10 Answers
Reviewed by technical_saurabh
on
September 21, 2022
Rating:
No comments:
Post a Comment