How can we access database using JSP?

 We also access database by using JSP. All JDBC APIs can be accessed from JSP. We have put all codes related to database access within the expression tag.

//addbook.html File

<html>

<head>

<Title> E-book Store </Title>

</head>

<body>

<br><center>

<h2>Adding New Book</h2> <hr color=blue size=3>

<form action="addbook.jsp" method="post">

<table>

<tr>

<td> ISBN </td>

<td><input type=text name=ISBN></td>

</tr>

<tr>

<td> Book Title </td>

<td><input type=text name=title></td>

</tr>

<tr>

<td> Authors</td></tr

<td><input type=text name=authors></td>

</tr>.

<tr>

<td> Price </td>

<td><input type=text name=price></td>

</tr>

<tr>

<td> Publisher </td>

<td><input type=text name=pub></td>

</tr>

<tr>

<td

colspan=2 align=center> <input type="submit" value="Add Book">

</td>

</tr>

</table>

</form>

</body>

</html>

/addbook.jsp File

<html>

<body>

<%

String ISBN = request.getParameter("ISBN");

String title = request.getParameter("title");

String auth = request.getParameter("authors"); int price = Integer.parseInt(request.getParameter("price")); String pub=request.getParameter("pub");

%>

<h2><center>

<font face="Tw Cen MT" color=blue>

<%@ page import ="java.sql.*" %> 

<%

Connection con= DriverManager.getConnection ("jdbc: mysql://localhost/ebook", "root", "arjun");

Statement stmt = conn.createStatement ();

String sql ="insert into books values ('"+ISBN'" , +title + "','"+ "'+auth+'" "+price+", '" +pub+"')";

stmt.executeUpdate (sql);

out.println("Book is Addes Successfully");

%>

</font><center><h2>

</body>

</html>

Output

Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Discuss classification or taxonomy of virtualization at different levels.

Pure Versus Partial EC