[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>
<sql:setDataSource var=”snap” driver=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://localhost:3306/data” user=”root” password=””
<title>JSP Page</title>
<body><form method=”post”>
<table width=”228″ border=”1″>
<td width=”54″>ID</td>
<td width=”8″>:</td>
<td width=”148″><label for=”nis”></label>
<input type=”text” name=”id” id=”id” /></td>
<td><label for=”nama”></label>
<input type=”text” name=”nama” id=”nama” /></td>
<td><label for=”alamat”></label>
<input type=”text” name=”kelas” id=”kelas” /></td>
<td><input type=”submit” name=”submit” id=”submit” value=”Submit” /></td>

<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}” />
<c:if test=”${updatedTable>=1}”>
<span style=”font-weight:bold; color:green;” >Congratulations ! Data inserted
<c:if test=”${exception!=null}”>
<c:out value=”Unable to insert data in database.” />