Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Program Solved Manual

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 13

Practical:-12

*Program code*
1. import java.awt.*;
import javax.swing.*;

public class Exp12


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

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.*;

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();
jpf.setEchoChar('#');
b1=new JButton("SUBMIT");
b2=new JButton("CANCEL");
add(e1);
add(t1);
add(e2);
add(jpf);
add(b1);
add(b2);
add(e3);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
String pass=new String(jpf.getPassword());
if(str.equals("SUBMIT"))
{
if(t1.getText().equals("Mohammad Rashid") &&
pass.equals("060422"))
e3.setText("User name and password is correct");
else
e3.setText("Invalid user name or password");
}
if(str.equals("CANCEL"))
{
t1.setText("");
jpf.setText("");
e3.setText("");
}
}
}

public class Exp12_1


{

public static void main(String[] args)


{
MyFrame f = new MyFrame("login page");
f.setSize(400, 250);
f.setLocation(300,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

2. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try{
String str=ae.getActionCommand();
String str1=t1.getText();
double d1=Double.parseDouble(str1);
String str2=t2.getText();
double d2=Double.parseDouble(str2);
if(str.equals("ADD"))
{
double d3=d1+d2;
t3.setText(d3+"");
}
if(str.equals("CANCEL"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}catch(Exception e){}
}
}

public class Exp12_2


{

public static void main(String[] args)


{
MyFrame f = new MyFrame("Addition");
f.setSize(400, 250);
f.setLocation(300,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

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");
}
}

public class Exp12_3


{

public static void main(String[] args)


{
MyFrame f = new MyFrame("login page");
f.setSize(400, 250);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

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);

System.out.println("Roll Number \t Name");


while(rs.next())
{
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) +
"\n");
}
//System.out.print(text);
st.close();
cn.close();
}
catch(Exception s)
{
System.out.println(s);
}
}
}

*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);
}
}
}

2. import java.sql.*; //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 = DriverManager.getConnection(URL, userName,
password);
Statement stmt = conn.createStatement();
ResultSet results = stmt.executeQuery("select * from student
where percent>=70");
System.out.println("Roll No. Name\tYear & Branch\t Percentage");

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;

public class AuthenticationServlet extends HttpServlet


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

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");

pw.println("<B>The selected option is: ");


pw.println(opt);
pw.close();
}
}

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>

You might also like