0% found this document useful (0 votes)
12 views37 pages

JPPPP

Uploaded by

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

JPPPP

Uploaded by

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

//use of throw statement

public class ThrowDemo

static double divide (double num, double den)

{ if (den==0.0)

throw new ArithmeticException("Denominator cannot be zero");

else

return (num/den);

public static void main(String[] args)

double x,y,z;

try
{

x = 12.5;

y = 0.0;

z=divide(x,y);

System.out.println(x + "/" + y +" = " + z);

catch (ArithmeticException e)

System.out.println(e.getMessage());

}
//use of throw statement

public class ReThrowDemo { static void divide() {

int x,y,z;

try

{ x = 12; y = 0;

z = x/y;

System.out.println(x+"/"+y+"=" + z);

catch (ArithmeticException e)

System.out.println("Exception caught in divide()");


System.out.println("Cannot divide by zero in integer division"); throw e; // Rethrows an exception

} public static void main(String[] args)

System.out.println("Start of main()");

try

divide();

catch (ArithmeticException e)

System.out.println("Rethrown exception caught in main()"); System.out.println(e);

}
}}

Creating threads using the thread class

class A extends Thread

public void run()

for (int i = 1 i <= 5 i++)

System.out.println("\t From ThreadA: i ="+i) } System. out. println("Exit from A ");

class B extends Thread

public void run()

for(int j = 1 j <= 5 j++) { } System.out.println("\tFrom Thread : j ="+j) ; System. out. println("Exit from B");
}

class C extends Thread

public void run(

for (int k = 1 k <= 5 k++)

System.out.println("\tFrom Thread k ="+k) ; } System. out. println("Exit from C");

class ThreadTest

public static void main(String args[])

new A().start();

new B().start();

new C().start();
12.2 Use of yield(), stop(), and sleep() methods

class A extends Thread

public void run()

for (int i = 11 i <= 5 i++)

if ( i ==1) yield(); System.out.println("\tFrom Thread A i ="+1) ;

} System.out.println("exit from A");

1 class B extends Thread

public void run()

{ for(int 1/4 = 1; - f <= 5 ;j++) {

System.out.println("\tFrom Thread Bj j =w+j1 ; if ( j ==3) stop();

} System.out.println("Exit from B");


class C extends Thread

public void run()

for (int k = 1 k <= 5 k++)

{ System. out. println("\tFrom Thread C: k ="+k) if ( k ==1)

try sleep(1000);

catch (Exception e)

System. out. println("Exit from C ");

class ThreadMethods

public static void main(String args[])

A threadA new A();

B threadB - new B(); C threadC new C(;


System.out.println("Start thread A");

threadA.start();

System.out.println("Start thread B");

threadB.start();

System.out.println("Start thread C)

threadC.start();

System.out.println("End of main thread")/

File Handling Operations.

<html>

<head>
<title>File Handling</title>

</head>

<body>

<?php

// Opening a file for writing the data $myFile = "pk.txt";

$fp = fopen($myFile,'w'); if($fp = false)

10.9

);

?>

echo("Error in opening new file"); exit();

fwrite($fp, "Have a nice day. \n"); fclose($fp);

<?php

// Opening a file for appending the data $myFile "pk.txt"; $fp = fopen($myFile,'a'); if($fp = false)
{

o close

2>

echo("Error in opening new file");

exit();

fwrite($fp, "Take care.");

fclose($fp);

<?php

// Opening a file for reading the data

$myFile = "pk.txt";

$fp = fopen($myFile,'r');

if($fp = false)

echo("Error in opening new file");


exit();

$fsize = filesize($myFile)

$filetext = fread($fp,$fsize); echo("<pre>$filetext</pre>"); fclose($fp);

</body>

</html>

Example:

<html>

<head>

<title>Create a Database</title>
</head>

<body>

<?php

// Create connection

$con-mysqli_connect("localhost","root","");

/ Check connection /

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: ".mysqli_connect_error();

else

echo "Database Connection successfully build";


}

// Create database

$sql="CREATE DATABASE BCA";

if (mysqli_query($con,$sql))

echo "<br>Database BCA created successfully";

else

echo "Error creating database: ".mysqli_error($con);

// Close connection

mysqli_close($con);

?>

</body>
</html>

Example:

<html>

<head>

<title>Create a Table</title>

</head>

<body>

<?php
// Create connection

$con=mysqli_connect("localhost","root","","BCA");

// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: ".mysqli_connect_error();

1 else

8.6

fune

SEL

data

echo "Database Connection successfully build";

1
// Create table $sql="CREATE TABLE STUDENT (FirstName CHAR(30), LastName CHAR(30), Age INT)";

// Execute query if (mysqli_query($con,$sql))

echo "<br>Table STUDENT created successfully";

Exa

else

{ echo "Error creating table: ".mysqli_error($con);

// Close connection

mysqli_close($con);

?>

</body>

</html>
Example:

<html>

<head>

<title>Inserting Records into Database Table</title>

</head>

<body>

<?php

// Create connection

$con=mysqli_connect("localhost","root", "", "BCA");


// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: ".mysqli_connect_error();

else

Be

echo "Database Connection successfully build";

/ Inserting records into the table /

mysqli_query($con, "INSERT INTO STUDENT (FirstName, LastName, Age) VALUES ('RAJ', 'KUMAR',35)");

mysqli_query($con, "INSERT INTO STUDENT (FirstName, LastName, Age) VALUES ('SIMRAN',


'KAUR',33)");

mysqli_query($con, "INSERT INTO STUDENT (FirstName, LastName, Age) VALUES ('PREM', 'KAUR',31)");
echo "<br>Records have been successfully inserted";

// Close connection

mysqli_close($con);

?>

</body>

</html>

<html>

<head>
<title>Selecting Records from Database Table</title>

</head>

<body>

<?php

// Create connection

$con-mysqli_connect("localhost","root","", "BCA");

// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: ".mysqli_connect_error();

else

}
echo "Database Connection successfully build";

ata

ise

oa

// Select records from the table $result = mysqli_query($con, "SELECT * FROM STUDENT");

while($row = mysqli_fetch_array($result))

echo $row['FirstName']." ".$row['LastName']." ".$row['Age'];

echo "<br>";

// Close connection

mysqli_close($con);
?>

</body>

</html>

After the execution of above code, the output would be

Example:

<html>

<head>

<title>Deleting Records from Database Table</title>

</head>

<body>
<?php

// Create connection

$con-mysqli_connect("localhost","root", "", "BCA");

// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: ".mysqli_connect_error();

else

echo "Database Connection successfully build";

// Delete records from the table

mysqli_query($con, "DELETE FROM STUDENT"); echo "<br>Records have been successfully deleted";

// Close connection

mysqli_close($con);
?>

</body>

</html>

</head>

<center>

<h3>Add Record Form</h3>

</center>

<form action="insert.php" method="post"> <table cellspacing="5" cellpadding="5">


(

<tr>

<td>

<p>

<label for="firstName">First Name:</label> &nbsp; &nbsp; &nbsp; <!-- To add space -->

<input type="text" name="first_name" id="firstName">

</p>

</td>

</tr>

<tr>

can
<td>

<P>

<label for="lastName">Last Name:</label> &nbsp; &nbsp; &nbsp; <!-- To add space -->

<input type="text" name="last_name" id="lastName">

</p>

</td>

</tr>

Form

Ad>

<p>

<label for="age">Age:</label>

- To add space --> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

<input type="text" name="st_age" id="age">


</p>

</td>

</tr>

<tr>

<td align="center">

<input type="submit" value="Submit">

</td>

</tr>

</table>

</form>

</body>

</html>

After the execution of above


html>

<head>

<title>Form Handler</title>

</head>

<body>

<?php

// Create connection

$con=mysqli_connect("localhost","root","", "BCA");

// Check connection

if($con = false)

echo "Failed to connect to MySQL Server: ".mysqli_connect_error();

// Escape user inputs for security


$first_name = mysqli_real_escape_string($con, $_REQUEST['first_name']); $last_name =
mysqli_real_escape_string($con, $_REQUEST['last_name']); Sage = mysqli_real_escape_string($con,
$_REQUEST['st_age']);

// Add form record into database table

mysqli_query($con, "INSERT INTO STUDENT (FirstName, LastName, Age) VALUES ($first_name',


'$last_name', '$age')");

echo "<br>Record has been successfully inserted";

// close connection

mysqli_close($con);

<body>

</html>

Soon-mysqli_connect("localhost","root", "", "BCA");

Check connection
if (mysqli_connect_errno())

echo "Failed to connect to MySQL: ".mysqli_connect_error();

<?php

//Select records from the table

$result = mysqli_query($con, "SELECT * FROM STUDENT");

$fetchRow = mysqli_fetch_assoc($result); //mysqli_fetch_assoc() function will returr specific row as an


array.

php

Select records from the table

$result = mysqli_query($con, "SELECT * FROM STUDENT");

SfetchRow = mysqli_fetch_assoc($result); //mysqli_fetch_assoc() function will return that specific row as


an array.

<html>
<head>

<title> Retrieve data from database and display in php form</title>

</head>

<body>

<center>

<h3>Record Form</h3>

</center>

<form action="" method="post">

<table cellspacing="5" cellpadding="5">

<tr>

<td>

<p>
<label for="firstName">First Name:</label>

&nbsp; &nbsp; &nbsp; <!-- To add space ->

Integrating PHP ar

<input type="text" name="first_name" id="firstName" value="<?php echo $fetchRow['FirstName']?>">

input type="subn

</p>

</td>

</table>

</tr>

</form>
<tr>

@php

<td>

// close conne

<p>

mysqli close

<label for="lastName">Last Name:</label> &nbsp; &nbsp; &nbsp; <!-- To add space -->

</body>

<input type="text" name="last_name" id="lastName" value="<?php echo $fetchRow['LastName']?>">

</html>

</p>

After the

4
</td>

</tr>

<tr>

<td>

<p>

<label for="age">Age:</label>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!- To add space-> <input type="text"
name="st_age" id="age" value="<?php echo $fetchRow['Age]>>>

</p>

</td>

</tr>

SH

<tr>
<td align="center">

<input type="submit" value="Submit">

</td>

</tr>

</table>

</form>

<?php

// close connection

mysqli_close($con);

?>

</body>

</html>

After the execution

You might also like