⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 JAVA Developing Professional JavaApplets
💻 HTM
📖 第 1 页 / 共 5 页
字号:
&nbsp;&nbsp;&nbsp;&nbsp;public String getRow(int row)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws DBException<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( !query )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thrownew DBException(&quot;No active query&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if ( row&gt;= nRows )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thrownew DBException(&quot;Row out of bounds&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String buildResult= new String(&quot;&quot;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( int x =0; x &lt; nCols; x++ )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buildResult+= (result[row])[x] + &quot; |&quot;;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return buildResult;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * Retreive the contents of a column.<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @param row, col is the column to retreive<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @exception DBException if invalid rowor column<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;public String getColumn(int row, int col)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws DBException<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( !query )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thrownew DBException(&quot;No active query&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if ( row&gt;= nRows )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thrownew DBException(&quot;Row out of bounds&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if ( col&gt;= nCols )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thrownew DBException(&quot;Column out of bounds&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return result[row][col];<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public void allDone(String str)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(str);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * Display the contents of the statement<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp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sp;&nbsp;&nbsp;&nbsp;catch(DBException de)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(de);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}</TT></BLOCKQUOTE><HR><P>The SQLStmt class has public methods to enable extracting querydata in an orderly manner: <TT>numRows()</TT>,<TT>numCols()</TT>, <TT>getRow()</TT>,and <TT>getColumn()</TT>. SQLStmtsare two-way objects-they hold both the input and the output data.The output is contained in a two-dimensional array of Strings.This scheme forces the database interface library to translateall table columns into String format.<P>A new exception has been defined with this class. It can be foundon this book's CD-ROM in the file DBException.java. This exceptionis thrown by the SQLStmt class when a request is made, but noquery has been attempted. The Database class, discussed in thefollowing paragraphs, also throws DBExceptions.<P>The method <TT>allDone()</TT> willbe called by the native library and gives the library a convenientway to print. It serves no other purpose.<P>Although the SQLStmt class contains all the database information,it does not actually interface with the native library. For thistask, the Database class is used. This class is much simpler thanthe data container; its chief role is to interface with the nativemethods. Listing 10.5 shows the Database class.<HR><BLOCKQUOTE><B>Listing 10.5. The Database class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>import java.lang.*;<BR>import java.io.*;<BR>import SQLStmt;<BR>import DBException;<BR><BR>/**<BR> * Class to allow access to the database library<BR> */<BR>public class Database<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;// Table name to use (ODBC data source)<BR>&nbsp;&nbsp;&nbsp;&nbsp;public String tableName;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * Lone constructor.&nbsp;&nbsp;A datasource must be passed.<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @param s holds the name of the datasource to use<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;Database(String s)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tableName = s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * Native method query<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @param stmt holds the SQLStmt classto use<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @exception DBException is thrown onany error<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;public synchronized native void query(SQLStmtstmt)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws DBException;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public synchronized native SQLStmt sql(Stringstmt)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws DBException;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;static<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.loadLibrary(&quot;Database&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}</TT></BLOCKQUOTE><HR><P>The first native method uses an SQLStmt object for both inputand output, and the second native method uses a String input andreturns an SQLStmt object as output. It is the native library'stask to create and fill the output object. Both native methodsare marked as synchronized because the library implementationis single-threaded. Nothing in Java precludes making re-entrantnative libraries, but the database library uses global variablesfor storage. This makes it necessary to protect the library frombeing entered by more than one thread at a time.<P>As with the Demonstration class, the first step is to compilethe classes and pass them to the <TT>javah</TT>tool:<BLOCKQUOTE><TT>javac SQLStmt.java Database.java<BR>javah SQLStmt Database</TT></BLOCKQUOTE><P>Here is the output for the SQLStmt class:<BLOCKQUOTE><TT>/* DO NOT EDIT THIS FILE - it is machinegenerated */<BR>#include &lt;native.h&gt;<BR>/* Header for class SQLStmt */<BR><BR>#ifndef _Included_SQLStmt<BR>#define _Included_SQLStmt<BR>struct Hjava_lang_String;<BR><BR>typedef struct ClassSQLStmt {<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct Hjava_lang_String *sqlStmt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct HArrayOfArray *result;<BR>&nbsp;&nbsp;&nbsp;&nbsp;long nRows;<BR>&nbsp;&nbsp;&nbsp;&nbsp;long nCols;<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*boolean*/ long query;<BR>} ClassSQLStmt;<BR>HandleTo(SQLStmt);<BR><BR>#ifdef __cplusplus<BR>extern &quot;C&quot; {<BR>#endif<BR>#ifdef __cplusplus<BR>}<BR>#endif<BR>#endif</TT></BLOCKQUOTE><P>Notice how the two-dimensional array has been translated intoHArrayOfArray. SQLStmt doesn't have any native methods, thoughthe <TT>javah</TT> tool still placesthe surrounding <TT>ifdef cplusplus</TT>statements where native methods would normally appear.<P>Here is the output for the Database class:<BLOCKQUOTE><TT>/* DO NOT EDIT THIS FILE - it is machinegenerated */<BR>#include &lt;native.h&gt;<BR>/* Header for class Database */<BR><BR>#ifndef _Included_Database<BR>#define _Included_Database<BR>struct Hjava_lang_String;<BR><BR>typedef struct ClassDatabase {<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct Hjava_lang_String *tableName;<BR>} ClassDatabase;<BR>HandleTo(Database);<BR><BR>#ifdef __cplusplus<BR>extern &quot;C&quot; {<BR>#endif<BR>struct HSQLStmt;<BR>__declspec(dllexport) void Database_query(struct HDatabase *,structHSQLStmt *);<BR>__declspec(dllexport) struct HSQLStmt *Database_sql(struct HDatabase*,struct Hjava_lang_String *);<BR>#ifdef __cplusplus<BR>}<BR>#endif<BR>#endif</TT></BLOCKQUOTE><P>The two native methods appear at the bottom of the header. Sincethis file has native methods, it needs to have stub code generatedfor it. The next step is to execute the <TT>javah</TT>tool with the stubs option:<BLOCKQUOTE><TT>javah -stubs Database<BR></TT></BLOCKQUOTE><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>Note</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>There is no rule about what the stub module must be called. You can use the <TT>-ofilename</TT> option to override <TT>javah</TT>'s default naming convention. This option is also useful for forcing the output from multiple classes into a single stubs file: <TT>javah -stubs -ostubs.c class1 class2 class3</TT>. This can be done as long as all the native methods for classes 1, 2, and 3 will appear in the same native library.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Here is the stub module for the Database class:<BLOCKQUOTE><TT>/* DO NOT EDIT THIS FILE - it is machinegenerated */<BR>#include &lt;StubPreamble.h&gt;<BR><BR>/* Stubs for class Database */<BR>/* SYMBOL: &quot;Database/query(LSQLStmt;)V&quot;, Java_Database_query_stub*/<BR>declspec(dllexport) stack_item *Java_Database_query_stub(stack_item*_P_,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct execenv *_EE_) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;extern void Database_query(void *,void*);<BR>&nbsp;&nbsp;&nbsp;&nbsp;(void) Database_query(_P_[0].p,((_P_[1].p)));<BR>&nbsp;&nbsp;&nbsp;&nbsp;return _P_;<BR>}<BR>/* SYMBOL: &quot;Database/sql(Ljava/lang/String;)LSQLStmt;&quot;,Java_Database_sql_stub */<BR>declspec(dllexport) stack_item *Java_Database_sql_stub(stack_item*_P_,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct execenv *_EE_) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;extern long Database_sql(void *,void *);<BR>&nbsp;&nbsp;&nbsp;&nbsp;_P_[0].i = Database_sql(_P_[0].p,((_P_[1].p)));<BR>&nbsp;&nbsp;&nbsp;&nbsp;return _P_ + 1;<BR>}</TT></BLOCKQUOTE><P>All that's left to do is to write the implementation module forthe Database stub. In the interest of having a comprehensibleproject layout, the name of this module will be DatabaseImpl.c.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -