You are on page 1of 15

Ex.No.

: JSP - Library Management System


Date:

Aim:
Program to develop a Library Management System application accessing a database using JSP.

Procedure:

Relations using MYSQL given below enforcing primary key and foreign keyconstraints: BOOK

(ACCNO, TITLE, AUTHOR, PUBLISHER, EDITION, PRICE) MEMBER (MID, MNAME,

BRANCH)

FINE (MID, FINE_DATE, AMOUNT)

1. Open MySQL.

2. Create a database.

mysql> create database library;


Query OK, 1 row affected (0.02 sec)

3. Connect to the database.

mysql> use library;


Database changed

4. Create the following tables:

mysql> create table book (accno integer, title varchar(20),


-> author varchar(20), publisher varchar(20),
-> edition integer, price integer, primary key (accno));
Query OK, 0 rows affected (0.09 sec)

mysql> create table member (mid integer, mname varchar(20), ->


branch varchar(20), primary key (mid));
Query OK, 0 rows affected (0.06 sec)

mysql> create table fine (mid integer references member(mid),


-> fine_date date, amount integer);
Query OK, 0 rows affected (0.06 sec)
Program:

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Library Management System</title> </head>
<body>
<h1 align="center">Library Management System</h1> <hr />
<a href="Book.html">Book Details</a> </body>
</html>

Book.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Details</title>
</head>
<body>
<h1 align="center">Book Details</h1> <hr />
<form action="AddBook.jsp" method="post"> <table>
<tr>
<td>Acc. No. :</td> <td><input type="text" name="accno"></td> </tr>
<tr>
<td>Title :</td> <td><input type="text" name="title"></td> </tr>
<tr>
<td>Author :</td> <td><input type="text" name="author"></td> </tr>
<tr>
<td>Publisher :</td> <td><input type="text" name="publisher"></td> </tr>
<tr>
<td>Edition :</td> <td><input type="text" name="edition"></td> </tr>
<tr>
<td>Price :</td> <td><input type="text" name="price"></td> </tr>
<tr>
<td colspan="2" align="center"><inputtype="submit" value="Add Book"></td> </tr>
</table>
<br /> <a href="ViewBooks.jsp">View All Books</a>
</form>
</body>
</html>

AddBook.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-


8859-1" import="java.sql.*"%> <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add Book Details</title>
</head>
<body>
<h1 align="center">Add Book Details</h1> <hr />
<%
Connection conn = null; PreparedStatement ps = null; try {
Class.forName("com.mysql.cj.jdbc.Driver");
String URL = "jdbc:mysql://localhost:3306/library"; conn = DriverManager.getConnection(URL,
"root", "admin"); ps = conn.prepareStatement("insert into book values (?, ?, ?, ?, ?, ?)");
ps.setInt(1,Integer.parseInt(request.getParameter("accno"))); ps.setString(2,
request.getParameter("title")); ps.setString(3, request.getParameter("author")); ps.setString(4,
request.getParameter("publisher")); ps.setInt(5,
Integer.parseInt(request.getParameter("edition")));
ps.setInt(6,
Integer.parseInt(request.getParameter("price")));
int res = ps.executeUpdate(); if (res != 0)
out.println("Book Details Inserted Successfully..."); else
out.println("Book Details Insertion Failure...");
} catch (Exception e) { out.println(e); }
ps.close();
conn.close();
%>
<br />
<a href="Book.html">Back</a>
</body>
</html>

ViewBooks.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-
8859-1" import="java.sql.*"%> <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>View All Book Details</title>
</head>
<body>
<h1 align="center">All Book Details</h1> <hr />
<%
Connection conn = null;
PreparedStatement ps = null; ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String URL = "jdbc:mysql://localhost:3306/library"; conn = DriverManager.getConnection(URL,
"root", "admin"); ps = conn.prepareStatement("select * from book order by accno");
rs = ps.executeQuery(); %>
<table border="1">
<tr>
<td>Acc No.</td>
<td>Title</td>
<td>Author</td>
<td>Publisher</td>
<td>Edition</td>
<td>Price</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<%
while (rs.next()) { %>
<tr>
<td><%=rs.getInt("accno")%></td> <td><%=rs.getString("title")%></td>
<td><%=rs.getString("author")%></td> <td><%=rs.getString("publisher")%></td>
<td><%=rs.getInt("edition")%></td> <td><%=rs.getInt("price")%></td> <td><a
href="EditBook.jsp?accno=
<%=rs.getInt("accno")%>">Edit</a></td> <td><a href="DeleteBook.jsp?accno=
<%=rs.getInt("accno")%>">Delete</a></td>
</tr>
<%
}
%>
</table>
<%
} catch (Exception e) {
out.println(e);
}
rs.close();
ps.close();
conn.close();
%>
<br />
<a href="Book.html">Back</a>
</body>
</html>

EditBook.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-


8859-1" import="java.sql.*"%> <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Edit Book Details</title>
</head>
<body>
<h1 align="center">Edit Book Details</h1> <hr />
<%
Connection conn = null;
PreparedStatement ps = null; ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String URL = "jdbc:mysql://localhost:3306/library"; conn = DriverManager.getConnection(URL,
"root", "admin"); ps = conn.prepareStatement("select * from book where accno = ?");
ps.setInt(1,
Integer.parseInt(request.getParameter(("accno"))));
rs = ps.executeQuery(); rs.next();
%>
<form action="UpdateBook.jsp" method="post"> <table>
<tr>
<td>Acc. No. :</td> <td><input type="text" name="accno" value="<%=rs.getInt("accno")%>"
readonly></td>
</tr>
<tr>
<td>Title :</td> <td><input type="text" name="title" value="<%=rs.getString("title")%>"></td>
</tr>
<tr>
<td>Author :</td> <td><input type="text" name="author"
value="<%=rs.getString("author")%>"></td> </tr>
<tr>
<td>Publisher :</td> <td><input type="text" name="publisher"
value="<%=rs.getString("publisher")%>"></td> </tr>
<tr>
<td>Edition :</td> <td><input type="text" name="edition"
value="<%=rs.getInt("edition")%>"></td> </tr>
<tr>
<td>Price :</td> <td><input type="text" name="price" value="<%=rs.getInt("price")%>"></td>
</tr>
<tr>
<td colspan="2" align="center"><inputtype="submit" value="Update Book"></td> </tr>
</table>
</form>
<%
} catch (Exception e) {
out.println(e);
}
rs.close();
ps.close();
conn.close();
%>
<br />
<a href="ViewBooks.jsp">Back</a>
</body>
</html>

UpdateBook.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-


8859-1" import="java.sql.*"%> <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Update Book Details</title>
</head>
<body>
<h1 align="center">Update Book Details</h1> <hr />
<%
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String URL = "jdbc:mysql://localhost:3306/library"; conn = DriverManager.getConnection(URL,
"root", "admin"); ps = conn.prepareStatement( "update book set title = ?, author = ?, publisher = ?,
edition = ?, price = ? where accno = ?"); ps.setString(1, request.getParameter("title"));
ps.setString(2, request.getParameter("author")); ps.setString(3, request.getParameter("publisher"));
ps.setInt(4,
Integer.parseInt(request.getParameter("edition")));
ps.setInt(5,
Integer.parseInt(request.getParameter("price")));
ps.setInt(6,
Integer.parseInt(request.getParameter("accno")));
int res = ps.executeUpdate(); if (res != 0)
out.println("Book Details Updated Successfully...");
else
out.println("Book Details Updation Failure...");
} catch (Exception e) {
out.println(e);
}
ps.close();
conn.close();
%>
<br />
<a href="ViewBooks.jsp">Back</a>
</body>
</html>

DeleteBook.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-


8859-1" import="java.sql.*"%> <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Delete Book Details</title>
</head>
<body>
<h1 align="center">Delete Book Details</h1> <hr />
<%
Connection conn = null;
PreparedStatement ps = null; try {
Class.forName("com.mysql.cj.jdbc.Driver");
String URL = "jdbc:mysql://localhost:3306/library"; conn = DriverManager.getConnection(URL,
"root", "admin");
ps = conn.prepareStatement("delete from book where accno = ?");
ps.setInt(1,
Integer.parseInt(request.getParameter(("accno"))));
int res = ps.executeUpdate(); if (res != 0)
out.println("Book Details Deleted Successfully...");
else
out.println("Book Details Deletion Failure...");
} catch (Exception e) {
out.println(e);
}
ps.close();
conn.close();
%>
<br />
<a href="ViewBooks.jsp">Back</a>
</body>
</html>

Output:
Result:
Program to develop a Library Management System application accessing a database using JSP has
been developed.
Ex.No.: AJAX – Web Page Login Process
Date:

Aim:
Program to implement the concepts of AJAX for web page login process.

Procedure:

Relations using MYSQL for a banking application given belowenforcingprimarykeyandforeign key


constraints:
USERDETAILS (UNAME, PASSWD)
1. Open MySQL.
2. Create a database.
mysql> create database rec;
Query OK, 1 row affected (0.05 sec)
3. Connect to the database.
mysql> use rec;
Database changed
4. Create the following tables:
mysql> create table userdetails(uname varchar(20) primary key, -> passwd varchar(20) not null);
Query OK, 0 rows affected (0.08 sec)
5. Insert the following rows:
mysql> insert into userdetails values('admin', 'rec123');
Query OK, 1 row affected (0.01 sec)
mysql> insert into userdetails values('bhuvan', 'reccseit');
Query OK, 1 row affected (0.01 sec)

Program:

home.php
<?php
include "config.php";
// Check user login or not
if(!isset($_SESSION['uname'])){
header('Location: index.php');
}
// logout
if(isset($_POST['but_logout'])){
session_destroy();
header('Location: index.php');
}
?>
<!doctype html>
<html>
<head>
<title>Login page with jQuery and AJAX</title> </head>
<body>
<h1>Welcome to REC Homepage</h1> <form method='post' action="">
<input type="submit" value="Logout" name="but_logout"> </form>
</body>
</html>

Index.php

<!doctype html>
<html>
<head>
<title>Login page with jQuery and AJAX</title> <link href="style.css" rel="stylesheet"
type="text/css">
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript"> $(document).ready(function(){
$("#but_submit").click(function(){ var username = $("#txt_uname").val().trim();
var password = $("#txt_pwd").val().trim();
if( username != "" && password != "" ){ $.ajax({
url:'checkUser.php', type:'post', data:{username:username,password:password},
success:function(response){ var msg = ""; if(response == 1){
window.location = "home.php";
}else{
msg = "Invalid username and password!"; }
$("#message").html(msg); }
});
}
});
});
</script>
</head>
<body>
<div class="container">
<div id="div_login">
<h1>Login</h1>
<div id="message"></div>
<div>
<input type="text" class="textbox" id="txt_uname" name="txt_uname" placeholder="Username"/>
</div>
<div>
<input type="password" class="textbox" id="txt_pwd" name="txt_pwd" placeholder="Password"/>
</div>
<div>
<input type="button" value="Submit" name="but_submit"id="but_submit" />
</div>
</div>
</div>
</body>
</html>

config.php

<?php
session_start();
$host = "localhost"; /* Host name */ $user = "root"; /* User */
$password = "admin"; /* Password */ $dbname = "rec"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname); // Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error()); }
?>

checkUser.php

<?php
include "config.php";
$uname = mysqli_real_escape_string($con,$_POST['username']);
$password = mysqli_real_escape_string($con,$_POST['password']);
if ($uname != "" && $password != ""){ $sql_query = "SELECT count(*) as cntUser FROM
userdetails WHERE uname='".$uname."' and passwd='".$password."'"; $result =
mysqli_query($con,$sql_query); $row = mysqli_fetch_array($result);
$count = $row['cntUser'];
if($count > 0){
$_SESSION['uname'] = $uname; echo 1;
}else{
echo 0;
}
}

style.css

/* Container */
.container{
width:40%;
margin:0 auto;
}
/* Login */
#div_login{
border: 1px solid gray; border-radius: 3px; width: 470px;
height: 270px;
box-shadow: 0px 2px 2px 0px gray; margin: 0 auto;
}
#div_login h1{
margin-top: 0px;
font-weight: normal;
padding: 10px;
background-color: cornflowerblue; color: white;
font-family: sans-serif; }
#div_login div{
clear: both;
margin-top: 10px;
padding: 5px;
}
#div_login .textbox{
width: 96%;
padding: 7px;
}
#div_login input[type=submit]{ padding: 7px;
width: 100px;
background-color: lightseagreen; border: 0px;
color: white;
}
#message{
width:100%;
text-align:center;
color:red;
}
/* media */
@media screen and (max-width:720px){ .container{
width: 100%;
}
#div_login{
width: 99%;
}
}

Output:
Result:
Program to implement the concepts of AJAX for web page login process has been developed.

You might also like