![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Sending BLOBs Very large binary data will be sent using input/output (I/O) streams and setBinaryStream(). It is unnecessary to send the data in small chunks as is required for receiving BLOBs because the JDBC driver will make repeated calls on the I/O stream to read its content and send it to the database as the actual parameter data. Listing 7-3 illustrates an insertion of BLOBs using input streams. Listing 7-3: Sending very large parameters to the database. ... ... Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); java.io.File pictFile = new java.io.File(jones.jpeg); java.io.File audioFile = new java.io.File(jones.au); int pictFileLen = (int) pictFile.length(); int audioFileLen = (int) audioFile.length(); java.io.InputStream fPict = new java.io.FileInputStream(pictFile); java.io.InputStream fAudio = new java.io.FileInputStream(audioFile); PreparedStatement myStmt = myConnection.prepareStatement( UPDATE employees SET emp_pict = ?, emp_welcome = ? WHERE id = ?); myStmt.setBinaryStream(1, fPict, pictFileLen); myStmt.setBinaryStream(2, fAudio, audioFileLen); myStmt.setInt(3, 1); int res = myStmt.executeUpdate(); myStmt.close(); myConnection.close(); ... ... Metadata InterfacesMetadata interfaces are useful to query a database or a ResultSet for meta information. A programmer normally will not use the DatabaseMetaData interface; however, it provides many interesting methods for discovering database behaviors, default values, supported functions, and so forth. The ResultSetMetaData interface will probably be used more often, because it provides information on ResultSets that are, indeed, results of user queries. Information on Database ObjectsThe scope of database metadata is very broad. One of its most interesting uses is to obtain information on the database objects themselves. Connection DatabaseMetaData getMetaData(); DatabaseMetaData getMetaData(); A database can provide information on its objects, such as tables, stored procedures, SQL grammar, and various properties. All this information is obtainable through methods that apply to a DatabaseMetaData object. The code getMetaData() returns such an object. The DatabaseMetaData InterfaceThe DatabaseMetaData interface provides a number of methods to access database metadata. The methods fit into different categories, such as minor information on the database itself, information on what kind of features it supports, on its limitations, and on all database objects it contains. Most of them return string, boolean, or integer values, but a number of them return ResultSets. How to deal with ResultSets is discussed later. Miscellaneous Database InformationThe DatabaseMetaData interface is a rich interface. It provides many methods useful to discover the specifics of the database. DatabaseMetaData boolean allProceduresAreCallable(); boolean allTableAreSelectable(); String getURL(); String getUserName(); boolean isReadOnly(); boolean nullsAreSortedHigh(); boolean nullsAreSortedLow(); boolean nullsAreSortedAtStart(); boolean nullsAreSortedAtEnd(); String getDatabaseProductName(); String getDatabaseProductVersion(); String getDriverName(); String getDriverVersion(); int getDriverMajorVersion(); int getDriverMinorVersion(); boolean usesLocalFiles(); boolean usesLocalFilePerTable(); boolean supportsMixedCaseIdentifiers(); boolean storesUpperCaseIdentifiers(); boolean storesLowerCaseIdentifiers(); boolean storesMixedCaseIdentifiers(); boolean supportsMixedCaseQuotedIdentifiers(); boolean storesUpperCaseQuotedIdentifiers(); boolean storesLowerCaseQuotedIdentifiers(); boolean storesMixedCaseQuotedIdentifiers(); String getIdentifierQuoteString(); String getSQLKeywords(); String getNumericFunctions(); String getStringFunctions(); String getSystemFunctions(); String getTimeDateFunctions(); String getSearchStringExcape(); String getExtraNameCharacters(); Listing 7-4 illustrates how to get various kinds of information using some of the DatabaseMetaData object methods. Listing 7-4: DatabaseMetaData // databasemetadata import java.sql.*; class SimpleExample { public static void main(String args[]) { String url = jdbc:odbc:mysource; try { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); DatabaseMetaData mtdt = myConnection.getMetaData(); System.out.println(URL in use: + mtdt.getURL()); System.out.println(User name: + mtdt.getUserName()); System.out.println(DBMS name: + mtdt.getDatabaseProductName()); System.out.println(DBMS version: + mtdt.getDatabaseProductVersion()); System.out.println(Driver name: + mtdt.getDriverName()); System.out.println(Driver version: + mtdt.getDriverVersion()); System.out.println(supp. SQL Keywords: + mtdt.getSQLKeywords()); myConnection.close(); } catch(java.lang.Exception ex) { ex.printStackTrace(); } } } The examples output may be similar to the following code, which is particular to Sybase System 11. URL in use: jdbc:odbc:mysource User name: javauser DBMS name: SQL Server DBMS version: SQL Server/11.0/P/Sun_svr4/OS 5.4/1/OPT/Thu Dec 7 23:58:01 PST 1995 Driver name: JDBC-ODBC Bridge (SYSYB95.DLL) Driver version: 1.0101 (02.12.0000) supp. SQL Keywords: arith_overflow,break,browse,bulk,char_convert,checkpoint,clustered,commit,compute,confirm,controlrow,data_pgs,database,dbcc,disk,dummy,dump,endtran,errlvl,errorexit,exit,fillfactor,holdlock,identity_insert,if,kill,lineno,load,mirror,mirrorexit,noholdlock,nonclustered,numeric_truncation,offsets,once,over,perm,permanent,plan,print,proc,processexit,raiserror,read,readtext,reconfigure,replace,reserved_pgs,return,role,rowcnt,rowcount,rule,save,setuser,shared,shutdown,some,statistics,stripe,syb_identity,syb_restree,syb_terminate,temp,textsize,tran,trigger,truncate,tsequal,used_pgs,user_option,waitfor,while,writetext
|
![]() |
|