Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
The JSP Files (part 5): No Forwarding Address
Build dynamic, data-driven Web pages with JSP.

| The Scenic Route |

All working? Good. Now, let's use JSP to do exactly the same thing - fire a SELECT query at the database, and display the results in an HTML page.


<html>
<head>
<basefont face="Arial">
</head>
<body>
<%@ page language="java" import="java.sql.*" %>

<%!
// define variables
String UId;
String FName;
String LName;

// define database parameters
String host="localhost";
String user="us867";
String pass="jsf84d";
String db="db876";
String conn;
%>

<table border="2" cellspacing="2" cellpadding="5">

<tr>
<td><b>Owner</b></td>
<td><b>First name</b></td>
<td><b>Last name</b></td>
</tr>

<%

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

// create connection string
conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" +
pass;

// pass database parameters to JDBC driver
Connection Conn = DriverManager.getConnection(conn);

// query statement
Statement SQLStatement = Conn.createStatement();

// generate query
String Query = "SELECT uid, fname, lname FROM abook";

// get result
ResultSet SQLResult = SQLStatement.executeQuery(Query);

while(SQLResult.next())
{
UId = SQLResult.getString("uid");
FName = SQLResult.getString("fname");
LName = SQLResult.getString("lname");

out.println("<tr><td>" + UId + "</td><td>" + FName + "</td><td>" + LName
+ "</td></tr>");
}

// close connection
SQLResult.close();
SQLStatement.close();
Conn.close();

%>

</table>
</body>
</html>


And you'll see something like this:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Owner First name Last name
john Bugs Bunny
john Elmer Fudd
joe Peter Parker
bill Clark Kent
').'
'


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback