• Aucun résultat trouvé

Possible Alternatives to MySQL

Dans le document Beginning JSP, JSF and Tomcat (Page 195-199)

public int doEndTag() {

ServletContext context = pageContext.getServletContext();

DataManager dataManager =(DataManager)context.getAttribute("dataManager");

HttpSession session = pageContext.getSession();

@SuppressWarnings("unchecked") Hashtable<String, CartItem> cart =

(Hashtable<String, CartItem>)session.getAttribute("shoppingCart");

long orderID = dataManager.insertOrder(customer, cart);

if (orderID > 0L) session.invalidate();

pageContext.setAttribute(var, new Long(orderID).toString());

return EVAL_PAGE;

} }

Notice how you obtain the servlet context (corresponding to the JSP implicit object application) from pageContext, and from it the data manager, so that you can then execute the same insertOrder method you invoked directly from within the JSP page.

The highlighted line shows that I suppressed a warning. I did it because Eclipse kept complaining about typecasting of a generic Object to the Hashtable type. Normally, a warning tells you that something might be wrong. The use of @suppressWarnings is usually bad practice and encourages a sloppy programming style. In this particular case, I was left with no choice, because Eclipse’s warning was unjustified.

Possible Alternatives to MySQL

There’s no general reason why you shouldn’t use MySQL in your applications. Nevertheless, you do have alternatives worth mentioning. I have only tested E-shop with MySQL, but I expect it to work exactly the same with other DBMSs.

If you switch DBMSs, there’s a good chance that you’ll just need to change the values of the init parameters jdbcDriver and dbUrl in web.xml from these values for MySQL:

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/shop to the values for the other DBMS.

For example, for PostgreSQL (http://www.postgresql.org/), the values would look like this:

org.postgresql.Driver

jdbc:postgresql://localhost/shop

For Firebird (http://www.firebirdsql.org/), the values could look like this:

org.firebirdsql.jdbc.FBDriver

jdbc:firebirdsql:localhost/3050:D:\\Firebird Datafiles\\shop.fdb Sun Microsystems reports that 221 different JDBC drivers exist (see

http://developers.sun.com/product/jdbc/drivers). Therefore, you should be able to find the driver you need to connect to any database, although it might not be freely available.

If you don’t find the right JDBC driver or if it’s too expensive, you might be able to use the JDBC-ODBC bridge included in the JVM to connect to any ODBC-compliant database. ODBC refers to

an API supported by many database vendors on basically all operating systems. With the JDBC-ODBC bridge, you can also access Microsoft Excel files as if they were a database. For example, let’s suppose that you have the spreadsheet shown in Figure 6-6.

Figure 6-6. table.xls

To be able to access it via the JDBC-ODBC bridge, you first need to associate the file with an ODBC data source. To do so, go to Start h Settings h Control Panel h Administrative Tools h Data Sources (ODBC). There, click on the System DSN tab and then on the Add button, as shown in Figure 6-7.

Figure 6-7. ODBC Data Source control panel

This opens the Create New Data Source dialog. Scroll the list of possible data sources until you find Microsoft Excel Driver (*.xls). Select it and click on the Finish button. Despite the name of the button, you’re not done yet! A new dialog called ODBC Microsoft Excel Setup opens, which lets you select the Excel file and associate it with a data source name. See Figure 6-8.

Figure 6-8. ODBC Microsoft Excel setup

Click on the Select Workbook... button to select the file. Notice that I have placed table.xls in ROOT\tests\xls\, together with the JSP page to access it, but it doesn’t need to be there. Also, I have chosen tab as a data source name, but you’re free to choose any name.

Listing 6-18 shows you a little JSP page to access table.xls as if it were a database.

Listing 6-18. xls.jsp

<%@page language="java" contentType="text/html"%>

<%@page import="java.sql.*"%>

<html><head><title>XLS - ODBC test</title></head><body>

<%

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

Connection conn = DriverManager.getConnection ("jdbc:odbc:tab");

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select * from [zzz$]");

%><table border= "1"><%

ResultSetMetaData resMetaData = rs.getMetaData();

int nCols = resMetaData.getColumnCount();

%><tr><%

for (int kCol = 1; kCol <= nCols; kCol++) {

out.print("<td><b>" + resMetaData.getColumnName(kCol) + "</b></td>");

} %></tr><%

while (rs.next()) {

%><tr><%

for (int kCol = 1; kCol <= nCols; kCol++) {

out.print("<td>" + rs.getString(kCol) + "</td>");

} %></tr><%

}

%></table><%

conn.close();

%>

</body></html>

Notice that in the select statement, I have used [zzz$] as a table name to access the worksheet named zzz. Figure 6-9 shows the output of xsl.jsp.

Figure 6-9. The output of xls.jsp

One word of warning: you will fail to establish the Java connection if you have the file already open in Excel, because Excel opens it exclusively. It will not fail if, when you set up the data source as shown in Figure 6-8, you tick the Read Only box.

Summary

In this chapter, I introduced you to working with databases and SQL. I explained how to access databases from JSP via the Java SQL API. In particular, I showed you how to establish a connection, insert data, and perform queries. To complete the summary of essential DB operations, I also described how to group elementary updates into transactions.

To bring it all together, I described the design of database operations in the E-shop application and showed you their implementation both with scriptlets and with the XML syntax. Finally, I mentioned possible alternatives to MySQL and described how you can access a spreadsheet from JSP as if it were a database.

Brace yourself, because in the next chapter I will finally talk about JSF!

„ „ „

Dans le document Beginning JSP, JSF and Tomcat (Page 195-199)