Listing 17 - Listing 17.1 NorthwindTest.java package...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Listing 17.1 NorthwindTest.java package coreservlets; import java.sql.*; /** A JDBC example that connects to the MicroSoft Access sample * Northwind database, issues a simple SQL query to the * employee table, and prints the results. */ public class NorthwindTest { public static void main(String args) { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String url = "jdbc:odbc:Northwind"; String username = ""; // No username/password required String password = ""; // for desktop access to MS Access. showEmployeeTable(driver, url, username, password); } /** Query the employee table and print the first and * last names. */ public static void showEmployeeTable(String driver, String url, String username, String password) { try { // Load database driver if it's not already loaded. Class.forName(driver); // Establish network connection to database. Connection connection = DriverManager.getConnection(url, username, password); System.out.println("Employees\n" + "=========="); // Create a statement for executing queries. Statement statement = connection.createStatement(); String query = "SELECT firstname, lastname FROM employees"; // Send query to database and store results. ResultSet resultSet = statement.executeQuery(query); // Print results. while(resultSet.next()) { System.out.print(resultSet.getString("firstname") + " "); System.out.println(resultSet.getString("lastname")) ; } connection.close(); } catch(ClassNotFoundException cnfe) { System.err.println("Error loading driver: " + cnfe); } catch(SQLException sqle) { System.err.println("Error with connection: " + sqle); } } } Listing 17.2 NorthwindTest Result Prompt> java coreservlets.NorthwindTest Employees ========== Nancy Davolio Andrew Fuller Janet Leverling Margaret Peacock Steven Buchanan Michael Suyama Robert King Laura Callahan Anne Dodsworth . Listing 17.3 NorthwindServlet.java package coreservlets; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; /** A simple servlet that connects to a database and * presents the results from the query in an HTML * table. The driver, URL, username, password, * and query are taken from form input parameters. */ public class NorthwindServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\"\n"; String title = "Northwind Results"; out.print(docType + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\"><CENTER>\n" + "<H1>Database Results</H1>\n"); String driver = request.getParameter("driver"); String url = request.getParameter("url"); String username = request.getParameter("username"); String password = request.getParameter("password"); String query = request.getParameter("query"); showTable(driver, url, username, password, query, out); out.println("</CENTER></BODY></HTML>");out....
View Full Document

Page1 / 30

Listing 17 - Listing 17.1 NorthwindTest.java package...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online