// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
* My test servlet
*
* @author Liz Warner
*/
public class DBtest extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//print the HTML header </font>
out.println("<html>");
out.println("<head>");
out.println("<title> Hola </title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h1> Hi There again </h1>");
out.println("retrieving data now <br>");
// try to make a JDBC connection to the "test" database
// and retrieve some data
String DBUrl = "jdbc:mysql:///test";
Connection conn = null;
Statement stmt = null;
try
{
try
{
//create a new instance of the mysql driver
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception Ex)
{
out.println("Unable to Load Driver: " + Ex.toString() + "<br>");
}
// get a new connection object
conn = DriverManager.getConnection(DBUrl);
// get a new statement object
stmt = conn.createStatement();
// and execute a simple query
ResultSet rs = stmt.executeQuery("SELECT * from foo");
while (rs.next())
{
// iterate through the result set and print
// the query results to the page
out.println("bar = " + rs.getString("bar") + "<br>");
}
}
catch (SQLException sqlEx)
{
out.println("Caught SQL Exception: " + sqlEx.toString() + "<br>");
}
// now close the statement and connection if they exist
if (stmt != null)
{
try
{
stmt.close();
}
catch (SQLException sqlEx)
{
out.println("Could not close: " + sqlEx.toString() + "<br>");
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException sqlEx)
{
out.println("Could not close: " + sqlEx.toString() + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}
}