Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 122 123 124 125 126 127 < 128 > 129 130 131 132 133 134 .. 166 >> Next

Usage of Site by Browser
Browser Usage
Figure 40.3 Working version.
Sun recommends if you are running a server-side application without any graphical user interface that you specify this option.
Figure 40.3 is the outcome of making this 24-character addition to our JVM invocation.
Item 41: The Problem with Multiple Concurrent Result Sets
Over the years, we've run into many situations where we have needed to write Java applications that have the capability of connecting to "any database." If there is a JDBC driver available for that database, this shouldn't be a problem. In those situations, we typically design our application, write the code, and initially test our application against one type of database. Requirements for the development of many applications beg the need for query builders—graphical user interfaces that allow users to click on a selection of tables and columns from the database, allowing them to create advanced queries. Of course, if you have created an application like this, you know that the java.sql.DatabaseMetaData class allows you to get column and table information from the database.
Before you build this query builder application, you need to write a simple method that will print this database information to the screen. Later, you will probably change this method to return a structure with all of the tables and columns of the database. Listing 41.1 shows a portion of your code, where you get the table information from the database and print out each table and column of the database.
354 Item 41
01 /**
02 * this goes through the database and lists all the
03 * tables and columns of the database.
04 *
05 * @throws exception
06 */
07 public void printMetaDataStuff(Connection conn) throws Exception
08 {
09 String[] tabletypes = {"TABLE"};
10
11 if (conn == null)
12 {
13 throw new Exception("Could not establish a connection!");
14 }
15
16 DatabaseMetaData dmd = conn.getMetaData();
17
18 ResultSet rs = dmd.getTables(null,null,null,tabletypes);
19
20 if (rs == null)
21 {
22 throw new Exception("No metadata!");
23 }
24 while (rs.next())
25 {
26 String table = rs.getString("TABLE_NAME");
27
28 System.out.println("ResultSet 1: Got table " + table);
29 ResultSet rs2 = dmd.getColumns(null,null,table,null);
30
31 if (rs2 == null)
32 {
33 throw new Exception ("No Metadata!");
34 }
35 while (rs2.next())
36 {
37 String col = rs2.getString("COLUMN_NAME");
38 System.out.println("ResultSet2: Table " + table +
39 " has column " + col);
40 }
41 }
42 }
Listing 41.1 printMetaDataStuff() from BadResultSet.java
The Problem with Multiple Concurrent Result Sets 355
Initially, you test your code with one database, and it works fine. Many developers stop there and continue to build their application. However, just to be on the safe side, you decide to test this class against several databases running on different operating systems, including Microsoft Access, Microsoft SQL Server, MySQL, Sybase, and Oracle. Your class takes a database URL and driver class as an argument, builds a connection, and passes it to your printMetaDataStuff() function in Listing 41.1. You write a script that passes the JDBC URL and the driver class to the application, shown in Listing 41.2.
echo testing Access
java BadResultSet jdbc:odbc:TestAccessDB sun.jdbc.odbc.JdbcOdbcDriver echo testing SQL Server
java BadResultSet jdbc:odbc:TestSQLServer sun.jdbc.odbc.JdbcOdbcDriver testuser testpass
echo testing MySQL
java BadResultSet jdbc:mysql://testmachine:3306/metadotdb org.gjt.mm.mysql.Driver testuser testpass
echo testing Sybase
java BadResultSet jdbc:sybase:Tds:testmachine:3000/TestDB com.sybase.jdbc2.jdbc.SybDriver testuser testpass
echo testing ORACLE
java BadResultSet jdbc:oracle:thin:@testmachine:1521:dppo oracle.jdbc.driver.OracleDriver testuser testpass
Listing 41.2 Script for testing
As you can see in Listing 41.2, you are testing this application against different databases using different drivers. The output is shown in Listing 41.3. As you can see, many of these databases (MS Access, MySQL, and Sybase) were able to run printMeta-DataStuff() from Listing 41.1 with no problems. However, the SQL Server driver throws a java.lang.SQLException, saying that the "Connection is busy with the results of another hstmt." The Oracle database throws an ORA-1000 error, saying that you have exceeded your maximum open cursors. What happened?
356 Item 41
testing Access
ResultSet 1 Got table EQP
ResultSet2: Table EQP has column ACQUIRED_CC
ResultSet2: Table EQP has column ACQUIRED_DATE
ResultSet2: Table EQP has column ACTIVITY
ResultSet 1 Got table EQP_AFLD
ResultSet2: Table EQP_AFLD has column ARRESTING_BARRIER
ResultSet2: Table EQP_AFLD has column ARRESTING_CABLE
ResultSet2: Table EQP_AFLD has column BAYS_QTY
testing SQL Server
ResultSet 1: Got table AuthorizedUsers
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Connection is
Previous << 1 .. 122 123 124 125 126 127 < 128 > 129 130 131 132 133 134 .. 166 >> Next