Batbayar Jamiyan
 

Энэ бол миний тэмдэглэл хөтлөх зай. Шүлэг нийтлэл болон зарим сонирхолтой зүйлийн талаар оруулах болно. Та сэтгэгдэлээ үлдээх боломжтой шүү.

Add to Favorites Send me an e-mail
Interesting websites
Other sites
robins4 Robin Mulder
prevodilac Esada Ciric-Delic
zamzm Ali Yousef
shauspc pc shau
souvaripiu souvari koner
Top commentators
joy Batbayar Jamiyan
Comments: 5
Visitors
Calendar
<
May 2012
>
MTWTFSS
 123456
78910111213
14151617181920
21222324252627
28293031
Subscription
E-mail: 
Vote
Намайг таних уу?

Vote results
Back to homeBatbayar Jamiyan / Blog / Тэмдэглэл / Connecting to MySQL database and retrieving and displaying data in JSP page

Connecting to MySQL database and retrieving and displaying data in JSP page

Posted byText

joy Send a message
Batbayar Jamiyan
Connecting to MySQL database and retrieving and displaying data in JSP page
2020 days ago 17.11.2006 22:50:28 Quote('55852','55852','5','521')">Report spam

This tutorial shows you how to connect to MySQL database and retrieve the data from the database. In this example we will use tomcat version 4.0.3 to run our web application.

Creating Table in the database.

Using a JDBC driver

org.gjt.mm.mysql.Driver Driver to connect to the database.

Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 

Connecting to your database

To connect to the database getConnection() function of the DriverManager class is used.

con=DriverManager.getConnection(url);

 

Executing Query and Processing result set

In this example we will use a table called jakartaproject having five fields in table. We will use the following query to retrieve all the records from the table:

select * from jakartaproject 

Here is the code of our JSP file

<%@ page language="java" import="java.sql.*"%>
<html>
<head><title>Read from mySQL Database</title>
</head>
<body>

<p align="center"><b>Following records are selected from the 'jakartaproject' table.</b><br>&nbsp;</p>

<div align="center" width="85%">
<center>
<table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="658" height="63">
<tbody>
<td bgColor="#008080" width="47" align="center" height="19"><font color="#ffffff"><b>Sr.
No.</b></font></td>
<td bgColor="#008080" width="107" height="19"><font color="#ffffff"><b>Project</b></font></td>
<td bgColor="#008080" width="224" height="19"><font color="#ffffff"><b>Url
Address</b></font></td>
<td bgColor="#008080" width="270" height="19"><font color="#ffffff"><b>Description
of the project</b></font></td>

<%
String DRIVER = "org.gjt.mm.mysql.Driver";
Class.forName(DRIVER).newInstance();


Connection con=null;
ResultSet rst=null;
Statement stmt=null;

try{
String url="jdbc:mysql://192.168.10.2/tutorial?user=tutorial&password=tutorial";

int i=1;
con=DriverManager.getConnection(url);
stmt=con.createStatement();
rst=stmt.executeQuery("select * from jakartaproject ");
while(rst.next()){

if (i==(i/2)*2){
%>
<tr>
<td bgColor="#ffff98" vAlign="top" width="47" align="center" height="19"><%=i%>.</td>
<td bgColor="#ffff98" vAlign="top" width="107" height="19"><%=rst.getString(2)%></td>
<td bgColor="#ffff98" vAlign="top" width="224" height="19"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%></a>&nbsp;</td>
<td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(4)%></td>
</tr>
<%
}else{
%>
<tr>
<td bgColor="#ffcc68" vAlign="top" width="47" align="center" height="19"><%=i%>.</td>
<td bgColor="#ffcc68" vAlign="top" width="107" height="19"><%=rst.getString(2)%></td>
<td bgColor="#ffcc68" vAlign="top" width="224" height="19"><a href="<%=rst.getString(3)%>"><%=rst.getString(3)%></a>&nbsp;</td>
<td bgColor="#ffcc68" vAlign="top" width="270" height="19"><%=rst.getString(4)%></td>
</tr>
<% }

i++;
}
rst.close();
stmt.close();
con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
%>

</tbody>
</table>
</center>
</div>


</body>
</html>

Deploying web application

Put the code in tomcat and test the application through browser. The browser should display the display the data stored in the table.


---
Joy

Comments: 0 Views: 906
Tags: jsp
Login Password
advanced... ( / Registration )

Subject

In the text you can use Wiki or HTML tags.



Who is active on the site?
Anonymous: 6, Registered: 0 (?)
Abuse | Hosted by MyLivePage | | © Kolobok smiles, Aiwan