You are on page 1of 9

27/12/2017 JSTL 1.

1 : simple insert, update, delete example | javaknowledge

Home About Contact Privacy Policy DMCA Partners PrimeFaces Core Java
Swing Jasper report Android Sitemap

JSTL 1.1 : simple insert, update, delete example


Search...
Posted on December 1, 2012 by admin 5 comments

JAVA Standard Tag Library(JSTL) provides basic functionalities for JSP.


The above example shows a basic insert, update and delete example Follow Us!
using core and sql tag libraries.

1 CREATE DATABASE IF NOT EXISTS loginjdbc;


2 CREATE TABLE `product` (
3 `id` int(10) unsigned NOT NULL auto_increment,
4 `pname` varchar(45) NOT NULL,
5 `quantity` int(10) unsigned NOT NULL,
6 PRIMARY KEY (`id`)
7 );
8 Recent Posts
9 INSERT INTO `product` (`id`,`pname`,`quantity`) VA
10 (1,'Mouse',50), Struts2 Ajax Validation
11 (2,'Keyboard',5),
12 (3,'Monitor',34); Struts2 Multiple
Configuration File

1. index.jsp Struts2 Multiple


Namespace
1 <%@page contentType="text/html" pageEncoding="UTF-
2 <!DOCTYPE html> Struts2 Introduction
3 <html>
4 <head> URL Rewriting Example
5 <meta http-equiv="Content-Type" content="t in JSF 2.0 using
6 <title>JSP Page</title>
7 </head> PrettyFaces
8 <body>
9 <h1>Choose Option</h1>
10 <a href="insert.jsp">Insert Record</a><p><
11 <a href="display.jsp">Display Record</a>
12 </body> Recent
13 </html> Comments

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 1/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

admin: Does the same


2. insert.jsp thing happening when
you run from th ...
1 <%@page contentType="text/html" pageEncoding="UTF-
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" admin: Are you running
3 <!DOCTYPE html> your application from
4 <html> jar file? If ...
5 <head>
masudranasabuj:
6 <meta http-equiv="Content-Type" content="t
Anyone in any age may
7 <title>JSP Page</title> learn anything. It’s a ...
8 </head>
9 <body>
10 <form action="insertdb.jsp" method="post">
11 <table border="0" cellspacing="2" cell
12 <thead>
13 <tr>
14 <th colspan="2">Purchase P Categories
15 </tr>
16 </thead> ► Android (19)
17 <tbody> ► Core Java (37)
18 <tr>
19 <td><label>Product Name</l ► Free Projects (2)
20 <td><input type="text" nam
► Hibernate (5)
21 </tr>
22 <tr> ► Jasper report (7)
23 <td><label>Quantity</label
24 <td><input type="text" nam
► JSF 2.0 (14)
25 </tr> ► JSP & Servlet (9)
26 <tr>
27 <td><input type="submit" v ▼ JSTL 1.1 (4)
28 <td><input type="reset" va » Jsp, Servlet, JSTL and
29 </tr> MySQL Simple
30 </tbody>
31 </table> CRUD Application
32 </form> » JSTL 1.1 : Paginate
33 <font color="red"><c:if test="${not empty
34 <c:out value="${param.errMsg}" /> Table Example with
35 <a href="index.jsp">Go Back</a> Next Previous
36 </c:if></font>
Features
37 <font color="green"><c:if test="${not empt
38 <c:out value="${param.susMsg}" /> » JSTL 1.1 : simple
39 <a href="index.jsp">Go Back</a> insert, update,
40 </c:if></font>
41 delete example
42 </body> » JSTL 1.1 : simple
43 </html>
login and logout
example with
3. insertdb.jsp session
► NetBeans (2)
1 <%@ page import="java.io.*,java.util.*,java.sql.*"
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" ► PrimeFaces (8)
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" ► Software (3)
4
5 <html> ► Spring (1)

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 2/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

6 <head> ► Struts2 (5)


7 <title>JINSERT Operation</title>
8 </head> ► Tomcat (2)
9 <body>
10 <c:if test="${ empty param.pname or empty
11 <c:redirect url="insert.jsp" >
12 <c:param name="errMsg" value="Plea
13 </c:redirect> Tag Cloud
14
15 </c:if>
16 <sql:setDataSource var="dbsource" driver="
17 url="jdbc:mysql://local
18 user="root" password="
19
20
21 <sql:update dataSource="${dbsource}" var="
22 INSERT INTO product(pname, quantity) V
23 <sql:param value="${param.pname}" />
24 <sql:param value="${param.qty}" />
25 </sql:update>
26 <c:if test="${result>=1}">
27 <font size="5" color='green'> Congratu
28 successfully.</font>
29
30 <c:redirect url="insert.jsp" >
31 <c:param name="susMsg" value="Cong
32 successfully." />
33 </c:redirect>
34 </c:if> Meta
35
36 Site Admin
37 </body>
38 </html> Log out

Entries RSS
4. display.jsp Comments RSS
1 <%@ page import="java.io.*,java.util.*,java.sql.*" WordPress.org
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql"
4
5 <html>
6 <head>
7 <title>SELECT Operation</title>
Archives
8 <script>
Archives
9 function confirmGo(m,u) {
Select Month
10 if ( confirm(m) ) {
11 window.location = u;
12 }
13 }
14 </script>
15 </head>
Connect on
16 <body> Facebook
17
18 <sql:setDataSource var="dbsource" driver="
19 url="jdbc:mysql://local

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 3/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

20 user="root" password="
21 Javaknowledge
3,747 likes
22 <sql:query dataSource="${dbsource}" var="r
23 SELECT * from product;
24 </sql:query>
25 <center>
26 <form> Like Page Sig
27 <table border="1" width="40%">
28 <caption>Product List</caption> Be the first of your friends to like this
29 <tr>
30 <th>Product ID</th>
31 <th>Product Name</th>
32 <th>Quantity</th>
33 <th colspan="2">Action</th>
34 </tr>
35 <c:forEach var="row" items="${resu
36 <tr> Tweets by
37 <td><c:out value="${row.id @java_knowledge
38 <td><c:out value="${row.pn
39 <td><c:out value="${row.qu
40 <td><a href="update.jsp?id javaknowl…
41 <td><a href="javascript:co @java_kn…
42 Android Apps
43 </tr> Development Bangla
44 </c:forEach> Tutorial. fb.me/KLAg2npl
45 </table>
46 </form> May 6, 2017
47 <a href="index.jsp">Go Home</a>
48 </center>
javaknowl…
49 </body>
50 </html> @java_kn…

Embed View on Twitter

5. update.jsp

1 <%@page contentType="text/html" pageEncoding="UTF-


2 <%@ page import="java.io.*,java.util.*,java.sql.*"
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"
4 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql"
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta http-equiv="Content-Type" content="t
9 <title>JSP Page</title>
10 </head>
11 <body>
12
13 <sql:setDataSource var="dbsource" driver="
14 url="jdbc:mysql://local
15 user="root" password="
16
17 <sql:query dataSource="${dbsource}" var="r
18 SELECT * from product where id=?;
19 <sql:param value="${param.id}" />
20 </sql:query>
21 <form action="updatedb.jsp" method="post">

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 4/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

22 <table border="0" width="40%">


23 <caption>Update Product</caption>
24 <tr>
25 <th>Product Name</th>
26 <th>Quantity</th>
27 </tr>
28 <c:forEach var="row" items="${resu
29 <tr>
30 <td><input type="hidden" v
31 <input type="text" val
32 <td><input type="text" val
33 <td><input type="submit" v
34 </tr>
35 </c:forEach>
36 </table>
37 <a href="index.jsp">Go Home</a>
38 </form>
39 </body>
40 </html>

6. updatedb.jsp

1 <%@page contentType="text/html" pageEncoding="UTF-


2 <%@ page import="java.io.*,java.util.*,java.sql.*"
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"
4 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql"
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta http-equiv="Content-Type" content="t
9 <title>JSP Page</title>
10 </head>
11 <body>
12 <sql:setDataSource var="dbsource" driver="
13 url="jdbc:mysql://local
14 user="root" password="
15 <sql:update dataSource="${dbsource}" var="
16 UPDATE product SET pname = ?, quantity
17 WHERE id='${param.id}'
18 <sql:param value="${param.pname}" />
19 <sql:param value="${param.qty}" />
20 </sql:update>
21 <c:if test="${count>=1}">
22 <font size="5" color='green'> Congratu
23 successfully.</font>
24 <a href="index.jsp">Go Home</a>
25 </c:if>
26 </body>
27 </html>

7. deletedb.jsp

1 <%@page contentType="text/html" pageEncoding="UTF-


2 <%@ page import="java.io.*,java.util.*,java.sql.*"

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 5/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"


4 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql"
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta http-equiv="Content-Type" content="t
9 <title>JSP Page</title>
10 </head>
11 <body>
12 <sql:setDataSource var="dbsource" driver="
13 url="jdbc:mysql://local
14 user="root" password="
15 <sql:update dataSource="${dbsource}" var="
16 DELETE FROM product
17 WHERE id='${param.id}'
18 </sql:update>
19 <c:if test="${count>=1}">
20 <font size="5" color='green'> Congratu
21 successfully.</font>
22 <a href="index.jsp">Go Home</a>
23 </c:if>
24 </body>
25 </html>

Output:

JSTL 1.1 insert window

JSTL 1.1 display window

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 6/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

Related Posts

Jsp, Servlet, JSTL and Simple CRUD Using JSTL 1.1 : simple login
MySQL Simple CRUD JSF2+Hibernate and logout example with
Application Integration and MySQL session

JSTL 1.1 : Paginate Table Login and Registration Populate JTree from
Example with Next Example in JSP with MySQL Database
Previous Features Session

Tweet Share 25 Like 25 Simpan Share

admin
Hello! I am Md. Abdul Bari; founder and
admin of javaknowledge; currently
providing training as a J2EE faculty in The
Computers Ltd. Dhaka, Bangladesh under
an IT Scholarship project provided by IDB-
BISEW. I am a self learner and passionate
about training and writing. I am always
trying my best to share my knowledge
through my blog.
More Posts - Website
Follow Me:

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 7/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

tagged with delete, insert, jstl, update

JSTL 1.1
82,198` Total Views

LEAVE A REPLY

Logged in as onnnay. Log out?

Comment

Post Comment

5 COMMENTS

yenayev October 16, 2013 11:13 pm

Very good tutorial

Reply

newuser February 28, 2014 12:48 pm

hi I am very new to JSTL this website is very


helpful.
can u please help me to do update operation in
single page instead of navigating?

Reply

Trong-Huan April 17, 2014 4:50 pm

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 8/9
27/12/2017 JSTL 1.1 : simple insert, update, delete example | javaknowledge

I followed your tutorial, but after I ran the


project I had an error. I cant insert and display
data from database. Please help me!

Reply

mostafa June 11, 2014 7:10 pm

Sharing your knowledge makes of you a noble


man. Thanks.

Reply

Rakesh August 5, 2014 12:41 am

rg.apache.jasper.JasperException:
javax.servlet.ServletException:
javax.servlet.jsp.JspException:
SELECT * from product;
: ORA-00911: invalid character

when i insert a new record then the 500 internal server error
arise how i resolve it.

Reply

Journey to the way of Java...

http://www.javaknowledge.info/jstl-1-1-simple-insert-update-delete-example/ 9/9