0% found this document useful (0 votes)
45 views13 pages

Java Program Solved Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views13 pages

Java Program Solved Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Practical:-12

*Program code*
1. import [Link].*;
import [Link].*;

public class Exp12


{
public static void main(String[] args)
{
JFrame f = new JFrame("login page");
[Link](400, 250);
[Link](300,200);

[Link](new GridLayout(3,2));
JLabel e1,e2;
JTextField t1;
JPasswordField jpf;
JButton b1,b2;
e1=new JLabel("Login ID :");
e2=new JLabel("Password :");
t1=new JTextField(30);
jpf=new JPasswordField();
[Link]('#');
b1=new JButton("SUBMIT");
b2=new JButton("CANCEL");
[Link](e1);
[Link](t1);
[Link](e2);
[Link](jpf);
[Link](b1);
[Link](b2);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

*Exercise*
1. import [Link].*;
import [Link].*;
import [Link].*;

class MyFrame extends JFrame implements ActionListener


{
JLabel e3;
JTextField t1;
JPasswordField jpf;
JButton b1,b2;
MyFrame(String title)
{
super(title);
setLayout(new GridLayout(4,2));
JLabel e1,e2;
e1=new JLabel("Login ID :");
e2=new JLabel("Password :");
e3=new JLabel();
t1=new JTextField(30);
jpf=new JPasswordField();
[Link]('#');
b1=new JButton("SUBMIT");
b2=new JButton("CANCEL");
add(e1);
add(t1);
add(e2);
add(jpf);
add(b1);
add(b2);
add(e3);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
String str=[Link]();
String pass=new String([Link]());
if([Link]("SUBMIT"))
{
if([Link]().equals("Mohammad Rashid") &&
[Link]("060422"))
[Link]("User name and password is correct");
else
[Link]("Invalid user name or password");
}
if([Link]("CANCEL"))
{
[Link]("");
[Link]("");
[Link]("");
}
}
}

public class Exp12_1


{

public static void main(String[] args)


{
MyFrame f = new MyFrame("login page");
[Link](400, 250);
[Link](300,200);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

2. import [Link].*;
import [Link].*;
import [Link].*;

class MyFrame extends JFrame implements ActionListener


{
JTextField t1,t2,t3;
JButton b1,b2;
MyFrame(String title)
{
super(title);
setLayout(new GridLayout(4,2));
JLabel e1,e2,e3;
e1=new JLabel("Enter first number : ");
e2=new JLabel("Enter second number : ");
e3=new JLabel("Result : ");
t1=new JTextField(30);
t2=new JTextField(30);
t3=new JTextField(30);
b1=new JButton("ADD");
b2=new JButton("CANCEL");
add(e1);
add(t1);
add(e2);
add(t2);
add(e3);
add(t3);
add(b1);
add(b2);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
try{
String str=[Link]();
String str1=[Link]();
double d1=[Link](str1);
String str2=[Link]();
double d2=[Link](str2);
if([Link]("ADD"))
{
double d3=d1+d2;
[Link](d3+"");
}
if([Link]("CANCEL"))
{
[Link]("");
[Link]("");
[Link]("");
}
}catch(Exception e){}
}
}

public class Exp12_2


{

public static void main(String[] args)


{
MyFrame f = new MyFrame("Addition");
[Link](400, 250);
[Link](300,200);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

3. import [Link].*;
import [Link].*;
import [Link].*;
class MyFrame extends JFrame implements ActionListener
{
JLabel e1;
JPasswordField jpf;
JButton b1;
JTextArea ta;
MyFrame(String title)
{
super(title);
setLayout(new GridLayout(3,2));
e1=new JLabel("Enter Password :");
ta=new JTextArea(3,30);
[Link](true);
jpf=new JPasswordField();
b1=new JButton("OK");
add(e1);
add(jpf);
add(b1);
add(ta);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
String pass=new String([Link]());
if([Link]()<6)
[Link]("Password lenth must be >=6 characters");
else
[Link]("Password lenth is valid");
}
}

public class Exp12_3


{

public static void main(String[] args)


{
MyFrame f = new MyFrame("login page");
[Link](400, 250);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Practical:-14

*Program code*
1. import [Link].*;

class InetDemo
{
public static void main(String[] args)throws UnknownHostException
{
try{
InetAddress ip=[Link]("localhost");
[Link]("Host Name : "+[Link]());
[Link]("IP Address : "+[Link]());
}catch(Exception e){[Link](e);}
}
}
*Exercise*
1. import [Link].*;
import [Link].*;
public class IPFinder
{
public static void main(String[] args)
{
String host;
Scanner input = new Scanner([Link]);
[Link]("\nEnter host name ([Link]) : ");
host = [Link]();
try
{
InetAddress address = [Link](host);
[Link]("IP address: "+ [Link]());
}
catch (UnknownHostException uhEx)
{
[Link]("Could not find " + host);
[Link]("Error : " + uhEx);
}
}
}

Practical:-15

*Program code*
1. import [Link].*;
class URLDemo
{
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("[Link]
[Link]("Protocol: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("File: " + [Link]());
[Link]("Ext:" + [Link]());
}
}

*Exercise*
1. import [Link].*;
class Exp15_1
{
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("[Link]
[Link]("Protocol: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("File: " + [Link]());
}
}

2. import [Link].*;
import [Link].*;
import [Link].*;
class Exp15_2
{
public static void main(String args[]) throws Exception
{
int c;
String host;
Scanner input=new Scanner([Link]);
[Link]("\nEnter full URL: ");
host=[Link]();
URL hp = new URL(host);
URLConnection hpCon = [Link]();
long d = [Link]();
if(d==0)
[Link]("No date information.");
else
[Link]("Date: " + new Date(d));
[Link]("Content-Type: " + [Link]());
int len = [Link]();
if(len == -1)
[Link]("Content length is not available.");
else
[Link]("Content-Length: " + len);
}
}

Practical:-18

*Program code*
1. import [Link].*;

class CreateTableExample2
{
public static void main(String args[])
{
try{
[Link]("[Link]");
Connection c=[Link]("jdbc:odbc:mydsn");
Statement
s=[Link](ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="create table Product(prod_id Integer, prod_name
varchar(25), price Double)";
[Link](sql);
[Link]("Table created.");
ResultSet rs=[Link]("select * from Product");
while([Link]())
{
[Link]("Prod id : "+[Link](1));
[Link]("Prod Name : "+[Link](2));
[Link]("Price : "+[Link]("price")+"\
n");
}
}catch(Exception e)
{
[Link]("Exception generated: "+e);
}
}
}
2. import [Link].*;
class JdbcDemo2
{
public static void main (String args[])
{
try
{
String url= "jdbc:mysql://localhost:3306/mydb";

Connection cn=[Link](url,"root","");
[Link]("Connection to the database created");
Statement st=
[Link](ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String str= "create table student1 (roll_no integer(4), stud_name
varchar(25));";
[Link](str);
[Link]("table created");
str= "insert into student1 values(3,'ccc')";
[Link](str);
[Link]("row inserted");
str= "select * from student1";
ResultSet rs=[Link](str);

[Link]("Roll Number \t Name");


while([Link]())
{
[Link]([Link](1) + "\t" + [Link](2) +
"\n");
}
//[Link](text);
[Link]();
[Link]();
}
catch(Exception s)
{
[Link](s);
}
}
}

*Exercise*
1. import [Link].*;

class Exp18_1
{
public static void main(String args[])
{
String userName="root";
String password="";
String URL = "jdbc:mysql://localhost:3306/mydb";

try{
Connection conn = [Link](URL, userName,
password);
Statement
s=[Link](ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE
);
String sql="create table if not exists employee(emp_id
integer(4),emp_name text(25));";
[Link](sql);
[Link]("Table created.");
ResultSet rs=[Link]("select * from employee");
while([Link]())
{
[Link]("Employee id : "+[Link](1));
[Link]("Employee Name : "+[Link](2));
}
}catch(Exception e)
{
[Link]("Exception generated: "+e);
}
}
}

2. import [Link].*; //Import statement for JDBC


public class Exp18_2
{
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
String userName="root";
String password="";
String URL = "jdbc:mysql://localhost:3306/mydb";

try{
//Registering JDBC driver

//Creating connection to the database


Connection conn = [Link](URL, userName,
password);
Statement stmt = [Link]();
ResultSet results = [Link]("select * from student
where percent>=70");
[Link]("Roll No. Name\tYear & Branch\t Percentage");

[Link]("----------------------------------------------------");
while([Link]())
{
int rn = [Link](1);
String name = [Link](2);
String yr = [Link](3);
float pr = [Link](4);
[Link](rn + "\t" + name + "\t" + yr+ "\t\t " +
pr);
}
//Closing resultset, statement, and connection
[Link]();
[Link]();
[Link]();
}catch(Exception e)
{
[Link](e);
}
}
}

Practical:-19
*Program code*
1. import [Link].*;
public class PreparedStmtEx
{
public static void main(String args[])
{
try
{

Connection
con=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
//keep "" empty if not given during installation
PreparedStatement stmt=[Link]("update student1 set
roll_no=? where roll_no=?");
[Link](1,111);
[Link](2,101);
int i=[Link]();
[Link](i+" records updated");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}

2. import [Link].*;
public class PreparedStmtEx2
{
public static void main(String args[])
{
try
{
Connection
con=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
//keep "" empty if not given during installation
PreparedStatement stmt=[Link]("insert into student1
values(?,?)");
[Link](1,101);
[Link](2,"ddd");

int i=[Link]();
[Link](i+" records inserted");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}

*Exercise*
1. import [Link].*;
public class Exp19_1
{
public static void main(String args[])
{
try
{
Connection
con=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
Statement s=[Link]();
ResultSet rs=[Link]("select * from student");
while([Link]())
{
[Link]("Roll No.: "+[Link](1));
[Link]("Name : "+[Link](2));
[Link]("Branch : "+[Link]("Branch")+"\
n");
}
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}

2. import [Link].*;
public class Exp19_2
{
public static void main(String args[])
{
try
{
Connection
con=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
Statement s = [Link]();
[Link]("update student set sname='qqq' where rno=2");
ResultSet rs=[Link]("select * from student");
while([Link]())
{
[Link]("Roll No.: "+[Link](1));
[Link]("Name : "+[Link](2));
[Link]("Branch : "+[Link]("Branch")+"\
n");
}
}catch(Exception e)
{
[Link]("Exception generated: "+e);
}
}
}

Practical:-22

*Program code*
1. import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class AuthenticationServlet extends HttpServlet


{
String uname="ajp";
String pass="22517";
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
[Link]("text/html;charset=UTF-8");
PrintWriter pw = [Link]();
String str1=[Link]("username");
String str2=[Link]("password");
if([Link](uname)&&[Link](pass))
{
[Link]("Login Successfull.");
}
else
{
[Link]("Login Unsuccessfull.");
}
[Link]();
}
}

HTML CODE
1. Login
<html>
<body>
<center>
<form name="Form1" action="[Link]
method="POST">
<table>
<tr>
<td>Enter User Name</td><td><input type=text name="username"></td>
</tr>
<tr>
<td>Enter Password</td><td><input type=password name="password"></td>
</tr>
</table>
<input type=submit value="Submit">
</form>
</body>
</html>

2. Username
<html>
<body>
<center>
<form name="Form1" action="[Link] method="POST">
<table>
<tr>
<td>Enter User Name</td><td><input type=text name="username"></td>
</tr>
<tr>
<td></td><td><input type=submit value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
*Exercise*
1. import [Link].*;
import [Link].*;
import [Link].*;
public class ListRadioServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse
response)throws ServletException, IOException
{
[Link]("text/html");
PrintWriter pw = [Link]();
String[] str = [Link]("color");
[Link]("<B>The selected color(s) is(are): ");
for(int i=0;i<[Link];i++)
[Link](str[i]);
String opt = [Link]("option");

[Link]("<B>The selected option is: ");


[Link](opt);
[Link]();
}
}

HTML CODE
1. ListRadio
<html>
<body>
<center>
<form name="Form1" method="post"
action="[Link]
<table>
<tr>
<td><B>Color:</B></td>
<td><select name="color" multiple>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select></td>
</tr>
<tr>
<td><B>Options:</B></td>
<td><input type = "radio" name = "option" value = "yes"> Yes
<input type = "radio" name = "option" value = "physics"> No
</td>
</tr>
</table>
<input type=submit value="Submit">
</form>
</body>
</html>

2. import [Link].*;
import [Link].*;
import [Link].*;
public class Check extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse
response)throws ServletException, IOException
{
[Link]("text/html");
PrintWriter pw = [Link]();
String str1 = [Link]("marks");
if(str1!=null)
try{
int n=[Link](str1);
if(n>=40)
[Link]("You are passed.");
else
[Link]("You are failed.");
}catch(Exception e){}
[Link]();
}
}

HTML CODE
1. Student
<html>
<body>
<center>
<form name="Form1" action="[Link]
<table>
<tr>
<td align=right>Enter Marks:<td><input type=text name=marks value="">
</tr>
<tr>
<td align=right><input type=submit value="Submit">
</tr>
</table>
</form>
</body>
</html>

You might also like