Assignment 3
Set A2
import [Link].*;
public class DBInfoSimple {
public sta c void main(String[] args) {
try {
// Connect to database
Connec on con = [Link] on(
"jdbc:mysql://localhost:3306/your_db",
"root",
"your_password"
);
// Get metadata
DatabaseMetaData db = [Link]();
// Print database name
[Link]("Database: " + [Link]());
// Get tables
ResultSet rs = [Link](null, null, "%", null);
[Link]("Tables:");
while ([Link]()) {
[Link]([Link](3));
}
[Link]();
} catch (Excep on e) {
[Link](e);
}
}
}
Set B1
import [Link].*;
import java.u [Link];
public class MobileSimple {
public sta c void main(String[] args) throws Excep on {
Connec on con = [Link] on(
"jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = [Link]();
// Create table
[Link]("CREATE TABLE IF NOT EXISTS MOBILE(" +
"Model_Number INT PRIMARY KEY, Model_Name VARCHAR(50), RAM
INT)");
Scanner sc = new Scanner([Link]);
int ch;
do {
[Link]("\[Link] [Link] [Link] [Link] [Link]");
ch = [Link]();
switch (ch) {
case 1:
[Link]("Enter No, Name, RAM: ");
int no = [Link]();
String name = [Link]();
int ram = [Link]();
[Link]("INSERT INTO MOBILE VALUES(" +
no + ",'" + name + "'," + ram + ")");
break;
case 2:
[Link]("Enter Model No: ");
int u = [Link]();
[Link]("New Name: ");
String n = [Link]();
[Link]("UPDATE MOBILE SET Model_Name='" +
n + "' WHERE Model_Number=" + u);
break;
case 3:
[Link]("Enter Model No: ");
int d = [Link]();
[Link]("DELETE FROM MOBILE WHERE
Model_Number=" + d);
break;
case 4:
ResultSet rs = [Link]("SELECT * FROM MOBILE");
while ([Link]()) {
[Link]([Link](1) + " " +
[Link](2) + " " + [Link](3));
}
break;
}
} while (ch != 5);
[Link]();
}
}
Set B2
import [Link].*;
import [Link].*;
public class SimpleForm extends JFrame implements Ac onListener {
JTextField birth, mobile;
JBu on submit;
SimpleForm() {
setLayout(null);
JLabel l1 = new JLabel("Birth Year:");
[Link](50, 50, 100, 30);
add(l1);
birth = new JTextField();
[Link](150, 50, 100, 30);
add(birth);
JLabel l2 = new JLabel("Mobile:");
[Link](50, 100, 100, 30);
add(l2);
mobile = new JTextField();
[Link](150, 100, 100, 30);
add(mobile);
submit = new JBu on("Submit");
[Link](100, 150, 100, 30);
add(submit);
[Link] onListener(this);
setSize(300, 300);
setVisible(true);
}
public void ac onPerformed(Ac onEvent e) {
try {
if ([Link]().equals("0000"))
throw new Excep on("Invalid Birth Year!");
if ([Link]().length() != 10)
throw new Excep on("Invalid Mobile!");
JOp [Link](this, "Success!");
} catch (Excep on ex) {
JOp [Link](this, [Link]());
}
}
public sta c void main(String[] args) {
new SimpleForm();
}
}
Assignment 4
Set A
1)
import [Link].*;
import [Link].*;
import [Link].h p.*;
public class InfoServlet extends H pServlet {
public void doGet(H pServletRequest req, H pServletResponse res)
throws IOExcep on {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h2>Client Info</h2>");
[Link]("IP Address: " + [Link]() + "<br>");
[Link]("Browser: " + [Link]("User-Agent") + "<br>");
[Link]("<h2>Server Info</h2>");
[Link]("OS: " + [Link]("[Link]") + "<br>");
ServletContext ctx = getServletContext();
[Link]("Server Name: " + [Link]());
}
}
2)
<%@ page import="java.u l.*,[Link].h p.*" %>
<h3>Date & Time:</h3>
<%= new Date() %>
<h3>Request Header:</h3>
<%= [Link]("User-Agent") %>
<%
Cookie c = new Cookie("name","student");
[Link](c);
%>
<h3>Config Param:</h3>
<%= confi[Link]("myParam") %>
<h3>Applica on Param:</h3>
<%= applica [Link]("appParam") %>
<h3>Session ID:</h3>
<%= [Link]() %>
<%
[Link] ribute("msg","Hello");
%>
<h3>PageContext:</h3>
<%= [Link] ribute("msg") %>
<h3>Servlet Name:</h3>
<%= [Link]() %>
3)[Link]
<form ac on="[Link]">
Username: <input type="text" name="user">
<input type="submit" value="Login">
</form>
[Link]
<%
[Link] ribute("user", [Link]("user"));
%>
<form ac on="[Link]">
Book Price: <input type="text" name="price">
<input type="submit" value="Buy">
</form>
[Link]
<%
int total = 0;
int price = [Link]([Link]("price"));
if([Link] ribute("total") != null)
total = (int)[Link] ribute("total");
total += price;
[Link] ribute("total", total);
%>
<h3>Total Bill: <%= total %></h3>
Set b
1) [Link]
<form ac on="SearchServlet">
Enter Customer ID: <input type="text" name="id">
<input type="submit" value="Search">
</form>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].h p.*;
public class SearchServlet extends H pServlet {
public void doGet(H pServletRequest req, H pServletResponse res)
throws IOExcep on {
[Link]("text/html");
PrintWriter out = [Link]();
String id = [Link]("id");
try {
Connec on con = [Link] on(
"jdbc:mysql://localhost:3306/test","root","root");
PreparedStatement ps = [Link](
"SELECT * FROM customer WHERE id=?");
[Link](1, id);
ResultSet rs = [Link]();
if ([Link]()) {
[Link]("Name: " + [Link]("name"));
} else {
[Link]("Customer Not Found!");
}
} catch (Excep on e) {
[Link]("Error!");
}
}
}
2)Cookies
<form ac on="CookieServlet">
<input type="radio" name="sub" value="Maths">Maths
<input type="radio" name="sub" value="Physics">Physics
<input type="submit">
</form>
[Link]
import [Link].*;
import [Link].*;
import [Link].h p.*;
public class CookieServlet extends H pServlet {
public void doGet(H pServletRequest req, H pServletResponse res)
throws IOExcep on {
PrintWriter out = [Link]();
String sub = [Link]("sub");
Cookie[] cookies = [Link]();
boolean exists = false;
if (cookies != null) {
for (Cookie c : cookies) {
if ([Link]().equals(sub)) {
exists = true;
}
}
}
if (!exists) {
Cookie c = new Cookie("subject", sub);
[Link](c);
[Link]("Cookie Added: " + sub);
} else {
[Link]("Already Selected!");
}
}
}
3)pa ent
<%@ page import="[Link].*" %>
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Address</th><th>Age</th><th>Disease</th>
</tr>
<%
Connec on con = [Link] on(
"jdbc:mysql://localhost:3306/test","root","root");
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM pa ent");
while([Link]()) {
%>
<tr>
<td><%= [Link](1) %></td>
<td><%= [Link](2) %></td>
<td><%= [Link](3) %></td>
<td><%= [Link](4) %></td>
<td><%= [Link](5) %></td>
</tr>
<%
}
%>
</table>