|
|||||
|
Database connections are an important part of presenting dynamic content. The following code snippet shows a simplified JSP page which connects to a database and displays the results of a query in a dynamically-generated HTML table.
<%@page language="java" import="java.sql.*"%> <% (Driver)Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection
Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database",
"username","password"); Statement
StatementRecordset1 = Conn.createStatement(); ResultSet
Recordset1 = StatementRecordset1.executeQuery("select * from mytable"); boolean
Recordset1_isEmpty = !Recordset1.next(); boolean
Recordset1_hasData = !Recordset1_isEmpty; int
Recordset1_numRows = 0; %> <% int
Repeat1__numRows = 10 ; int
Repeat1__index = 0 ; Recordset1_numRows
+= Repeat1__numRows; %>
<table> <% { %>
<tr>
<td><%= (String) Recordset1.getObject("Field1Name")
%></td>
<td><%= (String) Recordset1.getObject("Field2Name")
%></td>
<td><%= (String) Recordset1.getObject("Field3Name")
%></td>
<td><%= (String) Recordset1.getObject("Field4Name")
%></td>
</tr>
<%Repeat1__index++; Recordset1_hasData
= Recordset1.next();} %>
</table>
<% Recordset1.close(); Conn.close(); %> Note: MMA technical support staff cannot provide troubleshooting of problems with third-party CGI scripts including but not limited to: Perl scripts, C/C++ binaries, PHP or web/database integration unless these services are specifically contracted. For these services, please see our Quote Request Form. |
|||||
|
||||||
Copyright
© 1995-2000
Motivational Marketing Associates, LLC
All Rights Reserved.