Making Text for the Web part 6 Barb Ericson Georgia Institute of Technology March 2006 Georgia Institute of Technology Doing a Join Query • Add aliases for the table names – Picture as pict, Person as per, PicturePerson as pictPer • Use the alias in front of the field name – per.FirstName, pict.FileName • Use And to join conditions Where per.firstName = 'Jennifer' And pictPer.PersonID = per.PersonID And pict.PictureID = pictPer.PictureID Georgia Institute of Technology Finding Jennifer /* main for testing */ public static void main(String[] args) { // create the database manager for an Access database DatabaseManager dbManager = new DatabaseManager("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:person"); // create the database manager for a MySQL database // new DatabaseManager("com.mysql.jdbc.Driver", // "jdbc:mysql://localhost:3306/person"); // test a query dbManager.testQuery("Select per.FirstName, " + "pict.FileName From " + "Picture as pict, Person as per, " + "PicturePerson as pictPer " + "Where per.FirstName = 'Jennifer' And " + "pictPer.PersonID = per.PersonID And " + "pict.PictureID = pictPer.PictureID", 2); } Georgia Institute of Technology Getting a String for a Query /** * Method to execute a query and return a string of the * first result * @param query the query to execute * @return the first result of the query, or null if none */ public String getStringForQuery(String query) { String result = null; // create a statement Statement statement = connection.createStatement(); // execute the query ResultSet rs = statement.executeQuery(query); // print out the results if (rs.next()) { result = rs.getString(1); } // try the following try { // close everything rs.close(); statement.close(); connection.close(); // open the connection to the database Connection connection = DriverManager.getConnection(this.urlStr); } catch (SQLException ex) { SimpleOutput.showError("Trouble with the database " + urlStr); ex.printStackTrace(); } return result; } Georgia Institute of Technology Adding Database Info to a Homepage • Now we can use a database to get information for an HTML page • Modify the writeHomepage method to get the person's age from the database – Use the DatabaseManager class to get it Georgia Institute of Technology Write Homepage with Database Info /** * Method for writing a homepage for the passed * first name that displays her/his interests and age * @param name the person's first name * @param interests a list of the person's interests */ public void writeHomepageV5(String name, String interests) { // Get a DatabaseManager object DatabaseManager dbManager = new DatabaseManager("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:person"); // try the following try { // open a file for writing BufferedWriter writer = new BufferedWriter(new FileWriter(name + ".html")); // write the start writeStart(writer); // write the header writeHead(writer,name + "'s Homepage"); // write the body writeBody(writer,"<h1>Welcome to " + name + "'s Homepage</h1>" + "<p> I am interested in " + interests + ".</p><p>I am " + age + " years old</p>"); // get this person's age String age = dbManager.getStringForQuery( "Select Age From Person " + "Where FirstName='" + name + "'"); // end the page writeEnd(writer); // close the writer writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } Georgia Institute of Technology Testing the Homepage Writer public static void main(String[] args) { WebPageWriter writer = new WebPageWriter(); writer.writeHomepageV5("Matthew", "playing video games"); } Georgia Institute of Technology Exercise • Create a writeHomepageV6 method that looks up the person's name in the database and adds thumbnails of pictures of that person to the page • Add information to the database for your pictures – Add to the Person table – Add to the Picture table – Add to the PicturePerson table • Use the writeHomepage method to generate a homepage for you – With your pictures on it Georgia Institute of Technology Servlets and JSP • Some database access is done through JavaServer Pages (JSPs) – HTML pages with special tags that allow Java code to be executed – JSP pages are translated into servlets • Small programs like applets – But they run on the server • Servlets connect to the database and get information – Which is displayed in the HTML Georgia Institute of Technology Summary • HTML is the language of the Web – Not a programming language – You can use Java programs to create HTML • Helper methods are often private methods that are used by other public methods – To break a task into parts (procedural abstraction) • Throwing an exception – A method can throw an exception using the throws keyword followed by the exception name • The Unnamed Package – All classes are put in a package. If you don't specify a package the "unnamed" one is used Georgia Institute of Technology Summary Continued • Maps store key and value pairs – Map is an interface – HashMap, Hashtable, and TreeMap are classes that implement Map • Generics allow you to declare the types of things in collection classes – and eliminate the need to downcast when you take items back out of a collection • Iterators let you walk through each item in a collection • Using classes in the java.sql package you can access information in a database – SQL is the standard language to use to communicate with a database Georgia Institute of Technology