[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>

Advertisements

[tutorial] Database Connection in JSP with JSTL

I wrote the post about how to connect JSP applications and your Database, and in this post I’ll write the post that has same purpose but in the different way. we’ll connect the database via JSTL.

The project in this tutorial was made in the same project before, I just make another page in the same project. Continue reading

[programming] Decision-Making in JSP

The basic structure of Decision-Making in  PHP programming Language and JSP Programming language is exactly the same. ‘Decision-Making in JSP also use IF…ELSE and SWITCH…CASE statements.

IF … ELSE statement

Most of decision-making structure refer to this statement. This statement is suitable for small case (one/two condition(s)) of decision making until big case decision-making (nested if/more than 2 conditions).

Structure : Continue reading

[programming] Processing Form Data In JSP

I miss this blog so much, another work loads. some applications to make, teaching schedule, and family’s event. And in this post I’ll show you how to process your data from form you made.

Before read the following tutorial, you should have basic knowledge about HTML, and Basic JSP.(about what to do with your computer before start to code in JSP)

At some point it feels familiar, and it will get more like family now. so come on.

  1. Insert and view data
    To access data that has been inserted into form basically you just need request.getParameter(“variable_name”) for example :

    <input name=address id=address type=text /> //text box in HTML
    <%
    String addr; //declare variable addr as String
    addr = request.getParameter("address"); // assign the form variable address into variable addr
    out.println(addr); //print the value of variable addr
    %>

    the syntax above will take data that has been inserted into address text box and print it into your page. All data that inserted into form is in String form. so when you want to insert an integer value you need to convert the value unto integer. For example :

    <input name=frprice id=frprice type=text />
    <%
    int price = Integer.parseInt(request.getParameter("frprice")); //define price variable as integer and convert the value of form variable frprice from string into integer.
    int count;
    count = price - (price * 5%);
    out.println(count);
    %>

    syntax above will convert string data from form variable named frprice into Integer and assign it into variable named price. Integer.parseInt(variable_name) in charge of converting string variable into integer.

[tutorial] Database Connection in JSP

have I told you that I am both a Programming teacher in Vocational High School and Lecturer in one of college in town. So, now in vocational high school we are in the second semester this year and based on the teaching subject that shared among the teachers, I -again- assigned to teach Java Server Page. I am a Web-Based-Application freak and I love web Programming. Web Programming allow me make the Program and designed the page at the same time. but I am more like a PHP Girl rather than a Java Woman, and I don’t want to cheat him… I don’t know, it isn’t like I hate Java, I just not so get along with him…. sorry Mr. Java.

But if the boss said that I have to teach it, so be it. When the First semester of the third class was learn about the basic of JSP, now this semester I will connect their application with the Database.

Application you might need : Continue reading