Java Program Solved Manual
Java Program Solved Manual
Java Program Solved Manual
*Program code*
1. import java.awt.*;
import javax.swing.*;
f.setLayout(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();
jpf.setEchoChar('#');
b1=new JButton("SUBMIT");
b2=new JButton("CANCEL");
f.add(e1);
f.add(t1);
f.add(e2);
f.add(jpf);
f.add(b1);
f.add(b2);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
*Exercise*
1. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
2. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
3. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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);
ta.setLineWrap(true);
jpf=new JPasswordField();
b1=new JButton("OK");
add(e1);
add(jpf);
add(b1);
add(ta);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String pass=new String(jpf.getPassword());
if(pass.length()<6)
ta.setText("Password lenth must be >=6 characters");
else
ta.setText("Password lenth is valid");
}
}
Practical:-14
*Program code*
1. import java.net.*;
class InetDemo
{
public static void main(String[] args)throws UnknownHostException
{
try{
InetAddress ip=InetAddress.getByName("localhost");
System.out.println("Host Name : "+ip.getHostName());
System.out.println("IP Address : "+ip.getHostAddress());
}catch(Exception e){System.out.println(e);}
}
}
*Exercise*
1. import java.net.*;
import java.util.*;
public class IPFinder
{
public static void main(String[] args)
{
String host;
Scanner input = new Scanner(System.in);
System.out.print("\nEnter host name (e.g.google.com) : ");
host = input.next();
try
{
InetAddress address = InetAddress.getByName(host);
System.out.println("IP address: "+ address.toString());
}
catch (UnknownHostException uhEx)
{
System.out.println("Could not find " + host);
System.out.println("Error : " + uhEx);
}
}
}
Practical:-15
*Program code*
1. import java.net.*;
class URLDemo
{
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("https://www.javatpoint.com/javafx-tutorial");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Ext:" + hp.toExternalForm());
}
}
*Exercise*
1. import java.net.*;
class Exp15_1
{
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("https://www.msbte.org.in");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
}
}
2. import java.net.*;
import java.io.*;
import java.util.*;
class Exp15_2
{
public static void main(String args[]) throws Exception
{
int c;
String host;
Scanner input=new Scanner(System.in);
System.out.print("\nEnter full URL: ");
host=input.next();
URL hp = new URL(host);
URLConnection hpCon = hp.openConnection();
long d = hpCon.getDate();
if(d==0)
System.out.println("No date information.");
else
System.out.println("Date: " + new Date(d));
System.out.println("Content-Type: " + hpCon.getContentType());
int len = hpCon.getContentLength();
if(len == -1)
System.out.println("Content length is not available.");
else
System.out.println("Content-Length: " + len);
}
}
Practical:-18
*Program code*
1. import java.sql.*;
class CreateTableExample2
{
public static void main(String args[])
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:mydsn");
Statement
s=c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="create table Product(prod_id Integer, prod_name
varchar(25), price Double)";
s.execute(sql);
System.out.println("Table created.");
ResultSet rs=s.executeQuery("select * from Product");
while(rs.next())
{
System.out.println("Prod id : "+rs.getInt(1));
System.out.println("Prod Name : "+rs.getString(2));
System.out.println("Price : "+rs.getString("price")+"\
n");
}
}catch(Exception e)
{
System.out.println("Exception generated: "+e);
}
}
}
2. import java.sql.*;
class JdbcDemo2
{
public static void main (String args[])
{
try
{
String url= "jdbc:mysql://localhost:3306/mydb";
Connection cn=DriverManager.getConnection(url,"root","");
System.out.println("Connection to the database created");
Statement st=
cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String str= "create table student1 (roll_no integer(4), stud_name
varchar(25));";
st.executeUpdate(str);
System.out.println("table created");
str= "insert into student1 values(3,'ccc')";
st.executeUpdate(str);
System.out.println("row inserted");
str= "select * from student1";
ResultSet rs=st.executeQuery(str);
*Exercise*
1. import java.sql.*;
class Exp18_1
{
public static void main(String args[])
{
String userName="root";
String password="";
String URL = "jdbc:mysql://localhost:3306/mydb";
try{
Connection conn = DriverManager.getConnection(URL, userName,
password);
Statement
s=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE
);
String sql="create table if not exists employee(emp_id
integer(4),emp_name text(25));";
s.execute(sql);
System.out.println("Table created.");
ResultSet rs=s.executeQuery("select * from employee");
while(rs.next())
{
System.out.println("Employee id : "+rs.getInt(1));
System.out.println("Employee Name : "+rs.getString(2));
}
}catch(Exception e)
{
System.out.println("Exception generated: "+e);
}
}
}
try{
//Registering JDBC driver
System.out.println("----------------------------------------------------");
while(results.next())
{
int rn = results.getInt(1);
String name = results.getString(2);
String yr = results.getString(3);
float pr = results.getFloat(4);
System.out.println(rn + "\t" + name + "\t" + yr+ "\t\t " +
pr);
}
//Closing resultset, statement, and connection
results.close();
stmt.close();
conn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
Practical:-19
*Program code*
1. import java.sql.*;
public class PreparedStmtEx
{
public static void main(String args[])
{
try
{
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
//keep "" empty if not given during installation
PreparedStatement stmt=con.prepareStatement("update student1 set
roll_no=? where roll_no=?");
stmt.setInt(1,111);
stmt.setInt(2,101);
int i=stmt.executeUpdate();
System.out.println(i+" records updated");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
2. import java.sql.*;
public class PreparedStmtEx2
{
public static void main(String args[])
{
try
{
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
//keep "" empty if not given during installation
PreparedStatement stmt=con.prepareStatement("insert into student1
values(?,?)");
stmt.setInt(1,101);
stmt.setString(2,"ddd");
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
*Exercise*
1. import java.sql.*;
public class Exp19_1
{
public static void main(String args[])
{
try
{
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
Statement s=con.createStatement();
ResultSet rs=s.executeQuery("select * from student");
while(rs.next())
{
System.out.println("Roll No.: "+rs.getInt(1));
System.out.println("Name : "+rs.getString(2));
System.out.println("Branch : "+rs.getString("Branch")+"\
n");
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
2. import java.sql.*;
public class Exp19_2
{
public static void main(String args[])
{
try
{
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
Statement s = con.createStatement();
s.executeUpdate("update student set sname='qqq' where rno=2");
ResultSet rs=s.executeQuery("select * from student");
while(rs.next())
{
System.out.println("Roll No.: "+rs.getInt(1));
System.out.println("Name : "+rs.getString(2));
System.out.println("Branch : "+rs.getString("Branch")+"\
n");
}
}catch(Exception e)
{
System.out.println("Exception generated: "+e);
}
}
}
Practical:-22
*Program code*
1. import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
HTML CODE
1. Login
<html>
<body>
<center>
<form name="Form1" action="http://localhost:8080/AuthenticationServlet"
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="http://localhost:8080/CheckLength" 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 java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ListRadioServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse
response)throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String[] str = request.getParameterValues("color");
pw.println("<B>The selected color(s) is(are): ");
for(int i=0;i<str.length;i++)
pw.println(str[i]);
String opt = request.getParameter("option");
HTML CODE
1. ListRadio
<html>
<body>
<center>
<form name="Form1" method="post"
action="http://localhost:8080/ListRadioServlet">
<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 java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Check extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse
response)throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String str1 = request.getParameter("marks");
if(str1!=null)
try{
int n=Integer.parseInt(str1);
if(n>=40)
pw.println("You are passed.");
else
pw.println("You are failed.");
}catch(Exception e){}
pw.close();
}
}
HTML CODE
1. Student
<html>
<body>
<center>
<form name="Form1" action="http://localhost:8080/aa/Check">
<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>