Java New
Java New
Java New
/Sec: -63/B
Code :
import java.io.*;
System.out.println("Object Serialization");
Product prd = new Product();
FileOutputStream fs = new FileOutputStream("notes.ser");
ObjectOutputStream obj = new ObjectOutputStream(fs); obj.writeObject(prd);
System.out.println("Object De-Serialization");
FileInputStream fis = new FileInputStream("notes.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Product p = (Product)ois.readObject();
System.out.println("product name is : "+ p.name + " product price is : "+p.price );
}
}
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Q2. Assume two files f1.txt and f2.txt with some content. Write a Java program to mergethe
content of these files and write into third file f3.txt
Code :
import java.io.*; public class Qes2 { public static void
main(String[] args) throws IOException {
FileReader fr1 = new FileReader("f1.txt");
BufferedReader br1 = new BufferedReader(fr1);
FileReader fr2 = new FileReader("f2.txt");
BufferedReader br2 = new BufferedReader(fr2);
PrintWriter pr = new
PrintWriter("f3.txt"); String line =
br1.readLine(); while (line != null) {
pr.println(line); line = br1.readLine();
pr.flush();
}
String line2 =
br2.readLine(); while (line2
!= null) {
pr.println(line2); line2 =
br2.readLine();
pr.flush();
}}}
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output
t
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Q3. Create Number.txt file. Write three-digit number starting from111 to 999. Now read
the Number.txt file line by line and if the three digit number is palindrome then write that
number in the palindromeOnly.txt file. (Use PrintWriter and BufferedReader class and
create separate method to check whether a number is palindrome or not).
Code :
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Q4. Ask a user to enter a product price using Scanner class. If the price is less then 10 then
throw user define exception TooMinAmount and if the price is greater then 20 then throw
TooMaxAmount else print Collect your product.
Code : import
java.util.Scanner;
class TooMinAmount extends Exception {
public TooMinAmount(String msg) {
super(msg);
}
}
class TooMaxAmount extends Exception {
public TooMaxAmount(String msg) {
super(msg);
}
}
public class Qes4 { public static void
main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter the product price: ");
double price = sc.nextDouble();
if (price < 10) {
throw new TooMinAmount("Price is too low!");
} else if (price > 20) {
throw new TooMaxAmount("Price is too high!");
} else {System.out.println("Collect your product");
}
} catch (TooMinAmount e) {
System.out.println(e.getMessage());
} catch (TooMaxAmount e) {
System.out.println(e.getMessage());
}
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
}
}
Output:
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
import java.sql.Connection; import
java.sql.DriverManager; import
java.sql.ResultSet; import
java.sql.Statement; import
com.mysql.cj.jdbc.Driver; public
class Find { public static void
main(String[] args) {
String url = "jdbc:mysql://localhost:3306/product";
String user = "root";
String password = "root";
try{
Connection con = DriverManager.getConnection(url , user , password);
String sql = "select * from student";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getInt(3)); }
}catch(Exception e){
e.printStackTrace();
}
}
}
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
import java.sql.*; import java.util.Scanner; public class CRUD {
public static void insertData(Connection con) throws SQLException {
Scanner s = new Scanner(System.in);
Scanner st = new Scanner(System.in);
System.out.println("Enter number of products to be inserted");
int n = s.nextInt();
String qeury = "insert into product values(?,?,?)";
PreparedStatement ps = con.prepareStatement(qeury);
for (int i = 0; i < n; i++) {
System.out.println("Enter the id , name , price of product : ");
int id = s.nextInt();
String name = st.nextLine();
int price = s.nextInt();
ps.setInt(1, id);
ps.setString(2, name);
ps.setInt(3, price);
ps.executeUpdate();
}
System.out.println("data inserted successfully ");
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
}
public static void showData(Connection con) throws SQLException{
String sql = "select * from product";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
System.err.println("product details :");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2) +
" " + rs.getInt(3));
}
}
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
import java.sql.Connection; import
java.sql.DriverManager; import
java.sql.PreparedStatement; import
java.sql.ResultSet; import
java.sql.Statement; import
com.mysql.cj.jdbc.Driver;
insp.setString(2,"pasta");
insp.setInt(3,250);
insp.addBatch();
insp.setInt(1,5 );
insp.setString(2,"glas");
insp.setInt(3, 25);
insp.addBatch();\
updp.setInt(1,2);
updp.setString(2,"Cat");
updp.addBatch();
delp.setInt(1,5);
delp.addBatch();
insp.executeUpdate();
// updp.executeUpdate();
delp.executeUpdate();
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
Callable.java:
import java.sql.CallableStatement;
import java.sql.Connection; import
java.sql.DriverManager; import
java.sql.PreparedStatement; import
java.sql.ResultSet; import
java.sql.Statement; import
java.sql.Types; import
java.util.Scanner;
import com.mysql.cj.jdbc.Driver;
try{
Connection con = DriverManager.getConnection(url , user , password);
}catch(Exception e){
e.printStackTrace();
}
}
}
DELIMITER //
DELIMITER ;
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action ="fact" method="get" >
<input type="input" placeholder="Enter a number" name="n"/>
<input type="submit" value="find factorial" />
</form>
</body>
</html>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
Sum.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action ="sum" method="get" >
<input type="input" placeholder="Enter a first Number" name="n1"/>
<input type="input" placeholder="Enter a Second Number" name="n2"/>
<br/>
<input type="submit" value="find Sum" />
</form>
</body>
</html>
Sum.java package
com.amit.fact; import
java.io.IOException;
import java.io.PrintWriter;
import
javax.servlet.http.HttpServ
let; import
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
javax.servlet.http.HttpServ
letRequest; import
javax.servlet.http.HttpServ
letResponse;
int n1 = Integer.parseInt(req.getParameter("n1"));
int n2 = Integer.parseInt(req.getParameter("n2"));
int sum = n1 + n2; PrintWriter out;
try { out =
res.getWriter();
out.print("sum of the two numbers are :"+sum);
}catch(IOException io) {
io.printStackTrace();
}
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>S</servlet-name>
<servlet-class>com.amit.fact.Sum</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>S</servlet-name>
<url-pattern>/sum</url-pattern>
</servlet-mapping></web-app>
Name:- Suraj Singh Negi Course/Semester:-MCA/2nd Roll_No./Sec:-63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
Dispatch.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="dispatch" method="get">
<input type="input" name="num" palaceholder ="Enter a number" />
<input type="submit" value="submit" />
</form>
</body>
</html>
Send.java:
package com.amit.fact; import
java.io.IOException; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession;
int n = Integer.parseInt(req.getParameter("num"));
n++;
HttpSession s = req.getSession();
s.setAttribute("ck", n);
// url name not class
res.sendRedirect("send2");
}
}
Send2.java :
package com.amit.fact; import
java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.http.HttpServlet;
HttpSession s = req.getSession();
int n = (int)s.getAttribute("ck");
n = n*10;
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>com.amit.fact.Send</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/dispatch</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatch2</servlet-name>
<servlet-class>com.amit.fact.Send2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatch2</servlet-name>
<url-pattern>/send2</url-pattern>
</servlet-mapping>
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Q13. Create a dynamic web project to demonstrate Session management using Cookie
Code :
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="cookie" method="get">
<input type="input" name="user" palaceholder ="Enter a username" />
<input type="submit" value="submit" />
</form>
</body>
</html>
Cookie2.java:
package com.amit.fact;
}
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
Index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="submit" method="post">
<input type="text" name="username" placeholder="enter name "><br>
<input type="password" name="password" placeholder= "Enter password"><br>
<button type="submit">Login</button>
</form>
</body>
</html>
Login.java :
package com.amit.fact; import
java.io.IOException; import
javax.servlet.ServletException; import
javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Web.xml :
<servlet>
<servlet-name>log</servlet-name>
<servlet-class>com.amit.fact.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>log</servlet-name>
<url-pattern>/submit</url-pattern>
</servlet-mapping>
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Code :
Index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="factorial.jsp" method="post">
Factorial.jsp :
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Factorial Result</title>
</head>
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
<body>
<h2>Factorial Result</h2>
<%
// Retrieve the number from the request String
numberStr = request.getParameter("number"); int
number = Integer.parseInt(numberStr);
int factorial = 1; for(int i
= 1; i <= number; i++) {
factorial *= i;
}
%>
<p>The factorial of <%= number %> is <%= factorial %>.</p>
</body>
</html>
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Q16. Create Dynamic Web project to demonstrate the use of Java Bean class using useBean
and setProperty tag in JSP pages.
Code :
Indx.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="add.jsp" method="post">
<input type="number" name="number1" >
<input type="number" name="number2" >
<br>
<button type="submit">Add</button>
</form>
</body>
</html>
Add.jsp:
<%@ page import="com.mca.jsp.NumberBean" %>
<jsp:useBean id="numberBean" class="com.mca.jsp.NumberBean" scope="request" />
<jsp:setProperty property="*" name="numberBean" />
<!DOCTYPE html>
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
<html><head><title>Result</title>
</head><body><h2>Result</h2>
<%int sum = numberBean.getSum();%>
<p>The sum of <%= numberBean.getNumber1() %> and <%= numberBean.getNumber2() %>
is <%= sum %>.</p>
</body></html>
Numberjean.java : package
com.mca.jsp; public class
NumberBean { private
int number1; private int
number2; private int
sum;
public int getNumber1() {
return number1;
}
public void setNumber1(int number1) {
this.number1 = number1;
}
public int getNumber2() {
return number2;
}
public void setNumber2(int number2) {
this.number2 = number2;}public int
getSum() { return number1 + number2;
}
}
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Output :
Name: - Suraj Singh Negi Course/Semester: -MCA/2nd Roll No./Sec: -63/B
Q17. Write a java program to calculate sum of last digits of two numbers.
Program:-
Program:-
class TestCyclic
{
public static void main(String[] args)
{
System.out.println(cyclic(12345));
while(n>0)
{
rem=n%10;
n=n/10;
sum=sum+rem;
}
}
return sum;
}}
Output: - 55