0% found this document useful (0 votes)
4 views

Final_19CSPC505_Web_Programming_Lab_Manual

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Final_19CSPC505_Web_Programming_Lab_Manual

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Ex No: 6 Web Application using PHP

Aim:
To create a web application using php.

Algorithm:
Step 1: create a index.php file
Step 2: configure the XAMPP server.
Step 3: Execute the query and create database name and table name.
Step 4:.Add the student details into the table.
Step 5: For displaying the table information in online, index.php itself styles has
been used.
Step 6: you can do update, sort by order, remove the added items from the table.

Program:

Create Dbase

<?php
$link = mysqli_connect("localhost", "root", "");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt create database query execution


$sql = "CREATE DATABASE pacet";
if(mysqli_query($link, $sql)){
echo "Database created successfully";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

1
// Close connection
mysqli_close($link);
?>

2. Create Table

<?php

$link = mysqli_connect("localhost", "root", "", "pacet");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt create table query execution


$sql = "CREATE TABLE cse(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(70) NOT NULL UNIQUE
)";
if(mysqli_query($link, $sql)){
echo "Table created successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

4. insert.php

<?php
$link = mysqli_connect("localhost", "root", "", "pacet");

2
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);
$last_name = mysqli_real_escape_string($link, $_REQUEST['last_name']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$sql = "INSERT INTO cse (first_name, last_name, email) VALUES ('$first_name',
'$last_name', '$email')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
3.Online Form using PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="firstName">First Name:</label>
<input type="text" name="first_name" id="firstName">
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="last_name" id="lastName">
</p>
<p>
<label for="emailAddress">Email Address:</label>
<input type="text" name="email" id="emailAddress">

3
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>

Output

4
Result:
Thus the given PHP with MYSQL program has been executed successfully.
Ex No: 7 Designing XML Schemas

Aim:
To design a XML schema for student information system
Algorithm:
Step 1: Create elements for your XML Schema.
Step 2: Define which XML Schema elements are child elements.
Step 3: Create your XML Schema attributes.
Step 4: Create your XML Schema types to define the content of your elements
and attributes.
Step 5: Check your XML Schema to be sure XML elements and XML attributes
are properly named and that there are no other errors.
Step 6: Validate your XML Schema

Program:
ugrads.xsd

5
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ugrads" type="uType">
<!-- student primary key -->
<xsd:key name="sPKey">
<xsd:selector xpath="student"/>
<xsd:field xpath="studentID"/>
</xsd:key>
<!-- enrollment foreign key -->
<xsd:keyref name="sFKey" refer="sPKey">
<xsd:selector xpath="enrollment"/>
<xsd:field xpath="studentID"/>
</xsd:keyref>
</xsd:element>
<!-- ugrads type -->
<xsd:complexType name="uType">
<xsd:sequence>
<xsd:element name="student" type="sType" maxOccurs="unbounded"/>
<xsd:element name="enrollment" type="eType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!-- student type -->
<xsd:complexType name="sType">
<xsd:sequence>
<xsd:element name="studentID" type="xsd:string"/>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<!-- enrollment type -->
<xsd:complexType name="eType">
<xsd:sequence>
<xsd:element name="courseID" type="xsd:string"/>
<xsd:element name="studentID" type="xsd:string"/>
<xsd:element name="grade" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>

6
</xsd:schema>
gradCourses.xsd
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="gradCourses" type="gType">
<!-- course primary key -->
<xsd:key name="cPKey">
<xsd:selector xpath="course"/>
<xsd:field xpath="courseID"/>
</xsd:key>
</xsd:element>
<xsd:complexType name="gType">
<xsd:sequence>
<xsd:element name="course" type="cType" maxOccurs="unbounded">
<!-- student primary key -->
<xsd:key name="sPKey">
<xsd:selector xpath="student"/>
<xsd:field xpath="studentID"/>
</xsd:key>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="cType">
<xsd:sequence>
<xsd:element name="courseID" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="student" type="sType" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="sType">
<xsd:sequence>
<xsd:element name="studentID" type="xsd:string"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="grade" type="xsd:string"/>
</xsd:sequence>

7
</xsd:complexType>
</xsd:schema>

students.xsd
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="students">
<xsd:complexType name="sType">
<xsd:sequence>
<xsd:element name="student" type="sType" maxOccurs="unbounded">
<!-- course primary key -->
<xsd:key name="cPKey">
<xsd:selector xpath="course" />
<xsd:field xpath="courseID" />
</xsd:key>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!-- student primary key -->
<xsd:key name="sPKey">
<xsd:selector xpath="student"/>
<xsd:field xpath="studentID"/>
</xsd:key>
</xsd:element>
<xsd:complexType name="sType">
<xsd:sequence>
<xsd:element name="studentID" type="xsd:string"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="type" type="myTypes"/>
<xsd:element name="course" type="cType" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="cType">
<xsd:sequence>
<xsd:element name="courseID" type="xsd:string"/>

8
<xsd:element name="title" type="xsd:string" minOccurs="0"/>
<xsd:element name="type" type="myTypes" minOccurs="0"/>
<xsd:element name="grade" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="myTypes">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="GRAD"/>
<xsd:enumeration value="UGRAD"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>

mapping.xq
<students>{
(
for $us in doc("ugrads.xml")/ugrads/student
let $usid := $us/studentID
return
<student>
<studentID>{ data($usid) }</studentID>
<name>{ data($us/name) }</name>
<type>UGRAD</type>
{(
for $gc in
doc("gradCourses.xml")/gradCourses/course[student/studentID=$usid]
let $cgrade := $gc/student[studentID = $usid]/grade
return
<course>
<courseID>{ data($gc/courseID) }</courseID>
<title>{ data($gc/title) }</title>
<type>GRAD</type>
<grade>{ data($cgrade) }</grade>
</course>
)
union

9
(
for $ue in doc("ugrads.xml")/ugrads/enrollment[studentID = $usid]
where not(
some $courseID in
doc("gradCourses.xml")//course[student/studentID = $usid]/courseID
satisfies $ue/courseID = $courseID)
return
<course>
<courseID>data($ue/courseID)</courseID>
<type>UGRAD</type>
if (COUNT($ue/grade) > 0)
then <grade>{ data($ue/grade) }</grade>
</course>
)}
</student>
)
union
(
for $gsid in distinct-values(doc("gradCourses.xml")//student/studentID)
let $gs := doc("gradCourses.xml")//student[studentID = $gsid]
where not(some $usid in doc("ugrads.xml")//student/studentID satisfies $gsid =
$usid)
return
<student>
<studentID>{ data($gsid) }</studentID>
<name>{ data($gs[1]/name) }</name>
<type>GRAD</type>
{
for $gc in doc("gradCourses.xml")//course[student/studentID = $gsid]
let $cgrade := $gc/student[studentID = $gsid]/grade
return
<course>
<courseID>{ data($gc/courseID) }</courseID>
<title>{ data($gc/title) }</title>
<type>GRAD</type>
<grade>{ data($cgrade) }</grade>

10
</course>
}
</student>
)
}</students>
Output:

Result:
Thus, the designing of XML schema for student information system was
designed and executed successfully.

Ex No: 8 Programs using JSON and Ajax

Aim:
To develop a program using Json and Ajax

Algorithm:
Step 1: Create a html file and placeholder to call ajax

11
Step 2: Create a JSON data file and save it with .json extension
Step 3: Make AJAX Call to Populate HTML Table with JSON Data
Step 4: Display JSON Data in HTML Table using Javascript & AJAX
Program:
Ajax.html
<h1 id="title"></h1>
<hr>
<div id="text"></div>
<script src="ajax.js"></script>

Program:
Ajax.js
function ajax_get(url, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('responseText:' + xmlhttp.responseText);
try {
var data = JSON.parse(xmlhttp.responseText);
} catch(err) {
console.log(err.message + " in " + xmlhttp.responseText);
return;
}
callback(data);
}
};

xmlhttp.open("GET", url, true);


xmlhttp.send();
}

ajax_get('data.json', function(data) {
document.getElementById("title").innerHTML = data["title"];

var html = "<h2>" + data["title"] + "</h2>";


html += "<h3>" + data["description"] + "</h3>";

12
html += "<ul>";
for (var i=0; i < data["articles"].length; i++) {
html += '<li><a href="' + data["articles"][i]["url"] + '">' + data["articles"][i]["title"]
+ "</a></li>";
}
html += "</ul>";
document.getElementById("text").innerHTML = html;
});

data.json
{
"title" : "P A College of Engineering and Technology",
"description" : "Creating a New Innovation",
"articles" : [
{
"title" : "PACET - About us",
"url" : "https://www.pacolleges.org/about.html"
},
{
"title" : "Department of Computer Science and Engineering",
"url" : "https://www.pacolleges.org/aboutcse.html"
}
]
}

Output:

13
14
Result:

15
Thus, the program using json and ajax has been developed and executed
successfully.

Ex No: 9 Designing web applications in Net Beans Environment

Aim:
To design a web application in NetBeans environment.

Procedure:
Step 1: Open Netbeans IDE, Select File -> New Project

Step 2: Select Java Web -> Web Application, then click on Next,

16
Step 3: Give a name to your project and click on Next, and then, Click Finish

Step 4: The complete directory structure required for the Servlet Application will
be created automatically by the IDE.

17
Step 5: To create a Servlet, open Source Package, right click on default packages
-> New -> Servlet.

18
Step 6: Give a Name to your Servlet class file,

Step 7: Now, your Servlet class is ready, and you just need to change the method
definitions and can write out code inside Servlet class.

19
Step 8: Create an HTML file, right click on Web Pages -> New -> HTML

20
Step 9: Give it a name. We recommend you to name it index, because browser
will always pick up the index.html file automatically from a directory. Index file is
read as the first page of the web application.

Step 10: Write some code inside your HTML file. We have created a hyperlink to
our Servlet in our HTML file.

21
Step 11: Edit web.xml file. In the web.xml file you can see, we have specified the
url-pattern and the servlet-name, this means when logout, profile url is accessed
our Servlet file will be executed.

22
Step 12: Run your application, right click on your Project and select Run

Step 13: Click on the link created, to open your Servlet.

23
24
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Servlet Login Example</title>
</head>
<body>

<h1>Login App using HttpSession</h1>


<a href="login.html">Login</a>|
<a href="LogoutServlet">Logout</a>|

25
<a href="ProfileServlet">Profile</a>

</body>
</html>

link.html
<a href="login.html">Login</a> |
<a href="LogoutServlet">Logout</a> |
<a href="ProfileServlet">Profile</a>
<hr>

login.html
<form action="LoginServlet" method="post">
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>

LoginServlet.java
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;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);

26
String name=request.getParameter("name");
String password=request.getParameter("password");

if(password.equals("admin123")){
out.print("Welcome, "+name);
HttpSession session=request.getSession();
session.setAttribute("name",name);
}
else{
out.print("Sorry, username or password error!");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}

LogoutServlet.java
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;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();

request.getRequestDispatcher("link.html").include(request, response);

HttpSession session=request.getSession();
session.invalidate();

27
out.print("You are successfully logged out!");

out.close();
}
}

ProfileServlet.java
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;
import javax.servlet.http.HttpSession;
public class ProfileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);

HttpSession session=request.getSession(false);
if(session!=null){
String name=(String)session.getAttribute("name");

out.print("Hello, "+name+" Welcome to Profile");


}
else{
out.print("Please login first");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}

28
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">

<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ProfileServlet</display-name>
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LogoutServlet</display-name>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>

29
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
</web-app>

Result:
Thus, the development of web application using netbeans environment
has been designed and executed successfully.

Ex No: 10 Database Connectivity with MySQL using Java Servlets, JSP, and
PHP

30
Aim
To create a Database Connectivity with MySQL using Java Servlets, JSP,
and PHP.

Algorithm for Java Servlet with MySQL:


Step 1: Proper JDBC Environment should set-up along with database creation.
Step 2: To do so, download the mysql-connector.jar file from the internet,
Step 3: As it is downloaded, move the jar file to the apache-tomcat server folder,
Step 4: Place the file in lib folder present in the apache-tomcat directory.
Step 5: To start with the basic concept of interfacing:
Step 5.1: Creation of Database and Table in MySQL
Step 5.2: Implementation of required Web-pages
Step 5.3: Creation of Java Servlet program with JDBC Connection
Step 5.4: To use this class method, create an object in Java Servlet
program
Step 5.5: Get the data from the HTML file

Program:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Insert Data</title>
</head>
<body>
<!-- Give Servlet reference to the form as an instances
GET and POST services can be according to the problem statement-->
<form action="./InsertData" method="post">
<p>ID:</p>
<!-- Create an element with mandatory name attribute,
so that data can be transfer to the servlet using getParameter() -->
<input type="text" name="id"/>
<br/>
<p>String:</p>
<input type="text" name="string"/>
<br/><br/><br/>

31
<input type="submit"/>
</form>
</body>
</html>

connection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// This class can be used to initialize the database connection
public class DatabaseConnection {
protected static Connection initializeDatabase()
throws SQLException, ClassNotFoundException
{
// Initialize all the information regarding
// Database Connection
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql:// localhost:3306/";
// Database name to access
String dbName = "demoprj";
String dbUsername = "root";
String dbPassword = "root";

Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbURL +
dbName,dbUsername,dbPassword);
return con;
}
}

dbservlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;

32
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Import Database Connection Class file
import code.DatabaseConnection;
// Servlet Name
@WebServlet("/InsertData")
public class InsertData extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
try {
// Initialize the database
Connection con = DatabaseConnection.initializeDatabase();
// Create a SQL query to insert data into demo table
// demo table consists of two columns, so two '?' is used
PreparedStatement st = con
.prepareStatement("insert into demo values(?, ?)");
// For the first parameter,
// get the data using request object
// sets the data to st pointer
st.setInt(1, Integer.valueOf(request.getParameter("id")));
// Same for second parameter
st.setString(2, request.getParameter("string"));
// Execute the insert command using executeUpdate()
// to make changes in database
st.executeUpdate();
// Close all the connections
st.close();
con.close();
// Get a writer pointer
// to display the successful result
PrintWriter out = response.getWriter();

33
out.println("<html><body><b>Successfully Inserted"
+ "</b></body></html>");
}
catch (Exception e) {
e.printStackTrace();
}
}
}

Output:

34
Algorithm for JSP with MySQL:
Step 1: Proper JDBC Environment should set-up along with database creation.
Step 2: To do so, download the mysql-connector.jar file from the internet,
Step 3: As it is downloaded, move the jar file to the apache-tomcat server folder,
Step 4: Place the file in lib folder present in the apache-tomcat directory.
Step 5: To start with the basic concept of interfacing:
Step 5.1: Creation of Database and Table in MySQL
Step 5.2: Implementation of required Web-pages
Step 5.3: Creation of JSP program with JDBC Connection
Step 5.4: Get the data from the HTML file
Program:
jsp_sql.jsp
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("userid");
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "test";
String userid = "root";
String password = "";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;

35
ResultSet resultSet = null;
%>
<!DOCTYPE html>
<html>
<body>

<h1>Retrieve data from database in jsp</h1>


<table border="1">
<tr>
<td>first name</td>
<td>last name</td>
<td>City name</td>
<td>Email</td>

</tr>
<%
try{
connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
statement=connection.createStatement();
String sql ="select * from users";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td><%=resultSet.getString("first_name") %></td>
<td><%=resultSet.getString("last_name") %></td>
<td><%=resultSet.getString("city_name") %></td>
<td><%=resultSet.getString("email") %></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}

36
%>
</table>
</body>
</html>

Output:

Algorithm for PHP with MySQL:

Step 1: Configure the Xampp server by start the Apache and MySql (download
“xampp-32-bit”).
Step 2: Call a wplab.html file with the values create, insert, filter display and
update.
Step 3: Execute the query and create database name and table name.
Step 4: Add the multiple rows into the table.
Step 5: Update the table and display.
Location:
C:\xampp\htdocs\10. php with mysql
Chrome Location: http://localhost/10. php with mysql

Program
wplab.html
<!DOCTYPE html>

37
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="wpcreatedbase.php" method="post">
<input type="submit" value="create">
</form>
<form action="wplabinsert.html" method="post">
<input type="submit" value="Insert">
</form>
<form action="wpdisplay.php" method="post">
<input type="submit" value="Filter & Display">
</form>
<form action="wpupdate.php" method="post">
<input type="submit" value="Update">
</form>
</body>
</html>
wpcreatedbase.php
<?php
$link = mysqli_connect("localhost", "root", "");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "CREATE DATABASE pacse1";
if(mysqli_query($link, $sql)){
echo "Database created successfully";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>

wpinsert.html

38
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="wpinsertform.php" method="post">
<p>
<label for="LabName">Lab Name:</label>
<input type="text" name="lab_name" id="LabName">
</p>
<p>
<label for="deptName">Department Name:</label>
<input type="text" name="dept_name" id="deptName">
</p>
<p>
<label for="palce">Place</label>
<input type="text" name="place" id="place">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>

wpdisplay.php

<?php
$link = mysqli_connect("localhost", "root", "", "pacet_cse");
if($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM wp_lab WHERE dept_name='cse'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){

39
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>lab_name</th>";
echo "<th>dept_name</th>";
echo "<th>place</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['lab_name'] . "</td>";
echo "<td>" . $row['dept_name'] . "</td>";
echo "<td>" . $row['place'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>

wpupdate.php

<?php
$link = mysqli_connect("localhost", "root", "", "pacet_cse");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "UPDATE wp_lab SET place='coimbatore1' WHERE id=20";
if(mysqli_query($link, $sql)){

40
echo "Records were updated successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>

Output:
Xampp Server:

41
42
43
Result:

44
Thus, the database connectivity with MySql using Java Servlets, JSP and
PHP was implemented and executed successfully.

45

You might also like