Oracle 5.0 Reference Manual page 2132

Table of Contents

Advertisement

<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<html>
<head><title>Hello world from JSP</title></head>
<body>
<%
InitialContext ctx;
DataSource ds;
Connection conn;
Statement stmt;
ResultSet rs;
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MySQLDataSource");
//ds = (DataSource) ctx.lookup("jdbc/MySQLDataSource");
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM Country");
while(rs.next()) {
%>
<h3>Name: <%= rs.getString("Name") %></h3>
<h3>Population: <%= rs.getString("Population") %></h3>
<%
}
}
catch (SQLException se) {
%>
<%= se.getMessage() %>
<%
}
catch (NamingException ne) {
%>
<%= ne.getMessage() %>
<%
}
%>
</body>
</html>
In addition two XML files are required: web.xml, and sun-web.xml. There may be other files present,
such as classes and images. These files are organized into the directory structure as follows:
index.jsp
WEB-INF
|
- web.xml
- sun-web.xml
The code for
is:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
<display-name>HelloWebApp</display-name>
<distributable/>
<resource-ref>
<res-ref-name>jdbc/MySQLDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
The code for
sun-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http:
<sun-web-app>
<context-root>HelloWebApp</context-root>
Using Connector/J with GlassFish
is:
2112

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents