You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cookbook.xml 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?xml version="1.0"?>
  2. <document>
  3. <properties>
  4. <author email="jahlborn@users.sf.net">James Ahlborn</author>
  5. <title>Jackcess Cookbook</title>
  6. </properties>
  7. <body>
  8. <section name="Introduction">
  9. <p>
  10. This cookbook will attempt to familiarize the reader with the various
  11. nooks and crannies of the Jackcess API. The API is large due to the
  12. large feature-set that an Access Database provides, so this cookbook
  13. will by no means be exhaustive. However, this will hopefully give the
  14. reader enough useful building blocks such that the rest of the API can
  15. be discovered and utilized as necessary.
  16. </p>
  17. <p>
  18. This cookbook is a cross between a tutorial and a reference, so the
  19. reader should be able to skip to relevant sections without needing to
  20. read the entire preceding text.
  21. </p>
  22. <p>
  23. While this cookbook strives to present best practices for both the
  24. Jackcess API and Java programming in general, at times, the code may
  25. be trimmed for the sake of brevity. For the same reason, pseudo-code
  26. may be used in places where the actual code is not relevant to the
  27. example.
  28. </p>
  29. <macro name="toc">
  30. <param name="section" value="2"/>
  31. <param name="fromDepth" value="0"/>
  32. <param name="toDepth" value="4"/>
  33. </macro>
  34. </section>
  35. <section name="The Basics">
  36. <subsection name="Opening an existing Database">
  37. <p>
  38. So you have an Access Database and you want to do something with it.
  39. You want to use Java, and you may not even be running on Windows (or
  40. you tried the JDBC/ODBC bridge and it failed miserably). Through
  41. some Google-fu, you landed here at the Jackcess project. Now what?
  42. </p>
  43. <p>
  44. Well, the first thing you need to do is open the database. The
  45. entry point class in the Jackcess API is, suprisingly enough, the
  46. <a href="apidocs/com/healthmarketscience/jackcess/Database.html">Database</a> class.
  47. </p>
  48. <source>
  49. Database db = Database.open(new File("mydb.mdb"));
  50. </source>
  51. <p>
  52. That's it, now you have a Database instance (maybe this isn't that
  53. difficult after all).
  54. </p>
  55. <p>
  56. Important note, <i>always</i> make sure you close a Database
  57. instance when you are finished with it (preferably in a finally
  58. block like any other important resource). Failure to close the
  59. Database instance could result in data loss or database corruption.
  60. </p>
  61. </subsection>
  62. <subsection name="Reading a Table">
  63. <p>
  64. Okay, so you have a Database instance, now what? Since pretty much
  65. everything in an Access database lives in a table, grabbing a <a href="apidocs/com/healthmarketscience/jackcess/Table.html">Table</a>
  66. would be the logical next step.
  67. </p>
  68. <source>
  69. Table table = db.getTable("Test");
  70. </source>
  71. <p>
  72. Where's the data? While a <a
  73. href="apidocs/com/healthmarketscience/jackcess/Cursor.html">Cursor</a>
  74. is the best way to interact with the data in a Table, for the sake
  75. of simplicity when just getting started, we will use the simplified
  76. iteration provided by the Table class itself. When reading row
  77. data, it is generally provided as a <code>Map&lt;String,Object&gt;</code> where the keys are the column
  78. names and the values are the strongly typed column values.
  79. </p>
  80. <source>
  81. for(Map&lt;String,Object&gt; row : table) {
  82. System.out.prinln("Look ma, a row: " + row);
  83. }
  84. </source>
  85. <p>
  86. So, what's in a row? Well, let's assume your "Test" table is
  87. defined in the following way in Access:
  88. </p>
  89. <blockquote>
  90. <table>
  91. <tr>
  92. <th>Field Name</th><th>Data Type</th>
  93. </tr>
  94. <tr>
  95. <td>ID</td><td>AutoNumber (Long Integer)</td>
  96. </tr>
  97. <tr>
  98. <td>Name</td><td>Text</td>
  99. </tr>
  100. <tr>
  101. <td>Salary</td><td>Currency</td>
  102. </tr>
  103. <tr>
  104. <td>StartDate</td><td>Date/Time</td>
  105. </tr>
  106. </table>
  107. </blockquote>
  108. <p>
  109. Then, given a row of data, we could inspect the various <a href="apidocs/com/healthmarketscience/jackcess/Column.html">Columns</a> and
  110. their values like so:
  111. </p>
  112. <source>
  113. Map&lt;String,Object&gt; row = ...;
  114. for(Column column : table.getColumns()) {
  115. String columnName = column.getName();
  116. Object value = row.get(columnName);
  117. System.out.println("Column " + columnName + "(" + column.getType() + "): "
  118. + value + " (" + value.getClass() + ")");
  119. }
  120. // Example Output:
  121. //
  122. // Column ID(LONG): 27 (java.lang.Integer)
  123. // Column Name(TEXT): Bob Smith (java.lang.String)
  124. // Column Salary(MONEY): 50000.00 (java.math.BigDecimal)
  125. // Column StartDate(SHORT_DATE_TIME): Mon Jan 05 09:00:00 EDT 2010 (java.util.Date)
  126. </source>
  127. <p>
  128. As you can see in this example (and as previously mentioned), the
  129. row values are <i>strongly typed</i> Java objects. In Jackcess, the
  130. column types are represented by a Java enum named <a href="apidocs/com/healthmarketscience/jackcess/DataType.html">DataType</a>.
  131. The DataType javadoc details the Java types used to return row
  132. values as well as the value types which are acceptable inputs for
  133. new rows (more on this later). One other thing to note in this
  134. example is that the column names in the row Map are <i>case
  135. sensitive</i> strings (although other parts of the API strive to
  136. mimic Access's love of case-insensitivity).
  137. </p>
  138. </subsection>
  139. <subsection name="Adding a Row">
  140. <p>
  141. Awesome, so now we can read what's already there. Of course, lots
  142. of tools can do that. Now we want to write some data.
  143. </p>
  144. <p>
  145. The main hurdle to writing data is figuring out how to get the data
  146. in the right columns. The primary method for adding a row to a
  147. Table is the <a href="apidocs/com/healthmarketscience/jackcess/Table.html#addRow(java.lang.Object...)">addRow(Object...)</a>
  148. method. This method should be called with the appropriate, strongly
  149. typed Java object values <i>in the order of the Columns of the
  150. Table</i>. The order of the Columns on the Table instance <i>may
  151. not be the same as the display order of the columns in Access</i>.
  152. (Re-read those last two sentences again, as it will save you a lot of
  153. grief moving forward).
  154. </p>
  155. <p>
  156. Additionally, when adding rows, we never provide a value for any
  157. "auto" columns. You can provide a value (any value in fact), but it
  158. will be ignored (in the example below, we use a useful constant which
  159. makes the intent clear to any future developer).
  160. </p>
  161. <p>
  162. So, assuming that the order of the Columns on the Table instance is
  163. "ID", "Name", "Salary", and "StartDate", this is how we would add a
  164. row to the "Test" table:
  165. </p>
  166. <source>
  167. String name = "bob";
  168. BigDecimal salary = new BigDecimal("1000.00");
  169. Date startDate = new Date();
  170. table.addRow(Column.AUTO_NUMBER, name, salary, startDate);
  171. </source>
  172. <p>
  173. There you have it, a new row in your Access database.
  174. </p>
  175. </subsection>
  176. </section>
  177. <section name="Starting from Scratch">
  178. <subsection name="Creating a new Database">
  179. <p>
  180. While updating existing content is nice, and necessary, many times
  181. we want to create an entire new Database. While Jackcess doesn't
  182. support everything you may need when creating a new database, it
  183. does support a wide range of functionality, and adds more all the
  184. time. (If you started using Jackcess a while ago, you should
  185. definitely keep tabs on the release notes, as your knowledge of what
  186. is possible may be out of date).
  187. </p>
  188. <p>
  189. As of version 1.2.10, Jackcess supports:
  190. </p>
  191. <ul>
  192. <li>Creating databases for Access all versions 2000-2010</li>
  193. <li>Creating columns for all simple data types</li>
  194. <li>Creating tables with single-table Indexes</li>
  195. </ul>
  196. <p>
  197. Some notable gaps:
  198. </p>
  199. <ul>
  200. <li>Cannot currently create (index backed) foreign-key
  201. constraints</li>
  202. <li>Cannot currently create "complex" columns (attachment,
  203. multi-value, versioned memo)</li>
  204. </ul>
  205. <p>
  206. As long as your needs fall into the
  207. </p>
  208. </subsection>
  209. <subsection name="Creating a Table">
  210. <p>
  211. </p>
  212. </subsection>
  213. </section>
  214. </body>
  215. </document>