![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Source Listing 10-17 contains the source code for this example. Listing 10-17: rxblob.java. import java.sql.*; import java.io.*; import java.util.*; public class rxblob { static DataInputStream kbd = new DataInputStream(System.in); static String url = "jdbc:odbc:netbank"; static String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; static String login = "dba"; static String passwd = "javabank"; static String filename = ""; static String tablename = ""; static String blobcolumnname = ""; static String selectcolumnname = ""; static String selectcolumnvalue = ""; static Connection curConn = null; public static void main(String argv[]) throws IOException { String temp = ""; if ((argv[0] != null) && (argv[0].equals("-c"))) { tablename = argv[1]; blobcolumnname = argv[2]; selectcolumnname = argv[3]; selectcolumnvalue = argv[4]; filename = argv[5]; } else { System.out.println("Simple tool to retrieve BLOBS, by Bernard Van Haecke, 1996.\n"); System.out.print("Enter the url or [ENTER] for " + url + " : "); System.out.flush(); temp = kbd.readLine(); if (!temp.equals("")) url = temp; System.out.print("Enter the login or [ENTER] for " + login + " : "); System.out.flush(); temp = kbd.readLine(); if (!temp.equals("")) login = temp; System.out.print("Enter the passwd or [ENTER] for " + passwd + " : "); System.out.flush(); temp = kbd.readLine(); if (!temp.equals("")) passwd = temp; System.out.print("\nEnter the table name : "); System.out.flush(); tablename = kbd.readLine(); System.out.print("Enter the blob column name : "); System.out.flush(); blobcolumnname = kbd.readLine(); System.out.print("Enter the row selection criteria column name : "); System.out.flush(); selectcolumnname = kbd.readLine(); System.out.print("Enter the row selection criteria value : "); System.out.flush(); selectcolumnvalue = kbd.readLine(); System.out.print("Enter the file name : "); System.out.flush(); filename = kbd.readLine(); } rxblob session = new rxblob(); } public rxblob() throws IOException { try { Class.forName(driver); curConn = DriverManager.getConnection(url, login, passwd); } catch(java.lang.Exception ex) { System.out.println("url : " + url); System.out.println("login : " + login); System.out.println("passwd : " + passwd); ex.printStackTrace(); return; } processBlob(); finalize(); } protected void finalize() { try { curConn.close(); } catch (SQLException ex) { } } private void processBlob() throws IOException { try { java.io.File blobFile = new java.io.File(filename); java.io.OutputStream fblob = new java.io.FileOutputStream(blobFile); java.sql.Statement myStatement = curConn.createStatement(); ResultSet rs = myStatement.executeQuery("SELECT " + blobcolumnnam e + " FROM " + tablename + " WHERE " + selectcolumnna me + " = " + selectcolumnval ue); // we retrieve in 4K chunks byte[] buffer = new byte[4096]; int size; if (rs.next()) { // fetch blob java.io.InputStream strin = rs.getBinaryStream(blobcolumnname); for (;;) { size = strin.read(buffer); if (size == 0) { break; } // Send the buffer to some output stream fblob.write(buffer, 0, size); } } else System.out.println("Row not found."); myStatement.close(); rs.close(); } catch (SQLException ex) { // Unexpected SQL exception. System.out.println(ex); } catch (java.lang.Exception ex) { // Got some other type of exception. Dump it. ex.printStackTrace (); } } } Dealing with Database TransactionsAs seen in the section dedicated to database transactions, transactions group the execution of a number of SQL statements to maintain consistency in multiuser environments. In the following example, we group multiple SQL INSERT statements to ensure consistent financial transactions by using database transactions. The Bank of JavaThis Java applet is similar to the software provided by banks to their customers to perform operations on their accounts from their home computers with telephones and modems. It was simplified to illustrate database transactions with JDBC. The main operations are these:
The information about clients and their accounts is, of course, stored in a database. The data structure was kept simple to allow a quick understanding of the whole application and the transaction mechanism. Database transactions are performed in the main thread, while a second thread serves as a clock. Each time an update is made to the database, the SQL queries that perform the update are grouped in a single transaction unit. By doing so, no inconsistent update can bring the database to an incoherent state. If the client-server link is broken in the middle of a transaction, the transaction will be canceled; otherwise, it is committed. Canceling a transaction is called transaction rollback. This mechanism prevents unlogged transfers or unlogged withdrawals. Indeed, transfers and withdrawals are SQL INSERTs in database tables such as accounts and history log.
|
![]() |
|