in this chapter, we will discuss how to access database with jsp. we assume you have good understanding on how jdbc application works. before starting with database access through a jsp, make sure you have proper jdbc environment setup along with a database.
for more detail on how to access database using jdbc and its environment setup you can go through our jdbc tutorial.
to start with basic concept, let us create a table and create a few records in that table as follows −
create table
to create the employees table in the emp database, use the following steps −
step 1
open a command prompt and change to the installation directory as follows −
c:\> c:\>cd program files\mysql\bin c:\program files\mysql\bin>
step 2
login to the database as follows −
c:\program files\mysql\bin>mysql -u root -p enter password: ******** mysql>
step 3
create the employee table in the test database as follows − −
mysql> use test;
mysql> create table employees
(
id int not null,
age int not null,
first varchar (255),
last varchar (255)
);
query ok, 0 rows affected (0.08 sec)
mysql>
create data records
let us now create a few records in the employee table as follows − −
mysql> insert into employees values (100, 18, 'zara', 'ali'); query ok, 1 row affected (0.05 sec) mysql> insert into employees values (101, 25, 'mahnaz', 'fatma'); query ok, 1 row affected (0.00 sec) mysql> insert into employees values (102, 30, 'zaid', 'khan'); query ok, 1 row affected (0.00 sec) mysql> insert into employees values (103, 28, 'sumit', 'mittal'); query ok, 1 row affected (0.00 sec) mysql>
select operation
following example shows how we can execute the sql select statement using jtsl in jsp programming −
<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>
<html>
<head>
<title>select operation</title>
</head>
<body>
<sql:setdatasource var = "snapshot" driver = "com.mysql.jdbc.driver"
url = "jdbc:mysql://localhost/test"
user = "root" password = "pass123"/>
<sql:query datasource = "${snapshot}" var = "result">
select * from employees;
</sql:query>
<table border = "1" width = "100%">
<tr>
<th>emp id</th>
<th>first name</th>
<th>last name</th>
<th>age</th>
</tr>
<c:foreach var = "row" items = "${result.rows}">
<tr>
<td><c:out value = "${row.id}"/></td>
<td><c:out value = "${row.first}"/></td>
<td><c:out value = "${row.last}"/></td>
<td><c:out value = "${row.age}"/></td>
</tr>
</c:foreach>
</table>
</body>
</html>
access the above jsp, the following result will be displayed −
| emp id | first name | last name | age |
|---|---|---|---|
| 100 | zara | ali | 18 |
| 101 | mahnaz | fatma | 25 |
| 102 | zaid | khan | 30 |
| 103 | sumit | mittal | 28 |
insert operation
following example shows how we can execute the sql insert statement using jtsl in jsp programming −
<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>
<html>
<head>
<title>jinsert operation</title>
</head>
<body>
<sql:setdatasource var = "snapshot" driver = "com.mysql.jdbc.driver"
url = "jdbc:mysql://localhost/test"
user = "root" password = "pass123"/>
<sql:update datasource = "${snapshot}" var = "result">
insert into employees values (104, 2, 'nuha', 'ali');
</sql:update>
<sql:query datasource = "${snapshot}" var = "result">
select * from employees;
</sql:query>
<table border = "1" width = "100%">
<tr>
<th>emp id</th>
<th>first name</th>
<th>last name</th>
<th>age</th>
</tr>
<c:foreach var = "row" items = "${result.rows}">
<tr>
<td><c:out value = "${row.id}"/></td>
<td><c:out value = "${row.first}"/></td>
<td><c:out value = "${row.last}"/></td>
<td><c:out value = "${row.age}"/></td>
</tr>
</c:foreach>
</table>
</body>
</html>
access the above jsp, the following result will be displayed −
| emp id | first name | last name | age |
|---|---|---|---|
| 100 | zara | ali | 18 |
| 101 | mahnaz | fatma | 25 |
| 102 | zaid | khan | 30 |
| 103 | sumit | mittal | 28 |
| 104 | nuha | ali | 2 |
delete operation
following example shows how we can execute the sql delete statement using jtsl in jsp programming −
<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>
<html>
<head>
<title>delete operation</title>
</head>
<body>
<sql:setdatasource var = "snapshot" driver = "com.mysql.jdbc.driver"
url = "jdbc:mysql://localhost/test"
user = "root" password = "pass123"/>
<c:set var = "empid" value = "103"/>
<sql:update datasource = "${snapshot}" var = "count">
delete from employees where id = ?
<sql:param value = "${empid}" />
</sql:update>
<sql:query datasource = "${snapshot}" var = "result">
select * from employees;
</sql:query>
<table border = "1" width = "100%">
<tr>
<th>emp id</th>
<th>first name</th>
<th>last name</th>
<th>age</th>
</tr>
<c:foreach var = "row" items = "${result.rows}">
<tr>
<td><c:out value = "${row.id}"/></td>
<td><c:out value = "${row.first}"/></td>
<td><c:out value = "${row.last}"/></td>
<td><c:out value = "${row.age}"/></td>
</tr>
</c:foreach>
</table>
</body>
</html>
access the above jsp, the following result will be displayed −
| emp id | first name | last name | age |
|---|---|---|---|
| 100 | zara | ali | 18 |
| 101 | mahnaz | fatma | 25 |
| 102 | zaid | khan | 30 |
update operation
following example shows how we can execute the sql update statement using jtsl in jsp programming −
<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>
<html>
<head>
<title>delete operation</title>
</head>
<body>
<sql:setdatasource var = "snapshot" driver = "com.mysql.jdbc.driver"
url = "jdbc:mysql://localhost/test"
user = "root" password = "pass123"/>
<c:set var = "empid" value = "102"/>
<sql:update datasource = "${snapshot}" var = "count">
update employees set where last = 'ali'
<sql:param value = "${empid}" />
</sql:update>
<sql:query datasource = "${snapshot}" var = "result">
select * from employees;
</sql:query>
<table border = "1" width = "100%">
<tr>
<th>emp id</th>
<th>first name</th>
<th>last name</th>
<th>age</th>
</tr>
<c:foreach var = "row" items = "${result.rows}">
<tr>
<td><c:out value = "${row.id}"/></td>
<td><c:out value = "${row.first}"/></td>
<td><c:out value = "${row.last}"/></td>
<td><c:out value = "${row.age}"/></td>
</tr>
</c:foreach>
</table>
</body>
</html>
access the above jsp, the following result will be displayed −
| emp id | first name | last name | age |
|---|---|---|---|
| 100 | zara | ali | 18 |
| 101 | mahnaz | fatma | 25 |
| 102 | zaid | ali | 30 |