Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Display Data from Database in JSP

This is detailed java program to connect java application with mysql database and execute query to display data from the specified table.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

Display Data from Database in JSP

  

This is detailed java program to connect java application with mysql database and execute query to display data from the specified table. 

Before running this java code you need to copy a mysql connector jar file (mysql-connector-java-3.1.6-bin.jar) in the jdk1.6.0_01\lib and set class path for this jar file.

This is first jsp page that has a link 'show data from table', user can show all the data from table after clicking link. This is the code of first welcome jsp page.

welcome_to_database_query.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>display data from the table using jsp</title>
</head> 
<body>
<TABLE style="background-color: #ffffcc;">
<TR>
<TD align="center"><h2>To display all the data from the 
table click here...</h2></TD>
</TR>
<TR>
<TD align="center"><A HREF="ConnectJspToMysql.jsp">
<font size="4" color="blue">show data from table</font>
</A></TD>
</TR>
</TABLE>
</body> 
</html>
 

Create a new directory in Tomcat-6.0.16/webapps/ and Save this code with the name "welcome_to_database_query.jsp" in Tomcat-6.0.16/webapps/user along with WEB-INF directory. Start tomcat server and type url 'http://localhost:8080/user/welcome_to_database_query.jsp' in address bar of browser and run.

This page has a link, to show data from the database click on the link that calls another jsp file named ConnectJspToMysql.jsp

ConnectJspToMysql.jsp page creates connection between application and database. In this code connection is between application and table name 'stu_info' in database 'student'.

ConnectJspToMysql.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>display data from the table using jsp</title>
</head>
<body>
<h2>Data from the table 'stu_info' of database 'student'</h2>
<%
try {
/* 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 connection by using Connection interface
Connection connection = null;
/* declare object of Statement interface that is used for executing sql 
statements. */
Statement statement = null;
// declare a resultset that uses as a table for output data from tha table.
ResultSet rs = null;
// Load JBBC 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();
// sql query to retrieve values from the secified table.
String QueryString = "SELECT * from stu_info";
rs = statement.executeQuery(QueryString);
%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getInt(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><%=rs.getString(3)%></TD>
<TD><%=rs.getString(4)%></TD>
</TR>
<% } %>
<%
// close all the connections.
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</TABLE><TABLE>
<TR>
<TD><FORM ACTION="welcome_to_database_query.jsp" method="get" >
<button type="submit"><-- back</button></TD>
</TR>
</TABLE>
</font>
</body>
</html>

Save this code with name ConnectJspToMysql.jsp in the same dirctory of welcome_to_database_query.jsp. Click on the link given in the first jsp page,that calls this jsp page and show all data from the table.

Click on the <--back button to go to first page of the application.

Download source code

Advertisements

Ads
Share on Google+Share on Google+

Display Data from Database in JSP

Posted on: July 2, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Display Data from Database in JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
Win Ngo
March 30, 2012
Thank....

Thank you very much!
srishti
July 2, 2012
print objects

what if we have made a class for the data and want to print an object?
Jagadeesh Bathini
July 6, 2012
code wanted

please upload upload code
javaclick
August 22, 2012
unable to connect with database

using this source code i am getting message that unable to connect with database. how do i overcome this?
Jack
March 26, 2013
Thanks

Very useful! Thank you!
Wizard
May 15, 2013
Thanks

Thanks a lot , u guys are doing a fantastic job, these postings of yours are valuable to many
soleman
May 5, 2013
very clear and thanks alot

thank you very much
Rajashekhar G C
August 3, 2013
JSP

This is really useful tutorial, thanks to roseindia
sundeep
September 21, 2013
jsp

awesome...thank u so much...

Ads

 

Ads