Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Create dynamic page through JSP 
 

in this section, you will learn how to create dynamic page in jsp.

 

Create dynamic page through JSP

                         

This is detailed jsp code that shows how to create dynamic page. This code shows one by one record of student from database on click a button named 'next record'. On each click of button, application fetch next record from database.

Create a database :  Before running this jsp code, first create a database named 'student' through the sql query given below in mysql command prompt :-

mysql> create database student;

Then create a table named 'stu_info' in the same database through following sql query :-

create table stu_info (
         ID int not null auto_increment,
         Name  varchar(20),
         Address varchar(20),
         Phone varchar(15),
         primary key(ID)
);

Create your application directory named "user" in the Tomcat server. Before running this java code you need mysql connector jar in the Tomcat-6.0.16/webapps/user/WEB-INF/lib.

ConnectJspToMysql.jsp:

This file provides full code how to connect with database and showing the next record of database in JSP.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<HTML>
<HEAD>
    <TITLE>Navigating in a Database Table </TITLE>
</HEAD>
<BODY bgcolor="#ffffcc">
<font size="+3" color="green"><br>Welcome in www.roseindia.net !</font>
<br>
<font size="+1" color="#6E6E6E"><br>This is dynamic page that shows data<br>
from database as per user choice.</font>
<%          int current = 0;
            // declare a connection by using Connection interface 
            Connection connection = null;
            /* Create string of connection url within specified format with machine 
		name, port number and database name. Here machine name id localhost 
		and database name is student. */
            String connectionURL = "jdbc:mysql://localhost:3306/student";
            /*declare a resultSet that works as a table resulted by execute a specified 
	      sql query. */
            ResultSet rs = null;
%>
<FORM NAME="form1" ACTION="ConnectJspToMysql.jsp" METHOD="get"><%
            if (request.getParameter("hidden") != null) {
                current = Integer.parseInt(request.getParameter("hidden"));
            }
            // Declare statement.
            Statement statement = null;
            try {
                // Load JDBC driver "com.mysql.jdbc.Driver"
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                /* Create a connection by using getConnection() method that takes 
		parameters of string type connection url, user name and password to 
		connect to database. */
                connection = DriverManager.getConnection(connectionURL, "root", "root");
                /* createStatement() is used for create statement object that is 
		used for sending sql statements to the specified database. */
                statement = connection.createStatement();
                // executeQuery() method execute specified sql query. 
                rs = statement.executeQuery("select * from stu_info");
                for (int i = 0; i < current; i++) {
                    rs.next();
                }
                if (!rs.next()) {
%>
<FONT size="+2" color="red"></b>
<%
        out.println("Sorry ! found some problems with database.");
    } else {
%>
<TABLE style="background-color: #ECE5B6;" WIDTH="30%" >
    
    <TR><TH width="50%">ID</TH><TD width="50%"> <%= rs.getInt(1)%> </TD></tr>
    <TR><TH>Name</TH><TD> <%= rs.getString(2)%> </TD></tr>
    <TR><TH>City</TH><TD> <%= rs.getString(3)%> </TD></tr>
    <TR><TH>Phone</TH><TD> <%= rs.getString(4)%> </TD></tr>    
    </TR>
</TABLE>
<BR>
<INPUT TYPE="hidden" NAME="hidden" VALUE="<%=current + 1%>">
<INPUT TYPE="submit" VALUE="next record">
</FORM>
<%
    }
} 
catch (Exception ex) {
%>
<FONT size="+3" color="red"></b>
    <%
                out.println("Unable to connect to database.");
            } finally {
                // close all the connections.
                rs.close();
                statement.close();
                connection.close();
            }
    %>
</FONT>
</FORM>
</body> 
</html>

 

Save this code as a .jsp file named "ConnectJspToMysql.jsp" in the directory Tomcat-6.0.16/webapps/user/ and run this jsp page with following url in address bar of the browser "http://localhost:8080/user/ConnectJspToMysql.jsp".

When click on button, application control goes to database and check for the next record. If next record is found in table, it is shown in browser else an error message.

Error message page:

Download Source code

                         

» View all related tutorials
Related Tags: java c exception class control io multiple find ip exec lock state int block exe row with matching match statement

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.