[programming] Insert data into database with JSP via JSTL


I have lots of talented and reliable students around me, and I’m proud of them.Proud to meet them and teach them. I’m sooooo proud of them, wish they’ll have such a bright future ahead of them. I love them so much you know…..😀

And tribute to Riedo Andy Kurniawan who helped me to work things out. heeheheee… We finally able to insert data into the database. I’ve been try various ways to insert form data into the database, all of those various ways was come to their Dead end. I searched over dozens of internet article related with this JSP things and failed. it was frustrating, I never try this hard to do such things in PHP, it makes me love PHP even more…. hahahahaaa…..

so here is the code to insert form data into the database via JSTL.

<%@ page import=”java.io.*,java.util.*,java.sql.*”%>
<%@ page import=”javax.servlet.http.*,javax.servlet.*” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core&#8221; prefix=”c”%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/sql&#8221; prefix=”sql”%>
<!DOCTYPE html>
<html>
<head>
<sql:setDataSource var=”snap” driver=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://localhost:3306/data” user=”root” password=””
/>
<title>JSP Page</title>
</head>
<body><form method=”post”>
<table width=”228″ border=”1″>
<tr>
<td width=”54″>ID</td>
<td width=”8″>:</td>
<td width=”148″><label for=”nis”></label>
<input type=”text” name=”id” id=”id” /></td>
</tr>
<tr>
<td>NAMA</td>
<td>:</td>
<td><label for=”nama”></label>
<input type=”text” name=”nama” id=”nama” /></td>
</tr>
<tr>
<td>KELAS</td>
<td>:</td>
<td><label for=”alamat”></label>
<input type=”text” name=”kelas” id=”kelas” /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type=”submit” name=”submit” id=”submit” value=”Submit” /></td>
</tr>
</table>
</form>

<c:if test=”${pageContext.request.method==’POST’}”>
<c:catch var=”exception”>
<sql:update dataSource=”${snap}” var=”updatedTable”>
INSERT INTO siswa VALUES (?,?,?)
<sql:param value=”${param.id}” />
<sql:param value=”${param.nama}” />
<sql:param value=”${param.kelas}” />
</sql:update>
<c:if test=”${updatedTable>=1}”>
<span style=”font-weight:bold; color:green;” >Congratulations ! Data inserted
successfully.</span>
</c:if>
</c:catch>
<c:if test=”${exception!=null}”>
<c:out value=”Unable to insert data in database.” />
</c:if>
</c:if>
</body>
</html>

One thought on “[programming] Insert data into database with JSP via JSTL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s